From 44248f19b5291d6adc41be06ced081f8bd90f085 Mon Sep 17 00:00:00 2001 From: "Markus F.X.J. Oberhumer" Date: Tue, 20 Sep 2016 15:24:07 +0200 Subject: [PATCH] Introduce mem_size() and New() and start using it. --- src/conf.h | 3 +-- src/mem.cpp | 59 ++++++++++++++++++++++++++++++++++++------------ src/mem.h | 7 ++++++ src/p_lx_elf.cpp | 38 +++++++++++++++---------------- src/p_vmlinx.cpp | 2 +- src/p_wcle.cpp | 14 ++++++------ src/pefile.cpp | 42 +++++++++++++++++----------------- src/work.cpp | 2 +- 8 files changed, 102 insertions(+), 65 deletions(-) diff --git a/src/conf.h b/src/conf.h index 783945a5..a7e79e80 100644 --- a/src/conf.h +++ b/src/conf.h @@ -297,8 +297,7 @@ inline const T& UPX_MIN(const T& a, const T& b) { if (a < b) return a; return b; // An Array allocates memory on the heap, but automatically // gets destructed when leaving scope or on exceptions. #define Array(type, var, size) \ - assert((int)(size) > 0); \ - MemBuffer var ## _membuf((size)*(sizeof(type))); \ + MemBuffer var ## _membuf(mem_size(sizeof(type), size)); \ type * const var = ((type *) var ## _membuf.getVoidPtr()) #define ByteArray(var, size) Array(unsigned char, var, size) diff --git a/src/mem.cpp b/src/mem.cpp index 76c8a967..fc3c3345 100644 --- a/src/mem.cpp +++ b/src/mem.cpp @@ -30,6 +30,43 @@ #include "mem.h" +/************************************************************************* +// +**************************************************************************/ + +// DO NOT CHANGE +#define MAX_SIZE (768 * 1024 * 1024) +ACC_COMPILE_TIME_ASSERT_HEADER(2ull * MAX_SIZE * 9 / 8 + 16*1024*1024 < INT_MAX) + +size_t mem_size(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra) +{ + assert(element_size > 0); + if (element_size > MAX_SIZE) throwCantPack("mem_size 1; take care"); + if (n > MAX_SIZE) throwCantPack("mem_size 2; take care"); + if (extra > MAX_SIZE) throwCantPack("mem_size 3; take care"); + upx_uint64_t bytes = element_size * n + extra; // cannot overflow + if (bytes > MAX_SIZE) throwCantPack("mem_size 4; take care"); + return ACC_ICONV(size_t, bytes); +} + +size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n) +{ + (void) mem_size(element_size, n); // check + return ACC_ICONV(size_t, n); // return n +} + +bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra) +{ + assert(element_size > 0); + if (element_size > MAX_SIZE) return false; + if (n > MAX_SIZE) return false; + if (extra > MAX_SIZE) return false; + upx_uint64_t bytes = element_size * n + extra; // cannot overflow + if (bytes > MAX_SIZE) return false; + return true; +} + + /************************************************************************* // **************************************************************************/ @@ -103,23 +140,19 @@ void MemBuffer::dealloc() unsigned MemBuffer::getSizeForCompression(unsigned uncompressed_size, unsigned extra) { - assert((int)uncompressed_size > 0); - assert((int)extra >= 0); - unsigned size = uncompressed_size + uncompressed_size/8 + 256 + extra; - return size; + size_t bytes = mem_size(1, uncompressed_size, extra); + bytes += uncompressed_size/8 + 256; + return ACC_ICONV(unsigned, bytes); } unsigned MemBuffer::getSizeForUncompression(unsigned uncompressed_size, unsigned extra) { - assert((int)uncompressed_size > 0); - assert((int)extra >= 0); - unsigned size = uncompressed_size + extra; -// size += 512; // 512 safety bytes + size_t bytes = mem_size(1, uncompressed_size, extra); // INFO: 3 bytes are the allowed overrun for the i386 asm_fast decompressors #if (ACC_ARCH_I386) - size += 3; + bytes += 3; #endif - return size; + return ACC_ICONV(unsigned, bytes); } @@ -187,10 +220,8 @@ void MemBuffer::alloc(unsigned size) assert(b == NULL); assert(b_size == 0); // - assert((int)size > 0); - unsigned total = use_mcheck ? size + 32 : size; - assert((int)total > 0); - unsigned char *p = (unsigned char *) malloc(total); + size_t bytes = mem_size(1, size, use_mcheck ? 32 : 0); + unsigned char *p = (unsigned char *) malloc(bytes); if (!p) throwOutOfMemoryException(); b_size = size; diff --git a/src/mem.h b/src/mem.h index 59d5a387..f21215ad 100644 --- a/src/mem.h +++ b/src/mem.h @@ -34,6 +34,13 @@ // **************************************************************************/ +size_t mem_size (upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0); +size_t mem_size_get_n(upx_uint64_t element_size, upx_uint64_t n); +bool mem_size_valid(upx_uint64_t element_size, upx_uint64_t n, upx_uint64_t extra=0); + +#define New(type,n) new type [ mem_size_get_n(sizeof(type),n) ] + + class MemBuffer { public: diff --git a/src/p_lx_elf.cpp b/src/p_lx_elf.cpp index c5714fe1..8589afc4 100644 --- a/src/p_lx_elf.cpp +++ b/src/p_lx_elf.cpp @@ -230,14 +230,14 @@ PackLinuxElf32::PackLinuxElf32help1(InputFile *f) if (f && Elf32_Ehdr::ET_DYN!=e_type) { unsigned const len = sz_phdrs + e_phoff; - file_image = new char[len]; + file_image = New(char, len); f->seek(0, SEEK_SET); f->readx(file_image, len); phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !! } if (f && Elf32_Ehdr::ET_DYN==e_type) { // The DT_STRTAB has no designated length. Read the whole file. - file_image = new char[file_size]; + file_image = New(char, file_size); f->seek(0, SEEK_SET); f->readx(file_image, file_size); phdri= (Elf32_Phdr *)(e_phoff + file_image); // do not free() !! @@ -599,14 +599,14 @@ PackLinuxElf64::PackLinuxElf64help1(InputFile *f) if (f && Elf64_Ehdr::ET_DYN!=e_type) { unsigned const len = sz_phdrs + e_phoff; - file_image = new char[len]; + file_image = New(char, len); f->seek(0, SEEK_SET); f->readx(file_image, len); phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !! } if (f && Elf64_Ehdr::ET_DYN==e_type) { // The DT_STRTAB has no designated length. Read the whole file. - file_image = new char[file_size]; + file_image = New(char, file_size); f->seek(0, SEEK_SET); f->readx(file_image, file_size); phdri= (Elf64_Phdr *)(e_phoff + file_image); // do not free() !! @@ -909,7 +909,7 @@ PackLinuxElf32::buildLinuxLoader( unsigned char const *const uncLoader = fold_hdrlen + fold; h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc)); - unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_cpr]; + unsigned char *const cprLoader = New(unsigned char, sizeof(h) + h.sz_cpr); int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr, NULL, ph.method, 10, NULL, NULL ); if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc) @@ -917,7 +917,7 @@ PackLinuxElf32::buildLinuxLoader( #if 0 //{ debugging only if (M_IS_LZMA(ph.method)) { ucl_uint tmp_len = h.sz_unc; // LZMA uses this as EOF - unsigned char *tmp = new unsigned char[tmp_len]; + unsigned char *tmp = New(unsigned char, tmp_len); memset(tmp, 0, tmp_len); r = upx_decompress(sizeof(h) + cprLoader, h.sz_cpr, tmp, &tmp_len, h.b_method, NULL); if (r == UPX_E_OUT_OF_MEMORY) @@ -974,7 +974,7 @@ PackLinuxElf64::buildLinuxLoader( unsigned char const *const uncLoader = fold_hdrlen + fold; h.sz_cpr = MemBuffer::getSizeForCompression(h.sz_unc + (0==h.sz_unc)); - unsigned char *const cprLoader = new unsigned char[sizeof(h) + h.sz_cpr]; + unsigned char *const cprLoader = New(unsigned char, sizeof(h) + h.sz_cpr); int r = upx_compress(uncLoader, h.sz_unc, sizeof(h) + cprLoader, &h.sz_cpr, NULL, ph.method, 10, NULL, NULL ); if (r != UPX_E_OK || h.sz_cpr >= h.sz_unc) @@ -1660,7 +1660,7 @@ PackLinuxElf64ppcle::canPack() if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) { // The DT_STRTAB has no designated length. Read the whole file. - file_image = new char[file_size]; + file_image = New(char, file_size); fi->seek(0, SEEK_SET); fi->readx(file_image, file_size); memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr)); @@ -1835,7 +1835,7 @@ PackLinuxElf64amd::canPack() if (Elf32_Ehdr::ET_DYN==get_te16(&ehdr->e_type)) { // The DT_STRTAB has no designated length. Read the whole file. - file_image = new char[file_size]; + file_image = New(char, file_size); fi->seek(0, SEEK_SET); fi->readx(file_image, file_size); memcpy(&ehdri, ehdr, sizeof(Elf64_Ehdr)); @@ -2300,7 +2300,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/) } } if (note_size) { - note_body = new unsigned char[note_size]; + note_body = New(unsigned char, note_size); note_size = 0; } phdr = phdri; @@ -2340,7 +2340,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/) Elf32_Shdr const *tmp = shdri; if (! shdri) { - shdr = new Elf32_Shdr[e_shnum]; + shdr = New(Elf32_Shdr, e_shnum); fi->seek(0,SEEK_SET); fi->seek(ehdri.e_shoff,SEEK_SET); @@ -2353,7 +2353,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/) //set the shstrtab sec_strndx = &shdr[ehdri.e_shstrndx]; - char *strtab = new char[(unsigned) sec_strndx->sh_size]; + char *strtab = New(char, sec_strndx->sh_size); fi->seek(0,SEEK_SET); fi->seek(sec_strndx->sh_offset,SEEK_SET); fi->readx(strtab,sec_strndx->sh_size); @@ -2362,7 +2362,7 @@ void PackLinuxElf32::pack1(OutputFile *fo, Filter & /*ft*/) Elf32_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id"); if (buildid) { - unsigned char *data = new unsigned char[(unsigned) buildid->sh_size]; + unsigned char *data = New(unsigned char, buildid->sh_size); memset(data,0,buildid->sh_size); fi->seek(0,SEEK_SET); fi->seek(buildid->sh_offset,SEEK_SET); @@ -2493,7 +2493,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/) } } if (note_size) { - note_body = new unsigned char[note_size]; + note_body = New(unsigned char, note_size); note_size = 0; } phdr = phdri; @@ -2538,7 +2538,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/) Elf64_Shdr *shdr = NULL; if (! shdri) { - shdr = new Elf64_Shdr[e_shnum]; + shdr = New(Elf64_Shdr, e_shnum); fi->seek(0,SEEK_SET); fi->seek(ehdri.e_shoff,SEEK_SET); @@ -2551,7 +2551,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/) //set the shstrtab sec_strndx = &shdri[ehdri.e_shstrndx]; - char *strtab = new char[(unsigned) sec_strndx->sh_size]; + char *strtab = New(char, sec_strndx->sh_size); fi->seek(0,SEEK_SET); fi->seek(sec_strndx->sh_offset,SEEK_SET); fi->readx(strtab,sec_strndx->sh_size); @@ -2560,7 +2560,7 @@ void PackLinuxElf64::pack1(OutputFile *fo, Filter & /*ft*/) Elf64_Shdr const *buildid = elf_find_section_name(".note.gnu.build-id"); if (buildid) { - unsigned char *data = new unsigned char[(unsigned) buildid->sh_size]; + unsigned char *data = New(unsigned char, buildid->sh_size); memset(data,0,buildid->sh_size); fi->seek(0,SEEK_SET); fi->seek(buildid->sh_offset,SEEK_SET); @@ -3253,7 +3253,7 @@ void PackLinuxElf64::unpack(OutputFile *fo) unsigned orig_file_size = get_te32(&hbuf.p_filesize); blocksize = get_te32(&hbuf.p_blocksize); if (file_size > (off_t)orig_file_size || blocksize > orig_file_size - || blocksize > 1024*1024*1024) + || !mem_size_valid(1, blocksize, OVERHEAD)) throwCantUnpack("p_info corrupted"); ibuf.alloc(blocksize + OVERHEAD); @@ -3782,7 +3782,7 @@ void PackLinuxElf32::unpack(OutputFile *fo) unsigned orig_file_size = get_te32(&hbuf.p_filesize); blocksize = get_te32(&hbuf.p_blocksize); if (file_size > (off_t)orig_file_size || blocksize > orig_file_size - || blocksize > 1024*1024*1024) + || !mem_size_valid(1, blocksize, OVERHEAD)) throwCantUnpack("p_info corrupted"); ibuf.alloc(blocksize + OVERHEAD); diff --git a/src/p_vmlinx.cpp b/src/p_vmlinx.cpp index 2768f524..4c7fb235 100644 --- a/src/p_vmlinx.cpp +++ b/src/p_vmlinx.cpp @@ -98,7 +98,7 @@ PackVmlinuxBase::compare_Phdr(void const *aa, void const *bb) if (xa > xb) return 1; if (a->p_paddr < b->p_paddr) return -1; // ascending by .p_paddr if (a->p_paddr > b->p_paddr) return 1; - return 0; + return 0; } template diff --git a/src/p_wcle.cpp b/src/p_wcle.cpp index f059d623..3e9926fd 100644 --- a/src/p_wcle.cpp +++ b/src/p_wcle.cpp @@ -207,7 +207,7 @@ void PackWcle::encodeObjectTable() { unsigned ic,jc; - oobject_table = new le_object_table_entry_t[soobject_table = 2]; + oobject_table = New(le_object_table_entry_t, soobject_table = 2); memset(oobject_table,0,soobject_table * sizeof(*oobject_table)); // object #1: @@ -242,7 +242,7 @@ void PackWcle::encodeObjectTable() void PackWcle::encodePageMap() { - opm_entries = new le_pagemap_entry_t[sopm_entries = opages]; + opm_entries = New(le_pagemap_entry_t, sopm_entries = opages); for (unsigned ic = 0; ic < sopm_entries; ic++) { opm_entries[ic].l = (unsigned char) (ic+1); @@ -256,7 +256,7 @@ void PackWcle::encodePageMap() void PackWcle::encodeFixupPageTable() { unsigned ic; - ofpage_table = new unsigned[sofpage_table = 1 + opages]; + ofpage_table = New(unsigned, sofpage_table = 1 + opages); for (ofpage_table[0] = ic = 0; ic < opages; ic++) set_le32(ofpage_table+ic+1,sofixups-FIXUP_EXTRA); } @@ -264,7 +264,7 @@ void PackWcle::encodeFixupPageTable() void PackWcle::encodeFixups() { - ofixups = new upx_byte[sofixups = 1*7 + FIXUP_EXTRA]; + ofixups = New(upx_byte, sofixups = 1*7 + FIXUP_EXTRA); memset(ofixups,0,sofixups); ofixups[0] = 7; set_le16(ofixups+2,(LE_STUB_EDI + neweip) & (mps-1)); @@ -608,7 +608,7 @@ void PackWcle::decodeFixups() selfrel_fixups++; unsigned selectlen = ptr_diff(selfrel_fixups, selector_fixups)/9; - ofixups = new upx_byte[fixupn*9+1000+selectlen*5]; + ofixups = New(upx_byte, fixupn*9+1000+selectlen*5); upx_bytep fp = ofixups; for (ic = 1, jc = 0; ic <= opages; ic++) @@ -682,7 +682,7 @@ void PackWcle::decodeFixups() void PackWcle::decodeFixupPageTable() { - ofpage_table = new unsigned[sofpage_table = 1 + opages]; + ofpage_table = New(unsigned, sofpage_table = 1 + opages); set_le32(ofpage_table,0); // the rest of ofpage_table is filled by decodeFixups() } @@ -691,7 +691,7 @@ void PackWcle::decodeFixupPageTable() void PackWcle::decodeObjectTable() { soobject_table = oimage[ph.u_len - 1]; - oobject_table = new le_object_table_entry_t[soobject_table]; + oobject_table = New(le_object_table_entry_t, soobject_table); unsigned jc, ic = soobject_table * sizeof(*oobject_table); const unsigned extradata = ph.version == 10 ? 17 : 13; diff --git a/src/pefile.cpp b/src/pefile.cpp index d9f210d0..493d80f6 100644 --- a/src/pefile.cpp +++ b/src/pefile.cpp @@ -310,7 +310,7 @@ PeFile::Reloc::Reloc(upx_byte *s,unsigned si) : PeFile::Reloc::Reloc(unsigned rnum) : start(NULL), size(0), rel(NULL), rel1(NULL) { - start = new upx_byte[rnum * 4 + 8192]; + start = new upx_byte[mem_size(4, rnum, 8192)]; counts[0] = 0; } @@ -393,7 +393,7 @@ void PeFile32::processRelocs() // pass1 LE32 *fix[4]; for (; ic; ic--) - fix[ic] = new LE32 [counts[ic]]; + fix[ic] = New(LE32, counts[ic]); unsigned xcounts[4]; memset(xcounts, 0, sizeof(xcounts)); @@ -430,7 +430,7 @@ void PeFile32::processRelocs() // pass1 } ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); - orelocs = new upx_byte [rnum * 4 + 1024]; // 1024 - safety + orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety sorelocs = ptr_diff(optimizeReloc32((upx_byte*) fix[3], xcounts[3], orelocs, ibuf + rvamin,1, &big_relocs), orelocs); @@ -488,7 +488,7 @@ void PeFile64::processRelocs() // pass1 LE32 *fix[16]; for (ic = 15; ic; ic--) - fix[ic] = new LE32 [counts[ic]]; + fix[ic] = New(LE32, counts[ic]); unsigned xcounts[16]; memset(xcounts, 0, sizeof(xcounts)); @@ -528,7 +528,7 @@ void PeFile64::processRelocs() // pass1 } ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL); - orelocs = new upx_byte [rnum * 4 + 1024]; // 1024 - safety + orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety sorelocs = ptr_diff(optimizeReloc64((upx_byte*) fix[10], xcounts[10], orelocs, ibuf + rvamin,1, &big_relocs), orelocs); @@ -617,7 +617,7 @@ class PeFile::ImportLinker : public ElfLinkerAMD64 unsigned l = strlen(dll); assert(l > 0); - char *name = new char[3 * l + 2]; + char *name = New(char, 3 * l + 2); assert(name); name[0] = first_char; char *n = name + 1 + 2 * l; @@ -632,7 +632,7 @@ class PeFile::ImportLinker : public ElfLinkerAMD64 { unsigned len = 1 + 2 * strlen(dll) + 1 + 2 * strlen(proc) + 1 + 1; tstr dlln(name_for_dll(dll, first_char)); - char *procn = new char[len]; + char *procn = New(char, len); upx_snprintf(procn, len - 1, "%s%c", (const char*) dlln, separator); encode_name(proc, procn + strlen(procn)); return procn; @@ -761,7 +761,7 @@ public: int osize = 4 + 2 * nsections; // upper limit for alignments for (unsigned ic = 0; ic < nsections; ic++) osize += sections[ic]->size; - output = new upx_byte[osize]; + output = New(upx_byte, osize); outputlen = 0; // sort the sections by name before adding them all @@ -936,7 +936,7 @@ unsigned PeFile::processImports0(ord_mask_t ord_mask) // pass 1 soimport++; // separator } } - oimport = new upx_byte[soimport]; + oimport = New(upx_byte, soimport); memset(oimport,0,soimport); qsort(idlls,dllnum,sizeof (udll*),udll::compare); @@ -1112,13 +1112,13 @@ void PeFile::Export::convert(unsigned eoffs,unsigned esize) iv.add(edir.name,len); len = 4 * edir.functions; - functionptrs = new char[len + 1]; + functionptrs = New(char, len + 1); memcpy(functionptrs,base + edir.addrtable,len); size += len; iv.add(edir.addrtable,len); unsigned ic; - names = new char* [edir.names + edir.functions + 1]; + names = New(char *, edir.names + edir.functions + 1); for (ic = 0; ic < edir.names; ic++) { char *n = base + get_le32(base + edir.nameptrtable + ic * 4); @@ -1145,7 +1145,7 @@ void PeFile::Export::convert(unsigned eoffs,unsigned esize) names[ic + edir.names] = NULL; len = 2 * edir.names; - ordinals = new char[len + 1]; + ordinals = New(char, len + 1); memcpy(ordinals,base + edir.ordinaltable,len); size += len; iv.add(edir.ordinaltable,len); @@ -1207,7 +1207,7 @@ void PeFile::processExports(Export *xport) // pass1 } xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT)); soexport = ALIGN_UP(xport->getsize(), 4u); - oexport = new upx_byte[soexport]; + oexport = New(upx_byte, soexport); memset(oexport, 0, soexport); } @@ -1327,7 +1327,7 @@ void PeFile::processTls1(Interval *iv, sotls = ALIGN_UP(sotls, cb_size) + 2 * cb_size; // the PE loader wants this stuff uncompressed - otls = new upx_byte[sotls]; + otls = New(upx_byte, sotls); memset(otls,0,sotls); memcpy(otls,ibuf + IDADDR(PEDIR_TLS),sizeof(tls)); // WARNING: this can acces data in BSS @@ -1419,7 +1419,7 @@ void PeFile::processLoadConf(Interval *iv) // pass 1 // printf("loadconf reloc detected: %x\n", pos); } - oloadconf = new upx_byte[soloadconf]; + oloadconf = New(upx_byte, soloadconf); memcpy(oloadconf, loadconf, soloadconf); } @@ -1643,7 +1643,7 @@ PeFile::Resource::upx_rnode *PeFile::Resource::convert(const void *rnode, ibufcheck(p, 2); const unsigned len = 2 + 2 * get_le16(p); ibufcheck(p, len); - child->name = new upx_byte[len]; + child->name = New(upx_byte, len); memcpy(child->name,p,len); // copy unicode string ssize += len; // size of unicode strings } @@ -1698,7 +1698,7 @@ void PeFile::Resource::build(const upx_rnode *node, unsigned &bpos, upx_byte *PeFile::Resource::build() { - newstart = new upx_byte [dirsize()]; + newstart = New(upx_byte, dirsize()); unsigned bpos = 0,spos = dsize; build(root,bpos,spos,0); @@ -1864,7 +1864,7 @@ void PeFile::processResources(Resource *res) for (soresources = res->dirsize(); res->next(); soresources += 4 + res->size()) ; - oresources = new upx_byte[soresources]; + oresources = New(upx_byte, soresources); upx_byte *ores = oresources + res->dirsize(); char *keep_icons = NULL; // icon ids in the first icon group @@ -1874,7 +1874,7 @@ void PeFile::processResources(Resource *res) if (res->itype() == RT_GROUP_ICON && iconsin1stdir == 0) { iconsin1stdir = get_le16(ibuf + res->offs() + 4); - keep_icons = new char[1 + iconsin1stdir * 9]; + keep_icons = New(char, 1 + iconsin1stdir * 9); *keep_icons = 0; for (unsigned ic = 0; ic < iconsin1stdir; ic++) upx_snprintf(keep_icons + strlen(keep_icons), 9, "3/%u,", @@ -2030,7 +2030,7 @@ unsigned PeFile::stripDebug(unsigned overlaystart) void PeFile::readSectionHeaders(unsigned objs, unsigned sizeof_ih) { - isection = new pe_section_t[objs]; + isection = New(pe_section_t, objs); fi->seek(pe_offset+sizeof_ih,SEEK_SET); fi->readx(isection,sizeof(pe_section_t)*objs); rvamin = isection[0].vaddr; @@ -2920,7 +2920,7 @@ int PeFile::canUnpack0(unsigned max_sections, LE16 &ih_objects, return false; unsigned objs = ih_objects; - isection = new pe_section_t[objs]; + isection = New(pe_section_t, objs); fi->seek(pe_offset + ihsize, SEEK_SET); fi->readx(isection,sizeof(pe_section_t)*objs); if (ih_objects < 3) diff --git a/src/work.cpp b/src/work.cpp index f40c2eec..802d3566 100644 --- a/src/work.cpp +++ b/src/work.cpp @@ -83,7 +83,7 @@ void do_one_file(const char *iname, char *oname) throwIOException("empty file -- skipped"); if (st.st_size < 512) throwIOException("file is too small -- skipped"); - if (st.st_size >= 1024*1024*1024) + if (!mem_size_valid(1, st.st_size)) throwIOException("file is too large -- skipped"); if ((st.st_mode & S_IWUSR) == 0) {