sheredom / subprocess.h

🐜 single header process launching solution for C and C++
The Unlicense
1.1k stars 97 forks source link

Resource leak when `subprocess_create_ex()` fails #85

Open wn2000 opened 4 months ago

wn2000 commented 4 months ago

The subprocess_create_ex() function just has return -1 in case of an error, without properly closing already open file descriptors.

Example: https://github.com/sheredom/subprocess.h/blob/7e59b6924aa0e1a595be86fdeb1d4c28835aa0b9/subprocess.h#L876-L880

In my test, trying to call this function with a non-existing command results in 6 more new FDs each time.

starseeker commented 3 months ago

The following is helpful (at least on Linux) for debugging this (see https://developers.redhat.com/articles/2023/01/09/how-use-valgrind-track-file-descriptors#valgrind___track_fds_yes):

valgrind -q --tool=none --track-fds=yes

starseeker commented 3 months ago

(Note - found out the hard way if you forget to call subprocess_destroy, fd leaks - even in normal usage - are one of the symptoms)

sheredom commented 3 months ago

Thanks for the report. Super busy at the moment but when I get the time I'll take a look!