Skip to content

Commit

Permalink
ntsysv: fix leaks
Browse files Browse the repository at this point in the history
Error: RESOURCE_LEAK (CWE-772): [#def1] [important]
chkconfig-1.26/ntsysv.c:316:5: alloc_fn: Storage is returned from allocation function "malloc".
chkconfig-1.26/ntsysv.c:316:5: var_assign: Assigning: "services" = storage returned from "malloc(88UL * numServicesAlloced)".
chkconfig-1.26/ntsysv.c:321:9: leaked_storage: Variable "services" going out of scope leaks the storage it points to.
  319|           fprintf(stderr, "failed to open " RUNLEVELS "/init.d: %s\n",
  320|                   strerror(errno));
  321|->         return 2;
  322|       }
  323|

Error: RESOURCE_LEAK (CWE-772): [#def2] [important]
chkconfig-1.26/ntsysv.c:316:5: alloc_fn: Storage is returned from allocation function "malloc".
chkconfig-1.26/ntsysv.c:316:5: var_assign: Assigning: "services" = storage returned from "malloc(88UL * numServicesAlloced)".
chkconfig-1.26/ntsysv.c:346:9: noescape: Resource "services + numServices" is not freed or pointed-to in "readServiceInfo".
chkconfig-1.26/ntsysv.c:375:13: leaked_storage: Variable "services" going out of scope leaks the storage it points to.
  373|               fprintf(stderr, "failed to open " XINETDDIR ": %s\n",
  374|                       strerror(errno));
  375|->             return 2;
  376|           }
  377|

Error: RESOURCE_LEAK (CWE-772): [#def3] [important]
chkconfig-1.26/ntsysv.c:393:17: alloc_fn: Storage is returned from allocation function "realloc".
chkconfig-1.26/ntsysv.c:393:17: var_assign: Assigning: "services" = storage returned from "realloc(services, numServicesAlloced * 88UL)".
chkconfig-1.26/ntsysv.c:397:13: noescape: Resource "services + numServices" is not freed or pointed-to in "readXinetdServiceInfo".
chkconfig-1.26/ntsysv.c:403:17: leaked_storage: Variable "services" going out of scope leaks the storage it points to.
  401|                           ent->d_name, strerror(errno));
  402|                   closedir(dir);
  403|->                 return 2;
  404|               } else if (!rc)
  405|                   numServices++;

Error: RESOURCE_LEAK (CWE-772): [#def4] [important]
chkconfig-1.26/ntsysv.c:372:9: alloc_fn: Storage is returned from allocation function "opendir".
chkconfig-1.26/ntsysv.c:372:9: var_assign: Assigning: "dir" = storage returned from "opendir("/etc/xinetd.d")".
chkconfig-1.26/ntsysv.c:378:9: noescape: Resource "dir" is not freed or pointed-to in "readdir".
chkconfig-1.26/ntsysv.c:422:5: leaked_storage: Variable "dir" going out of scope leaks the storage it points to.
  420|       *numServicesPtr = numServices;
  421|
  422|->     return 0;
  423|   }
  424|

Error: CPPCHECK_WARNING (CWE-401): [#def5] [important]
chkconfig-1.26/ntsysv.c:321: error[memleak]: Memory leak: services
  319|           fprintf(stderr, "failed to open " RUNLEVELS "/init.d: %s\n",
  320|                   strerror(errno));
  321|->         return 2;
  322|       }
  323|
  • Loading branch information
lnykryn committed May 13, 2024
1 parent 386c32c commit 3edcf07
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ntsysv.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ static int getServices(struct service **servicesPtr, int *numServicesPtr,
if (!(dir = opendir(RUNLEVELS "/init.d"))) {
fprintf(stderr, "failed to open " RUNLEVELS "/init.d: %s\n",
strerror(errno));
free(services);
return 2;
}

Expand Down Expand Up @@ -372,6 +373,7 @@ static int getServices(struct service **servicesPtr, int *numServicesPtr,
if (!(dir = opendir(XINETDDIR))) {
fprintf(stderr, "failed to open " XINETDDIR ": %s\n",
strerror(errno));
freeServices(services, numServices);
return 2;
}

Expand Down Expand Up @@ -400,6 +402,7 @@ static int getServices(struct service **servicesPtr, int *numServicesPtr,
fprintf(stderr, _("error reading info for service %s: %s\n"),
ent->d_name, strerror(errno));
closedir(dir);
freeServices(services, numServices);
return 2;
} else if (!rc)
numServices++;
Expand All @@ -408,8 +411,11 @@ static int getServices(struct service **servicesPtr, int *numServicesPtr,
if (err) {
fprintf(stderr, _("error reading from directory %s: %s\n"),
XINETDDIR, strerror(err));
freeServices(services, numServices);
return 1;
}

closedir(dir);
}

getSystemdServices(&services, &numServices);
Expand Down

0 comments on commit 3edcf07

Please sign in to comment.