Skip to content

Commit

Permalink
ntsysv: fix leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
lnykryn committed May 7, 2024
1 parent cef1f90 commit d77b88e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
4 changes: 2 additions & 2 deletions chkconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static int delService(char *name, int type, int level) {

finish:
freeService(s);
freeServices(&services, numservs);
freeServices(services, numservs);
return rc;
}

Expand Down Expand Up @@ -331,7 +331,7 @@ static int frobDependencies(struct service *s) {
if (frobOneDependencies(s, servs, numservs, 1, LSB) == -1)
r=1;

freeServices(&servs, numservs);
freeServices(servs, numservs);
return r;
}

Expand Down
11 changes: 3 additions & 8 deletions leveldb.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,10 @@ void freeService(struct service s) {
free(s.provides);
}

void freepService(struct service **s) {
freeService(**s);
free(*s);
}

void freeServices(struct service **s, int n) {
void freeServices(struct service *s, int n) {
for (int i = 0; i < n; i++)
freepService(&s[i]);
free(*s);
freeService(s[i]);
free(s);
}

int readServices(struct service **services) {
Expand Down
3 changes: 1 addition & 2 deletions leveldb.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ struct service {
int parseLevels(char *str, int emptyOk);

void freeService(struct service s);
void freepService(struct service **s);
void freeServices(struct service **s, int n);
void freeServices(struct service *s, int n);
/* returns 0 on success, 1 if the service is not chkconfig-able, -1 if an
I/O error occurs (in which case errno can be checked) */
int readServiceInfo(char *name, int type, struct service *service,
Expand Down
15 changes: 12 additions & 3 deletions ntsysv.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,12 @@ static int getServices(struct service **servicesPtr, int *numServicesPtr,
int systemd = systemdActive();

numServicesAlloced = 10;
services = malloc(sizeof(*services) * numServicesAlloced);
services = malloc(sizeof(struct service) * numServicesAlloced);

if (!(dir = opendir(RUNLEVELS "/init.d"))) {
fprintf(stderr, "failed to open " RUNLEVELS "/init.d: %s\n",
strerror(errno));
free(services);
return 2;
}

Expand All @@ -340,7 +341,9 @@ static int getServices(struct service **servicesPtr, int *numServicesPtr,
if (numServices == numServicesAlloced) {
numServicesAlloced += 10;
services =
realloc(services, numServicesAlloced * sizeof(*services));
realloc(services, numServicesAlloced * sizeof(struct service));
memset(services + numServices, 0,
(numServicesAlloced - numServices) * sizeof(struct service));
}

rc = readServiceInfo(ent->d_name, TYPE_INIT_D, services + numServices,
Expand Down Expand Up @@ -372,6 +375,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, numServicesAlloced);
return 2;
}

Expand All @@ -391,7 +395,9 @@ static int getServices(struct service **servicesPtr, int *numServicesPtr,
if (numServices == numServicesAlloced) {
numServicesAlloced += 10;
services =
realloc(services, numServicesAlloced * sizeof(*services));
realloc(services, numServicesAlloced * sizeof(struct service));
memset(services + numServices, 0,
(numServicesAlloced - numServices) * sizeof(struct service));
}

rc = readXinetdServiceInfo(ent->d_name, services + numServices);
Expand All @@ -400,11 +406,14 @@ 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, numServicesAlloced);
return 2;
} else if (!rc)
numServices++;
}

closedir(dir);

if (err) {
fprintf(stderr, _("error reading from directory %s: %s\n"),
XINETDDIR, strerror(err));
Expand Down

0 comments on commit d77b88e

Please sign in to comment.