htop-dev / htop

htop - an interactive process viewer

Home Page:https://htop.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Memory leak at Settings.c:867 (legacyDotfile)

LuMingYinDetect opened this issue · comments

The detailed path of the defect:

1.At line 796 in /htop/Settings.c, a pointer variable named legacyDotfile is defined. This pointer variable allocates a dynamic memory area through the function String_cat at line 818, as shown in the figure below:
https://github.com/LuMingYinDetect/htop_defects/blob/main/htop_1.png

2.The function String_cat allocates a new dynamic memory area and returns it. At line 130 of String_cat, a pointer variable named out is defined and a dynamic memory area is allocated through the function xMalloc. This pointer out is returned at line 134, as shown in the figure below:
https://github.com/LuMingYinDetect/htop_defects/blob/main/htop_2.png

3.After the execution of String_cat, if the if statement at line 825 returns false, the program will not execute the release operation of the dynamic memory area pointed to by legacyDotfile at line 826. Similarly, if the if statement at line 842 also returns false, the program will not execute the release operation of the dynamic memory area pointed to by legacyDotfile at line 850. In this scenario, the program will return at line 867. During this process, the dynamic memory area pointed to by legacyDotfile remains unreleased, thus resulting in a memory leak defect, as illustrated in the figure below:
https://github.com/LuMingYinDetect/htop_defects/blob/main/htop_3.png

Linking the proper places in the code (note the commit hash in the links):

  1. htop/Settings.c

    Line 796 in 8122fc3

    char* legacyDotfile = NULL;
  2. htop/Settings.c

    Line 818 in 8122fc3

    legacyDotfile = String_cat(home, "/.htoprc");
  3. htop/Settings.c

    Line 850 in 8122fc3

    free(legacyDotfile);

Usually works best to reference specific code locations as images are kinda unwieldy for that purpose.