From 5f3ceea7bf8968c94024196063aaf138e55e4802 Mon Sep 17 00:00:00 2001 From: q66 Date: Thu, 30 Jul 2020 03:32:48 +0200 Subject: [PATCH] drop custom new overloads --- src/engine/iqm.hh | 6 +++++- src/engine/octaedit.cc | 42 +++++++++++++++++++++++++++++---------- src/engine/rendermodel.cc | 2 ++ src/engine/worldio.cc | 10 ++++++++-- src/sauerlib/tools.cc | 32 ----------------------------- src/sauerlib/tools.hh | 9 ++------- src/shared/stream.cc | 11 ++++++++-- src/shared/zip.cc | 8 +++++++- 8 files changed, 64 insertions(+), 56 deletions(-) diff --git a/src/engine/iqm.hh b/src/engine/iqm.hh index 2feef70..e687c00 100644 --- a/src/engine/iqm.hh +++ b/src/engine/iqm.hh @@ -331,7 +331,11 @@ struct iqm : skelloader lilswap(&hdr.version, (sizeof(hdr) - sizeof(hdr.magic))/sizeof(uint)); if(hdr.version != 2) goto error; if(hdr.filesize > (16<<20)) goto error; // sanity check... don't load files bigger than 16 MB - buf = new (false) uchar[hdr.filesize]; + try { + buf = new uchar[hdr.filesize]; + } catch (...) { + goto error; + } if(!buf || f->read(buf + sizeof(hdr), hdr.filesize - sizeof(hdr)) != hdr.filesize - sizeof(hdr)) goto error; if(doloadmesh && !loadiqmmeshes(filename, hdr, buf)) goto error; diff --git a/src/engine/octaedit.cc b/src/engine/octaedit.cc index 3795781..ff3812f 100644 --- a/src/engine/octaedit.cc +++ b/src/engine/octaedit.cc @@ -1,5 +1,7 @@ #include "octaedit.hh" +#include + #include #include #include @@ -671,9 +673,12 @@ static block3 *blockcopy(const block3 &s, int rgrid) { int bsize = sizeof(block3)+sizeof(cube)*s.size(); if(bsize <= 0 || bsize > (100<<20)) return nullptr; - block3 *b = (block3 *)new (false) uchar[bsize]; - if(b) blockcopy(s, rgrid, b); - return b; + try { + auto *b = (block3 *)new uchar[bsize]; + blockcopy(s, rgrid, b); + return b; + } catch (...) {} + return nullptr; } static void freeblock(block3 *b, bool alloced = true) @@ -789,8 +794,12 @@ static undoblock *newundocube(const selinfo &s) selgridsize = ssize, blocksize = sizeof(block3)+ssize*sizeof(cube); if(blocksize <= 0 || blocksize > (undomegs<<20)) return nullptr; - undoblock *u = (undoblock *)new (false) uchar[sizeof(undoblock) + blocksize + selgridsize]; - if(!u) return nullptr; + undoblock *u = nullptr; + try { + u = (undoblock *)new uchar[sizeof(undoblock) + blocksize + selgridsize]; + } catch (...) { + return nullptr; + } u->numents = 0; block3 *b = u->block(); blockcopy(s, -s.grid, b); @@ -995,8 +1004,11 @@ static bool unpackblock(block3 *&b, B &buf) lilswap(&hdr.grid, 1); lilswap(&hdr.orient, 1); if(hdr.size() > (1<<20) || hdr.grid <= 0 || hdr.grid > (1<<12)) return false; - b = (block3 *)new (false) uchar[sizeof(block3)+hdr.size()*sizeof(cube)]; - if(!b) return false; + try { + b = (block3 *)new uchar[sizeof(block3)+hdr.size()*sizeof(cube)]; + } catch (...) { + return false; + } *b = hdr; cube *c = b->c(); memset(c, 0, b->size()*sizeof(cube)); @@ -1052,8 +1064,12 @@ static bool compresseditinfo(const uchar *inbuf, int inlen, uchar *&outbuf, int { uLongf len = compressBound(inlen); if(len > (1<<20)) return false; - outbuf = new (false) uchar[len]; - if(!outbuf || compress2((Bytef *)outbuf, &len, (const Bytef *)inbuf, inlen, Z_BEST_COMPRESSION) != Z_OK || len > (1<<16)) + try { + outbuf = new uchar[len]; + } catch (...) { + return false; + } + if(compress2((Bytef *)outbuf, &len, (const Bytef *)inbuf, inlen, Z_BEST_COMPRESSION) != Z_OK || len > (1<<16)) { delete[] outbuf; outbuf = nullptr; @@ -1067,8 +1083,12 @@ static bool uncompresseditinfo(const uchar *inbuf, int inlen, uchar *&outbuf, in { if(compressBound(outlen) > (1<<20)) return false; uLongf len = outlen; - outbuf = new (false) uchar[len]; - if(!outbuf || uncompress((Bytef *)outbuf, &len, (const Bytef *)inbuf, inlen) != Z_OK) + try { + outbuf = new uchar[len]; + } catch (...) { + return false; + } + if(uncompress((Bytef *)outbuf, &len, (const Bytef *)inbuf, inlen) != Z_OK) { delete[] outbuf; outbuf = nullptr; diff --git a/src/engine/rendermodel.cc b/src/engine/rendermodel.cc index f209684..b735a46 100644 --- a/src/engine/rendermodel.cc +++ b/src/engine/rendermodel.cc @@ -1,5 +1,7 @@ #include "rendermodel.hh" +#include + #include #include #include diff --git a/src/engine/worldio.cc b/src/engine/worldio.cc index b4ac3df..849fe3b 100644 --- a/src/engine/worldio.cc +++ b/src/engine/worldio.cc @@ -2,6 +2,8 @@ #include "worldio.hh" +#include + #include #include @@ -500,8 +502,12 @@ static void loadvslot(stream *f, VSlot &vs, int changed) static void loadvslots(stream *f, int numvslots) { - int *prev = new (false) int[numvslots]; - if(!prev) return; + int *prev; + try { + prev = new int[numvslots]; + } catch (...) { + return; + } memset(prev, -1, numvslots*sizeof(int)); while(numvslots > 0) { diff --git a/src/sauerlib/tools.cc b/src/sauerlib/tools.cc index 2c7e6b7..8a52346 100644 --- a/src/sauerlib/tools.cc +++ b/src/sauerlib/tools.cc @@ -4,38 +4,6 @@ #include -void *operator new(size_t size) -{ - void *p = malloc(size); - if(!p) abort(); - return p; -} - -void *operator new[](size_t size) -{ - void *p = malloc(size); - if(!p) abort(); - return p; -} - -void operator delete(void *p) { if(p) free(p); } - -void operator delete[](void *p) { if(p) free(p); } - -void *operator new(size_t size, bool err) -{ - void *p = malloc(size); - if(!p && err) abort(); - return p; -} - -void *operator new[](size_t size, bool err) -{ - void *p = malloc(size); - if(!p && err) abort(); - return p; -} - ////////////////////////// strings //////////////////////////////////////// static string tmpstr[4]; diff --git a/src/sauerlib/tools.hh b/src/sauerlib/tools.hh index 7bffa06..830ec9f 100644 --- a/src/sauerlib/tools.hh +++ b/src/sauerlib/tools.hh @@ -8,6 +8,8 @@ #include #include +#include + #include #include @@ -36,13 +38,6 @@ typedef unsigned long long int ullong; #define UNUSED #endif -void *operator new(size_t, bool); -void *operator new[](size_t, bool); -inline void *operator new(size_t, void *p) { return p; } -inline void *operator new[](size_t, void *p) { return p; } -inline void operator delete(void *, void *) {} -inline void operator delete[](void *, void *) {} - #ifdef swap #undef swap #endif diff --git a/src/shared/stream.cc b/src/shared/stream.cc index 9850b7b..af0fab7 100644 --- a/src/shared/stream.cc +++ b/src/shared/stream.cc @@ -1,3 +1,5 @@ +#include + #include "command.hh" #include /* conoutf */ @@ -1220,8 +1222,13 @@ char *loadfile(const char *fn, size_t *size, bool utf8) stream::offset fsize = f->size(); if(fsize <= 0) { delete f; return nullptr; } size_t len = fsize; - char *buf = new (false) char[len+1]; - if(!buf) { delete f; return nullptr; } + char *buf; + try { + buf = new char[len + 1]; + } catch (...) { + delete f; + return nullptr; + } size_t offset = 0; if(utf8 && len >= 3) { diff --git a/src/shared/zip.cc b/src/shared/zip.cc index 10b4792..24ebd4c 100644 --- a/src/shared/zip.cc +++ b/src/shared/zip.cc @@ -1,3 +1,5 @@ +#include + #include "command.hh" #include /* conoutf */ @@ -117,7 +119,11 @@ VAR(dbgzip, 0, 0, 1); static bool readzipdirectory(const char *archname, FILE *f, int entries, int offset, uint size, vector &files) { - uchar *buf = new (false) uchar[size], *src = buf; + uchar *buf = nullptr; + try { + buf = new uchar[size]; + } catch (...) {} + uchar *src = buf; if(!buf || fseek(f, offset, SEEK_SET) < 0 || fread(buf, 1, size, f) != size) { delete[] buf; return false; } loopi(entries) {