Skip to content

Commit 48b70bb

Browse files
committed
Fix NULL pointer dereference in S3CalculateRandomizedFields (#284)
If the sound has been muted (!gS3_enabled) during game data loading, it is then possible to unmute it during gameplay and request a sound effect which doesn't have a sample loaded (most prominent for pratcam sfx). While logic in `S3StartSound` accommodates for such a case by loading the missing sample, it first calls `S3CalculateRandomizedFields`, which triggers a NULL pointer dereference on platforms with memory protection. This bug is most likely an overlook from the DOS era. Fix this by checking for NULL pointer before use. Signed-off-by: Artur Rojek <contact@artur-rojek.eu>
1 parent 41d45f9 commit 48b70bb

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/S3/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ else()
1818
/wd4996
1919
)
2020
endif()
21+
if(DETHRACE_FIX_BUGS)
22+
target_compile_definitions(s3 PRIVATE DETHRACE_FIX_BUGS)
23+
endif()
2124

2225
if(IS_BIGENDIAN)
2326
target_compile_definitions(s3 PRIVATE BR_ENDIAN_BIG=1)

src/S3/audio.c

+7
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,13 @@ void S3CalculateRandomizedFields(tS3_channel* chan, tS3_descriptor* desc) {
962962
chan->left_volume = vol;
963963
chan->right_volume = vol;
964964
if (desc->type == eS3_ST_sample) {
965+
#if defined(DETHRACE_FIX_BUGS)
966+
/* Avoid a possible NULL pointer dereference. */
967+
if (desc->sound_data == NULL) {
968+
chan->rate = desc->min_pitch;
969+
return;
970+
}
971+
#endif
965972
chan->rate = S3IRandomBetweenLog(desc->min_pitch, desc->max_pitch, ((tS3_sample*)desc->sound_data)->rate);
966973
}
967974
}

0 commit comments

Comments
 (0)