Skip to content

Commit 9debcee

Browse files
authored
fix(compress): Fix a few memory leaks on fail conditions in Compression code (TheSuperHackers#1509)
1 parent 78a9efa commit 9debcee

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

Core/Libraries/Source/Compression/EAC/btreeencode.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,18 @@ static void BTREE_treepack(struct BTreeEncodeContext *EC,
367367

368368
EC->buf1 = (unsigned char *) galloc(buf1size);
369369
if (!EC->buf1)
370-
return; /* failure Insufficient memory for work buffer */
370+
{
371+
gfree(treebuf);
372+
return; /* failure Insufficient memory for work buffer */
373+
}
371374

372375
EC->buf2 = (unsigned char *) galloc(buf2size);
373376
if (!EC->buf2)
374-
return; /* failure Insufficient memory for work buffer */
377+
{
378+
gfree(treebuf);
379+
gfree(EC->buf1);
380+
return; /* failure Insufficient memory for work buffer */
381+
}
375382

376383
memcpy(EC->buf1, EC->buffer, EC->ulen); /* copy to scratch buffer */
377384

Core/Libraries/Source/Compression/EAC/refencode.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,10 @@ static int refcompress(unsigned char *from, int len, unsigned char *dest, int ma
7979
return(0);
8080
link = (int *) galloc(131072L*sizeof(int));
8181
if (!link)
82-
return(0);
82+
{
83+
gfree(hashtbl);
84+
return(0);
85+
}
8386

8487
memset(hashtbl,-1,65536L*sizeof(int));
8588

Core/Libraries/Source/Compression/LZHCompress/NoxCompress.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ Bool DecompressFile (char *infile, char *outfile)
6969
outBlock= (char *) DbgMalloc( rawSize );
7070

7171
if (( inBlock == NULL ) || ( outBlock == NULL ))
72+
{
73+
if (inBlock) DbgFree(inBlock);
74+
if (outBlock) DbgFree(outBlock);
7275
return FALSE;
76+
}
7377

7478
// Read in a big chunk o file
7579
NoxRead(inBlock, 1, compressedSize, inFilePtr);
@@ -97,21 +101,19 @@ Bool DecompressFile (char *infile, char *outfile)
97101

98102
DEBUG_LOG(("Decompressed %s to %s, output size = %d", infile, outfile, rawSize));
99103

104+
Bool success = FALSE;
100105
LZHLDestroyDecompressor(decompress);
101106
outFilePtr = fopen(outfile, "wb");
102107
if (outFilePtr)
103108
{
104109
fwrite (outBlock, rawSize, 1, outFilePtr);
105110
fclose(outFilePtr);
111+
success = TRUE;
106112
}
107-
else
108-
return FALSE;
109113

110-
// Clean up this mess
111114
DbgFree(inBlock);
112115
DbgFree(outBlock);
113-
return TRUE;
114-
116+
return success;
115117
} // End of if fileptr
116118

117119
return FALSE;
@@ -148,7 +150,11 @@ Bool CompressFile (char *infile, char *outfile)
148150
outBlock= (char *) DbgMalloc( LZHLCompressorCalcMaxBuf( rawSize ));
149151

150152
if (( inBlock == NULL ) || ( outBlock == NULL ))
153+
{
154+
DbgFree(inBlock);
155+
DbgFree(outBlock);
151156
return FALSE;
157+
}
152158

153159
// Read in a big chunk o file
154160
NoxRead(inBlock, 1, rawSize, inFilePtr);
@@ -164,23 +170,21 @@ Bool CompressFile (char *infile, char *outfile)
164170
compressedSize += compressed;
165171
}
166172

173+
Bool success = FALSE;
167174
LZHLDestroyCompressor(compressor);
168-
169175
outFilePtr = fopen(outfile, "wb");
170176
if (outFilePtr)
171177
{
172178
// write out the uncompressed size first.
173179
fwrite(&rawSize, sizeof(UnsignedInt), 1, outFilePtr);
174180
fwrite(outBlock, compressedSize, 1, outFilePtr);
175181
fclose(outFilePtr);
182+
success = TRUE;
176183
}
177-
else
178-
return FALSE;
179184

180-
// Clean up
181185
DbgFree(inBlock);
182186
DbgFree(outBlock);
183-
return TRUE;
187+
return success;
184188
}
185189

186190
return FALSE;

0 commit comments

Comments
 (0)