From a91fc41ad25047900b3e987f1eac2da4145cbe38 Mon Sep 17 00:00:00 2001 From: Bill Nottingham Date: Thu, 31 Mar 2005 21:43:17 +0000 Subject: free some of the more egregious memory leaks (#85935) --- src/initlog.c | 24 +++++++++++++++++------- src/process.c | 48 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/initlog.c b/src/initlog.c index 23f5c508..e9b8e265 100644 --- a/src/initlog.c +++ b/src/initlog.c @@ -40,7 +40,7 @@ struct logInfo *logData = NULL; void readConfiguration(char *fname) { int fd,num=0; struct stat sbuf; - char *data,*line; + char *data,*line, *d; regex_t *regexp; int lfac=-1,lpri=-1; @@ -49,7 +49,7 @@ void readConfiguration(char *fname) { close(fd); return; } - data=malloc(sbuf.st_size+1); + d = data=malloc(sbuf.st_size+1); if (read(fd,data,sbuf.st_size)!=sbuf.st_size) { close(fd); return; @@ -98,6 +98,7 @@ void readConfiguration(char *fname) { } if (lfac!=-1) logfacility=lfac; if (lpri!=-1) logpriority=lpri; + free(d); } char *getLine(char **data) { @@ -267,7 +268,7 @@ int logEvent(char *cmd, int eventtype,char *string) { /* insert more here */ NULL }; - int x=0,len; + int x=0,len, rc; struct logInfo logentry; if (cmd) { @@ -278,8 +279,10 @@ int logEvent(char *cmd, int eventtype,char *string) { logentry.cmd+=3; } else logentry.cmd = strdup(_("(none)")); - if (!string) - string = strdup(cmd); + if (!string) { + string = alloca(strlen(cmd)+1); + strcpy(string,cmd); + } while (eventtable[x] && x #include @@ -22,7 +33,7 @@ extern regex_t **regList; int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) { /* Fork command 'cmd', returning pid, and optionally pointer * to open file descriptor fd */ - int fdout, fderr, fdcmd, pid; + int fdout=-1, fderr=-1, fdcmd=-1, pid; int outpipe[2], errpipe[2], fdpipe[2]; int ourpid; @@ -52,6 +63,8 @@ int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) { } else { fdcmd = open("/dev/null",O_WRONLY); } + if (fdout==-1 || fderr==-1 || fdcmd==-1) + return -1; ourpid = getpid(); if ((pid = fork())==-1) { perror("fork"); @@ -128,7 +141,6 @@ int forkCommand(char **args, int *outfd, int *errfd, int *cmdfd, int quiet) { int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, int debug) { struct pollfd *pfds; - char *buf=malloc(8192*sizeof(char)); char *outbuf=NULL; char *tmpstr=NULL; int x,y,rc=-1; @@ -154,6 +166,9 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, usleep(500); if (((x=poll(pfds,numfds,500))==-1)&&errno!=EINTR) { perror("poll"); + free(pfds); + if (procpath) + free(procpath); return -1; } if (!reexec) { @@ -165,16 +180,23 @@ int monitor(char *cmdname, int pid, int numfds, int *fds, int reexec, int quiet, if (stat(procpath,&sbuf)&&!stat("/proc/cpuinfo",&sbuf)) done=1; } + if (x<0) + continue; y=0; while (y