Prefer MemBuffer instead of New()+delete
https://github.com/upx/upx/issues/428 modified: pefile.cpp modified: pefile.h
This commit is contained in:
+22
-18
@@ -372,7 +372,8 @@ void PeFile32::processRelocs() // pass1
|
|||||||
{
|
{
|
||||||
if (IDSIZE(PEDIR_RELOC))
|
if (IDSIZE(PEDIR_RELOC))
|
||||||
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
|
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
|
||||||
orelocs = new upx_byte [1];
|
mb_orelocs.alloc(1);
|
||||||
|
orelocs = (upx_byte *)mb_orelocs.getVoidPtr();
|
||||||
sorelocs = 0;
|
sorelocs = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -422,7 +423,8 @@ void PeFile32::processRelocs() // pass1
|
|||||||
}
|
}
|
||||||
|
|
||||||
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
|
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
|
||||||
orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety
|
mb_orelocs.alloc(mem_size(4, rnum, 1024)); // 1024 - safety
|
||||||
|
orelocs = (upx_byte *)mb_orelocs.getVoidPtr();
|
||||||
sorelocs = ptr_diff(optimizeReloc32((upx_byte*) fix[3], xcounts[3],
|
sorelocs = ptr_diff(optimizeReloc32((upx_byte*) fix[3], xcounts[3],
|
||||||
orelocs, ibuf + rvamin, 1, &big_relocs),
|
orelocs, ibuf + rvamin, 1, &big_relocs),
|
||||||
orelocs);
|
orelocs);
|
||||||
@@ -471,7 +473,8 @@ void PeFile64::processRelocs() // pass1
|
|||||||
{
|
{
|
||||||
if (IDSIZE(PEDIR_RELOC))
|
if (IDSIZE(PEDIR_RELOC))
|
||||||
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
|
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
|
||||||
orelocs = new upx_byte [1];
|
mb_orelocs.alloc(1);
|
||||||
|
orelocs = (upx_byte *)mb_orelocs.getVoidPtr();
|
||||||
sorelocs = 0;
|
sorelocs = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -523,7 +526,8 @@ void PeFile64::processRelocs() // pass1
|
|||||||
}
|
}
|
||||||
|
|
||||||
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
|
ibuf.fill(IDADDR(PEDIR_RELOC), IDSIZE(PEDIR_RELOC), FILLVAL);
|
||||||
orelocs = new upx_byte [mem_size(4, rnum, 1024)]; // 1024 - safety
|
mb_orelocs.alloc(mem_size(4, rnum, 1024)); // 1024 - safety
|
||||||
|
orelocs = (upx_byte *)mb_orelocs.getVoidPtr();
|
||||||
sorelocs = ptr_diff(optimizeReloc64((upx_byte*) fix[10], xcounts[10],
|
sorelocs = ptr_diff(optimizeReloc64((upx_byte*) fix[10], xcounts[10],
|
||||||
orelocs, ibuf + rvamin, 1, &big_relocs),
|
orelocs, ibuf + rvamin, 1, &big_relocs),
|
||||||
orelocs);
|
orelocs);
|
||||||
@@ -938,7 +942,8 @@ unsigned PeFile::processImports0(ord_mask_t ord_mask) // pass 1
|
|||||||
soimport++; // separator
|
soimport++; // separator
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
oimport = New(upx_byte, soimport);
|
mb_oimport.alloc(soimport);
|
||||||
|
oimport = (upx_byte *)mb_oimport.getVoidPtr();
|
||||||
memset(oimport,0,soimport);
|
memset(oimport,0,soimport);
|
||||||
|
|
||||||
qsort(idlls,dllnum,sizeof (udll*),udll::compare);
|
qsort(idlls,dllnum,sizeof (udll*),udll::compare);
|
||||||
@@ -1218,7 +1223,8 @@ void PeFile::processExports(Export *xport) // pass1
|
|||||||
}
|
}
|
||||||
xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT));
|
xport->convert(IDADDR(PEDIR_EXPORT),IDSIZE(PEDIR_EXPORT));
|
||||||
soexport = ALIGN_UP(xport->getsize(), 4u);
|
soexport = ALIGN_UP(xport->getsize(), 4u);
|
||||||
oexport = New(upx_byte, soexport);
|
mb_oexport.alloc(soexport);
|
||||||
|
oexport = (upx_byte *)mb_oexport.getVoidPtr();
|
||||||
memset(oexport, 0, soexport);
|
memset(oexport, 0, soexport);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1346,7 +1352,8 @@ void PeFile::processTls1(Interval *iv,
|
|||||||
sotls = ALIGN_UP(sotls, cb_size) + 2 * cb_size;
|
sotls = ALIGN_UP(sotls, cb_size) + 2 * cb_size;
|
||||||
|
|
||||||
// the PE loader wants this stuff uncompressed
|
// the PE loader wants this stuff uncompressed
|
||||||
otls = New(upx_byte, sotls);
|
mb_otls.alloc(sotls);
|
||||||
|
otls = (upx_byte *)mb_otls.getVoidPtr();
|
||||||
memset(otls,0,sotls);
|
memset(otls,0,sotls);
|
||||||
unsigned const take1 = sizeof(tls);
|
unsigned const take1 = sizeof(tls);
|
||||||
unsigned const skip1 = IDADDR(PEDIR_TLS);
|
unsigned const skip1 = IDADDR(PEDIR_TLS);
|
||||||
@@ -1450,7 +1457,8 @@ void PeFile::processLoadConf(Interval *iv) // pass 1
|
|||||||
// printf("loadconf reloc detected: %x\n", pos);
|
// printf("loadconf reloc detected: %x\n", pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
oloadconf = New(upx_byte, soloadconf);
|
mb_oloadconf.alloc(soloadconf);
|
||||||
|
oloadconf = (upx_byte *)mb_oloadconf.getVoidPtr();
|
||||||
memcpy(oloadconf, loadconf, soloadconf);
|
memcpy(oloadconf, loadconf, soloadconf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1903,7 +1911,8 @@ void PeFile::processResources(Resource *res)
|
|||||||
|
|
||||||
for (soresources = res->dirsize(); res->next(); soresources += 4 + res->size())
|
for (soresources = res->dirsize(); res->next(); soresources += 4 + res->size())
|
||||||
;
|
;
|
||||||
oresources = New(upx_byte, soresources);
|
mb_oresources.alloc(soresources);
|
||||||
|
oresources = (upx_byte *)mb_oresources.getVoidPtr();
|
||||||
upx_byte *ores = oresources + res->dirsize();
|
upx_byte *ores = oresources + res->dirsize();
|
||||||
|
|
||||||
char *keep_icons = NULL; // icon ids in the first icon group
|
char *keep_icons = NULL; // icon ids in the first icon group
|
||||||
@@ -2075,7 +2084,8 @@ void PeFile::readSectionHeaders(unsigned objs, unsigned sizeof_ih)
|
|||||||
if (!objs) {
|
if (!objs) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
isection = New(pe_section_t, objs);
|
mb_isection.alloc(sizeof(pe_section_t) * objs);
|
||||||
|
isection = (pe_section_t *)mb_isection.getVoidPtr();
|
||||||
if (file_size < (off_t)(pe_offset + sizeof_ih + sizeof(pe_section_t)*objs)) {
|
if (file_size < (off_t)(pe_offset + sizeof_ih + sizeof(pe_section_t)*objs)) {
|
||||||
char buf[32]; snprintf(buf, sizeof(buf), "too many sections %d", objs);
|
char buf[32]; snprintf(buf, sizeof(buf), "too many sections %d", objs);
|
||||||
throwCantPack(buf);
|
throwCantPack(buf);
|
||||||
@@ -3007,7 +3017,8 @@ int PeFile::canUnpack0(unsigned max_sections, LE16 &ih_objects,
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
unsigned objs = ih_objects;
|
unsigned objs = ih_objects;
|
||||||
isection = New(pe_section_t, objs);
|
mb_isection.alloc(sizeof(pe_section_t) * objs);
|
||||||
|
isection = (pe_section_t *)mb_isection.getVoidPtr();
|
||||||
fi->seek(pe_offset + ihsize, SEEK_SET);
|
fi->seek(pe_offset + ihsize, SEEK_SET);
|
||||||
fi->readx(isection,sizeof(pe_section_t)*objs);
|
fi->readx(isection,sizeof(pe_section_t)*objs);
|
||||||
if (ih_objects < 3)
|
if (ih_objects < 3)
|
||||||
@@ -3070,15 +3081,8 @@ upx_uint64_t PeFile::ilinkerGetAddress(const char *d, const char *n) const
|
|||||||
|
|
||||||
PeFile::~PeFile()
|
PeFile::~PeFile()
|
||||||
{
|
{
|
||||||
delete [] isection;
|
|
||||||
delete [] orelocs;
|
|
||||||
delete [] oimport;
|
|
||||||
oimpdlls = NULL;
|
oimpdlls = NULL;
|
||||||
delete [] oexport;
|
|
||||||
delete [] otls;
|
|
||||||
delete [] oresources;
|
|
||||||
delete [] oxrelocs;
|
delete [] oxrelocs;
|
||||||
delete [] oloadconf;
|
|
||||||
delete ilinker;
|
delete ilinker;
|
||||||
//delete res;
|
//delete res;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,6 +103,7 @@ protected:
|
|||||||
ord_mask_t ord_mask, bool set_oft);
|
ord_mask_t ord_mask, bool set_oft);
|
||||||
virtual unsigned processImports() = 0;
|
virtual unsigned processImports() = 0;
|
||||||
virtual void processImports2(unsigned, unsigned);
|
virtual void processImports2(unsigned, unsigned);
|
||||||
|
MemBuffer mb_oimport;
|
||||||
upx_byte *oimport;
|
upx_byte *oimport;
|
||||||
unsigned soimport;
|
unsigned soimport;
|
||||||
upx_byte *oimpdlls;
|
upx_byte *oimpdlls;
|
||||||
@@ -117,6 +118,7 @@ protected:
|
|||||||
void processRelocs(Reloc *);
|
void processRelocs(Reloc *);
|
||||||
void rebuildRelocs(upx_byte *&, unsigned bits,
|
void rebuildRelocs(upx_byte *&, unsigned bits,
|
||||||
unsigned flags, upx_uint64_t imagebase);
|
unsigned flags, upx_uint64_t imagebase);
|
||||||
|
MemBuffer mb_orelocs;
|
||||||
upx_byte *orelocs;
|
upx_byte *orelocs;
|
||||||
unsigned sorelocs;
|
unsigned sorelocs;
|
||||||
upx_byte *oxrelocs;
|
upx_byte *oxrelocs;
|
||||||
@@ -125,12 +127,14 @@ protected:
|
|||||||
void processExports(Export *);
|
void processExports(Export *);
|
||||||
void processExports(Export *,unsigned);
|
void processExports(Export *,unsigned);
|
||||||
void rebuildExports();
|
void rebuildExports();
|
||||||
|
MemBuffer mb_oexport;
|
||||||
upx_byte *oexport;
|
upx_byte *oexport;
|
||||||
unsigned soexport;
|
unsigned soexport;
|
||||||
|
|
||||||
void processResources(Resource *);
|
void processResources(Resource *);
|
||||||
void processResources(Resource *, unsigned);
|
void processResources(Resource *, unsigned);
|
||||||
void rebuildResources(upx_byte *&, unsigned);
|
void rebuildResources(upx_byte *&, unsigned);
|
||||||
|
MemBuffer mb_oresources;
|
||||||
upx_byte *oresources;
|
upx_byte *oresources;
|
||||||
unsigned soresources;
|
unsigned soresources;
|
||||||
|
|
||||||
@@ -147,6 +151,7 @@ protected:
|
|||||||
virtual void processTls(Reloc *r, const Interval *iv, unsigned a) = 0;
|
virtual void processTls(Reloc *r, const Interval *iv, unsigned a) = 0;
|
||||||
|
|
||||||
void rebuildTls();
|
void rebuildTls();
|
||||||
|
MemBuffer mb_otls;
|
||||||
upx_byte *otls;
|
upx_byte *otls;
|
||||||
unsigned sotls;
|
unsigned sotls;
|
||||||
unsigned tlsindex;
|
unsigned tlsindex;
|
||||||
@@ -156,6 +161,7 @@ protected:
|
|||||||
|
|
||||||
void processLoadConf(Reloc *, const Interval *, unsigned);
|
void processLoadConf(Reloc *, const Interval *, unsigned);
|
||||||
void processLoadConf(Interval *);
|
void processLoadConf(Interval *);
|
||||||
|
MemBuffer mb_oloadconf;
|
||||||
upx_byte *oloadconf;
|
upx_byte *oloadconf;
|
||||||
unsigned soloadconf;
|
unsigned soloadconf;
|
||||||
|
|
||||||
@@ -200,6 +206,7 @@ protected:
|
|||||||
LE32 flags;
|
LE32 flags;
|
||||||
__packed_struct_end()
|
__packed_struct_end()
|
||||||
|
|
||||||
|
MemBuffer mb_isection;
|
||||||
pe_section_t *isection;
|
pe_section_t *isection;
|
||||||
bool isdll;
|
bool isdll;
|
||||||
bool isrtm;
|
bool isrtm;
|
||||||
|
|||||||
Reference in New Issue
Block a user