schellingb / TinySoundFont

SoundFont2 synthesizer library in a single C/C++ file
MIT License
608 stars 71 forks source link

Likely memory leak in use of TSF_REALLOC in new sf3 code in out of memory conditions #77

Closed ell1e closed 10 months ago

ell1e commented 1 year ago

These look like potential memory leaks to me:

https://github.com/schellingb/TinySoundFont/blob/d21fe7b22c8c906e749e4be4bb389efc2e761964/tsf.h#L908

https://github.com/schellingb/TinySoundFont/blob/d21fe7b22c8c906e749e4be4bb389efc2e761964/tsf.h#L918

...in case the allocation fails. The old res pointer will leak then, I think the right way would be to never realloc() to the same pointer variable in one line and in case of failure, make sure the old one is free()'d if it's no longer used (as is the case here). My bad if I'm mistaken, I didn't actually confirm a memory leak via valgrind or something.

schellingb commented 10 months ago

Fixed this one, too! There certainly was a potential for a memory leak with the previous code. As always, thank you so much for reporting this!