Skip to content

Commit ca66664

Browse files
authored
Merge pull request #3327 from Shivansps/over_alloc_fix
fix over alloc to store lz4 offsets
2 parents 4c1b355 + cd2b578 commit ca66664

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

code/cfile/cfilecompression.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ void lz41_create_ci(CFILE* cf, int header)
121121
auto fSize = fread(&cf->size, sizeof(int), 1, cf->fp);
122122
auto fBsize = fread(&cf->compression_info.block_size, sizeof(int), 1, cf->fp);
123123

124-
#if !defined(NDEBUG)
125124
Assertion(cf->compression_info.num_offsets > 0, "Invalid number of offsets, compressed file is possibly in the wrong format or corrupted.");
125+
#if !defined(NDEBUG)
126126
Assertion(cf->size > 4, "Invalid filesize, compressed file is possibly in the wrong format or corrupted.");
127127
Assertion(cf->compression_info.block_size > 16, "Invalid block size, compressed file is possibly in the wrong format or corrupted.");
128128
Assertion(fNumoffsets == 1, "Error while reading the number of offsets, compressed file is possibly in the wrong format or corrupted.");
@@ -138,16 +138,13 @@ void lz41_create_ci(CFILE* cf, int header)
138138

139139
void lz41_load_offsets(CFILE* cf)
140140
{
141-
int max_blocks = (int)cf->size + 8 / cf->compression_info.block_size;
142-
cf->compression_info.offsets = (int*)malloc(max_blocks);
141+
cf->compression_info.offsets = (int*)malloc(cf->compression_info.num_offsets * sizeof(int));
143142
int block;
144143
int* offsets_ptr = cf->compression_info.offsets;
145144

146-
Assertion(cf->compression_info.num_offsets * (int)sizeof(int) <= max_blocks, "The number of offsets cant be higher than max possible blocks(size/blocksize), compressed file is possibly in the wrong format or corrupted.");
147-
148145
/* Seek to the first offset position, remember to consider the trailing ints */
149146
fso_fseek(cf, ( ( sizeof(int) * cf->compression_info.num_offsets ) * -1 ) - (sizeof(int)*3 ), SEEK_END);
150-
for (block = 0; block <= cf->compression_info.num_offsets; ++block)
147+
for (block = 0; block < cf->compression_info.num_offsets; ++block)
151148
{
152149
auto bytes_read = fread(offsets_ptr++, sizeof(int), 1, cf->fp);
153150
Assertion(bytes_read == 1, "Error reading offset list.");

0 commit comments

Comments
 (0)