psmisc/psmisc-22.7-memleaks.patch

225 lines
6.8 KiB
Diff

--- src/fuser.c
+++ src/fuser.c 2009-05-11 13:55:56.314402075 +0200
@@ -225,6 +225,9 @@ static void scan_procs(const opt_type op
if (islocatedon(&cwd_real[0], ino_tmp->name->filename))
add_matched_proc(ino_tmp->name, pid, uid, ACCESS_CWD);
}
+ if (root_stat) free(root_stat);
+ if (cwd_stat) free(cwd_stat);
+ if (exe_stat) free(exe_stat);
#ifndef __linux__
check_dir(pid, "lib", dev_head, ino_head, uid, ACCESS_MMAP, sockets, netdev);
check_dir(pid, "mmap", dev_head, ino_head, uid, ACCESS_MMAP, sockets, netdev);
@@ -239,10 +242,9 @@ static void add_inode(struct inode_list
{
struct inode_list *ino_tmp, *ino_head;
- ino_head = *ino_list;
-
- if ( (ino_tmp = malloc(sizeof(struct inode_list))) == NULL)
+ if ( (ino_tmp = (struct inode_list*)malloc(sizeof(struct inode_list))) == NULL)
return;
+ ino_head = *ino_list;
ino_tmp->name = this_name;
ino_tmp->device = device;
ino_tmp->inode = inode;
@@ -250,15 +252,15 @@ static void add_inode(struct inode_list
*ino_list = ino_tmp;
}
-static void add_device(struct device_list **dev_list, struct names *this_name, dev_t device)
+static void add_device(struct device_list **restrict dev_list, struct names *this_name, dev_t device)
{
struct device_list *dev_tmp, *dev_head;
/*printf("Adding device %s %d\n", this_name->filename, device);*/
- dev_head = *dev_list;
- if ( (dev_tmp = malloc(sizeof(struct device_list))) == NULL)
+ if ( (dev_tmp = (struct device_list*)malloc(sizeof(struct device_list))) == NULL)
return;
+ dev_head = *dev_list;
dev_tmp->name = this_name;
dev_tmp->device = device;
dev_tmp->next = dev_head;
@@ -269,16 +271,14 @@ static void add_ip_conn(struct ip_connec
{
struct ip_connections *ip_tmp, *ip_head;
- ip_head = *ip_list;
-
if ( (ip_tmp = malloc(sizeof(struct ip_connections))) == NULL)
return;
+ ip_head = *ip_list;
ip_tmp->name = this_name;
ip_tmp->lcl_port = lcl_port;
ip_tmp->rmt_port = rmt_port;
ip_tmp->rmt_address.s_addr = rmt_address;
ip_tmp->next = ip_head;
-
*ip_list = ip_tmp;
}
@@ -287,10 +287,9 @@ static void add_ip6_conn(struct ip6_conn
{
struct ip6_connections *ip_tmp, *ip_head;
- ip_head = *ip_list;
-
if ( (ip_tmp = malloc(sizeof(struct ip6_connections))) == NULL)
return;
+ ip_head = *ip_list;
ip_tmp->name = this_name;
ip_tmp->lcl_port = lcl_port;
ip_tmp->rmt_port = rmt_port;
@@ -319,7 +318,7 @@ static void add_matched_proc(struct name
}
}
/* Not found */
- if ( (pptr = malloc(sizeof (struct procs))) == NULL) {
+ if ( (pptr = (struct procs*)malloc(sizeof (struct procs))) == NULL) {
fprintf(stderr,_("Cannot allocate memory for matched proc: %s\n"), strerror(errno));
return;
}
@@ -329,10 +328,13 @@ static void add_matched_proc(struct name
pptr->next = NULL;
/* set command name */
pptr->command = NULL;
+
+ fp = NULL;
+ pathname = NULL;
if ( (asprintf(&pathname, "/proc/%d/stat", pid) > 0) &&
( (fp = fopen(pathname, "r")) != NULL) &&
( fscanf(fp, "%*d (%100[^)]", cmdname) == 1))
- if ( (pptr->command = malloc(MAX_CMDNAME+1)) != NULL) {
+ if ( (pptr->command = (char*)malloc(MAX_CMDNAME+1)) != NULL) {
cmdlen = 0;
for (cptr = cmdname; cmdlen < MAX_CMDNAME && *cptr ; cptr++) {
if (isprint(*cptr))
@@ -346,6 +348,10 @@ static void add_matched_proc(struct name
name_list->matched_procs = pptr;
else
last_proc->next = pptr;
+ if (pathname)
+ free(pathname);
+ if (fp)
+ fclose(fp);
}
int parse_mount(struct names *this_name, struct device_list **dev_list)
@@ -372,16 +378,16 @@ int parse_file(struct names *this_name,
real[0] = '\0';
if (check4nfs(this_name->filename, real)) {
- if ((opts & (OPT_MOUNTPOINT|OPT_MOUNTS)) == 0) {
+ if (this_name->filename)
free(this_name->filename);
- this_name->filename = strdup(real);
- this_name->name_space |= NAMESPACE_NFS;
- add_inode(ino_list, this_name, (dev_t)-1, (ino_t)-1);
- return 0;
- }
+ this_name->filename = strdup(real);
+ this_name->name_space |= NAMESPACE_NFS;
+ add_inode(ino_list, this_name, (dev_t)-1, (ino_t)-1);
+ return 0;
}
if (real[0] != '\0') {
- free(this_name->filename);
+ if (this_name->filename)
+ free(this_name->filename);
this_name->filename = strdup(real);
}
@@ -1218,23 +1224,25 @@ static struct stat *get_pidstat(const op
char pathname[256];
struct stat *st;
- if ( (st = malloc(sizeof(struct stat))) == NULL)
+ if ( (st = (struct stat*)malloc(sizeof(struct stat))) == NULL)
return NULL;
snprintf(pathname, 256, "/proc/%d/%s", pid, filename);
if (check4nfs(pathname, real)) {
if ((opts & (OPT_MOUNTPOINT|OPT_MOUNTS)) == 0)
- return NULL;
+ goto out;
}
if (stat(pathname, st) != 0)
- return NULL;
- else
- return st;
+ goto out;
+ return st;
+out:
+ free(st);
+ return NULL;
}
static void check_dir(const pid_t pid, const char *dirname, struct device_list *dev_head, struct inode_list *ino_head, const uid_t uid, const char access,
struct unixsocket_list *sockets, dev_t netdev)
{
- char *dirpath, *filepath;
+ char *dirpath = NULL, *filepath = NULL;
DIR *dirp;
struct dirent *direntry;
struct inode_list *ino_tmp;
@@ -1243,13 +1251,13 @@ static void check_dir(const pid_t pid, c
struct stat st, lst;
if ( (dirpath = malloc(MAX_PATHNAME)) == NULL)
- return;
+ goto out;
if ( (filepath = malloc(MAX_PATHNAME)) == NULL)
- return;
+ goto out;
snprintf(dirpath, MAX_PATHNAME, "/proc/%d/%s", pid, dirname);
if ( (dirp = opendir(dirpath)) == NULL)
- return;
+ goto out;
while ( (direntry = readdir(dirp)) != NULL) {
if (direntry->d_name[0] < '0' || direntry->d_name[0] > '9')
continue;
@@ -1293,8 +1301,11 @@ static void check_dir(const pid_t pid, c
}
} /* while fd_dent */
closedir(dirp);
- free(dirpath);
- free(filepath);
+out:
+ if (dirpath)
+ free(dirpath);
+ if (filepath)
+ free(filepath);
}
static void check_map(const pid_t pid, const char *filename, struct device_list *dev_head, struct inode_list *ino_head, const uid_t uid, const char access)
@@ -1349,7 +1360,7 @@ void add_mount_device(struct mountdev_li
struct mountdev_list *newmount;
/*printf("Adding mount Path: %s Dir:%s dev:%0x\n",dir, fsname, device);*/
- if ( (newmount = malloc(sizeof(struct mountdev_list))) == NULL)
+ if ( (newmount = (struct mountdev_list*)malloc(sizeof(struct mountdev_list))) == NULL)
return;
newmount->fsname = strdup(fsname);
newmount->dir = strdup(dir);
@@ -1394,14 +1405,17 @@ void fill_unix_cache(struct unixsocket_l
free(path);
continue;
}
- if ( (newsocket = malloc(sizeof(struct unixsocket_list))) == NULL)
+ if ( (newsocket = (struct unixsocket_list*)malloc(sizeof(struct unixsocket_list))) == NULL) {
+ free(path);
continue;
+ }
newsocket->sun_name = strdup(scanned_path);
newsocket->inode = st.st_ino;
newsocket->dev = st.st_dev;
newsocket->net_inode = scanned_inode;
newsocket->next = *unixsocket_head;
*unixsocket_head = newsocket;
+ free(path);
} /* while */
fclose(fp);