Skip to content

Commit 7ec4302

Browse files
authored
Fix segfault in ExportWaveAsCode (#3769)
`char *txtData = (char *)RL_CALLOC(waveDataSize * 6 + 2000, sizeof(char));` assumes every chunk being added to txtData is 6 bytes. This is not always true, sometimes a newline is involved and the data becomes 12 bytes instead, and this can cause a random segfault. This commit changes `6` to `12`, and explains why in the comment.
1 parent c0b081f commit 7ec4302

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/raudio.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -1081,8 +1081,9 @@ bool ExportWaveAsCode(Wave wave, const char *fileName)
10811081
int waveDataSize = wave.frameCount*wave.channels*wave.sampleSize/8;
10821082

10831083
// NOTE: Text data buffer size is estimated considering wave data size in bytes
1084-
// and requiring 6 char bytes for every byte: "0x00, "
1085-
char *txtData = (char *)RL_CALLOC(waveDataSize*6 + 2000, sizeof(char));
1084+
// and requiring 12 char bytes for every byte; the actual size varies, but
1085+
// the longest possible char being appended is "%.4ff,\n ", which is 12 bytes.
1086+
char *txtData = (char *)RL_CALLOC(waveDataSize*12 + 2000, sizeof(char));
10861087

10871088
int byteCount = 0;
10881089
byteCount += sprintf(txtData + byteCount, "\n//////////////////////////////////////////////////////////////////////////////////\n");

0 commit comments

Comments
 (0)