neurobin / shc

Shell script compiler
https://neurobin.org/projects/softwares/unix/shc/
GNU General Public License v3.0
2.04k stars 348 forks source link

Fix memory leaks in write_c and eval_shell #165

Open ashamedbit opened 10 months ago

ashamedbit commented 10 months ago

Hi, I am a new contributor looking forward to contribute to the shc project :). I came across the following open issue here #155 It is a leaksanitizer report which detects a leak that happens on passing a simple bash script. I was able to reproduce the bug on running a custom shell script and was able to generate a patch for it. The leak happens within function write_c. Basically all the allocated variables in this function including msg1, msg2, chk1, chk2, tst1, tst2 and name need to be freed after allocation. I have a model patch and I was wondering if I can merge the same.

I was able to verify that after the patch, the leak no longer occurs through leaksanitizer (and there is no double free for that matter)

In addition I have also patched a leak that I encountered when running on an invalid bash script. This new leak happens in eval_shell function when an invalid first line on script is encountered. Here ptr is allocated but not freed before the abrupt exit.

Let me know if this is a good patch! If not let me know what changes need be done. I am eager to become a regular contributor to this repository :)

suntong commented 3 months ago

LGTM, @neurobin?