Skip to content

Commit 21dbc4e

Browse files
committed
thcrap_tasofro: fill the overallocated part of rep_buffer with zeroes
When we replace a file with a smaller one, we overallocate our rep buffer because we always give the the game the biggest of the 2 sizes. But we used to only fill the useful part of the rep buffer with the rep file, and keep the overallocated part as garbage, which then ends up in the game's internal buffer. This is fine for a bunch of file types that have clear end markers, like png where the header contains the file size or compiled nut scripts which end with a little endian TAIL tag, but it broke things when replacing plaintext nut scripts because the nut compiler tried to interpred the garbage at the end as code.
1 parent c2fe3c3 commit 21dbc4e

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

thcrap_tasofro/src/tasofro_file.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ size_t TasofroFile::init_game_file_size(size_t game_file_size)
5050
if (game_file_size > this->pre_json_size) {
5151
// The original file is bigger than our replacement file,
5252
// we might need a bigger buffer.
53+
size_t previous_size = this->pre_json_size;
5354
this->pre_json_size = game_file_size;
5455
if (this->rep_buffer) {
5556
this->rep_buffer = realloc(this->rep_buffer, POST_JSON_SIZE(this));
57+
memset((char*)this->rep_buffer + previous_size, 0, POST_JSON_SIZE(this) - previous_size);
5658
}
5759
}
5860
return POST_JSON_SIZE(this);

0 commit comments

Comments
 (0)