Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NULL pointer dereference in S3StartSound (#284) #285

Merged
merged 1 commit into from
Feb 22, 2023

Conversation

zear
Copy link
Collaborator

@zear zear commented Feb 10, 2023

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 the code accommodates for such a case, it does it too late, triggering a NULL pointer dereference on platforms which feature memory protection. This bug is most likely an overlook from the DOS era.

Fix this by moving the sample NULL pointer check before its first use.

Signed-off-by: Artur Rojek contact@artur-rojek.eu

src/S3/audio.c Outdated
@@ -793,6 +793,18 @@ tS3_sound_tag S3StartSound(tS3_outlet* pOutlet, tS3_sound_id pSound) {
gS3_last_error = eS3_error_bad_id;
return 0;
}
#if defined(S3_FIX_BUGS)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use DETHRACE_FIX_BUGS here - its probably unlikely someone would want to fix bugs in one area but not the other

src/S3/audio.c Outdated
@@ -793,6 +793,18 @@ tS3_sound_tag S3StartSound(tS3_outlet* pOutlet, tS3_sound_id pSound) {
gS3_last_error = eS3_error_bad_id;
return 0;
}
#if defined(S3_FIX_BUGS)
/*
* Load the sample early to prevent a possible NULL pointer dereference in
Copy link
Owner

@dethrace-labs dethrace-labs Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice find! I wonder if a smaller fix could be done inside S3CalculateRandomizedFields itself? Something like

#if FIX_BUGS
if (desc->sound_data == NULL) {
  chan->rate = desc->min_pitch;
  return;
}
#endif
chan->rate = S3IRandomBetweenLog(...)

?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. And I can't even hear a difference between chan->rate = desc->min_pitch; and chan->rate = S3IRandomBetweenLog(...); in practice :)

@zear zear force-pushed the fix_pratcam_sfx branch 2 times, most recently from b104036 to ecb6248 Compare February 19, 2023 13:15
…-labs#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>
Copy link
Collaborator

@madebr madebr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can reproduce the crash in OG and current dethrace.

Just a little question. Would always loading the samples, but not playing them, when sound is disabled be much harder to implement?

@@ -18,6 +18,9 @@ else()
/wd4996
)
endif()
if(DETHRACE_FIX_BUGS)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in this bug report, but I think we should combine BRENDER_FIX_BUGS and DETHRACE_FIX_BUGS in one cmake option + compile definition.

Copy link
Owner

@dethrace-labs dethrace-labs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dethrace-labs
Copy link
Owner

@zear I've invited you as a collaborator to the project. If you accept, you will be able to merge your own approved PRs when you are ready. And thanks!

@madebr
Copy link
Collaborator

madebr commented Feb 20, 2023

Welcome to the 🚗 club @zear !

@zear zear merged commit 48b70bb into dethrace-labs:main Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants