We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4501

Summary: Segfault when passing NULL as extension to Sound_NewSample
Product: SDL_sound Reporter: SuperZazu <nico.g.allemand>
Component: everythingAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Ryan C. Gordon <icculus>
Severity: normal    
Priority: P2 CC: sezeroz
Version: unspecified   
Hardware: x86   
OS: macOS 10.13   

Description SuperZazu 2019-02-09 11:09:13 UTC
Hello!

I just noticed a segfault when passing NULL to Sound_NewSample. Note that it does not happen when passing "".

To reproduce the bug quickly, you can modify "playsound_simple.c" like this:

```
diff -r 4c3740db6ea6 playsound/playsound_simple.c
--- a/playsound/playsound_simple.c      Sat Feb 09 11:38:38 2019 +0100
+++ b/playsound/playsound_simple.c      Sat Feb 09 12:07:31 2019 +0100
@@ -99,7 +99,9 @@
     PlaysoundAudioCallbackData data;
 
     SDL_zero(data);
-    data.sample = Sound_NewSampleFromFile(fname, NULL, 65536);
+    // segfault does happen when ext == NULL; and does not happen when ext == ""
+    const char* ext = NULL;
+    data.sample = Sound_NewSample(SDL_RWFromFile(fname, "rb"), ext, NULL, 65536);
     if (data.sample == NULL)
     {
         fprintf(stderr, "Couldn't load '%s': %s.\n", fname, Sound_GetError());
```

Here is a patch that fixes the problem:

```
diff -r 9262f9205898 -r 4c3740db6ea6 src/SDL_sound.c
--- a/src/SDL_sound.c   Fri Jul 20 14:59:10 2018 -0400
+++ b/src/SDL_sound.c   Sat Feb 09 11:38:38 2019 +0100
@@ -506,6 +506,9 @@
         } /* for */
     } /* if */
 
+    if (ext == NULL)
+        ext = "";
+
     /* no direct extension match? Try everything we've got... */
     for (decoder = &decoders[0]; decoder->funcs != NULL; decoder++)
     {
```

As a side note, is there any simpler way to propose patches directly to the mercurial repository?

Thank you for your time
Comment 1 Ozkan Sezer 2019-10-19 00:22:37 UTC
I fixed it differently here:
https://github.com/sezero/SDL_sound/commit/3e7c880b27360d3bc87fd3560f173b5651aad3ce
Comment 2 SuperZazu 2019-10-19 20:47:39 UTC
Indeed, I noticed that your repo included a fix for this after creating my post, thanks for your work!
Comment 3 Ozkan Sezer 2020-12-05 18:13:26 UTC
The issue is fixed in mainstream as of 
https://hg.icculus.org/icculus/SDL_sound/rev/5f2d5eeb97e6

Closing.