From def585881089f5e4ab9d236aa5ca48f4c64442ab Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Wed, 26 Aug 2015 00:00:00 -0500 Subject: [PATCH] library: fix major oops in procps_pids_reap() function In my zeal to finalize the initial pids implementation I omitted some quite important parameter checking from the above function. Thank goodness top was kind to us. Also, in anticipation of the additions of single stack read and supporting functions some items were renamed. Signed-off-by: Jim Warner --- proc/pids.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/proc/pids.c b/proc/pids.c index 854af90b..913bb2b3 100644 --- a/proc/pids.c +++ b/proc/pids.c @@ -956,10 +956,17 @@ PROCPS_EXPORT struct pids_reap *procps_pids_reap ( #define n_alloc info->alloc_total #define n_inuse info->inuse_total static proc_t task; // static for initial zeroes + later dynamic free(s) - proc_t*(*read_something)(PROCTAB*, proc_t*); + proc_t*(*reap_something)(PROCTAB*, proc_t*); struct pids_stacks *ext; int n_save = n_alloc; + if (info == NULL) + return NULL; + if (!info->maxitems && !info->curitems) + return NULL; + if (which != PROCPS_REAP_TASKS_ONLY && which != PROCPS_REAP_THREADS_TOO) + return NULL; + if (!info->anchor) { if ((!(info->anchor = calloc(sizeof(void *), MEMORY_INCR))) || (!(info->reaped.stacks = calloc(sizeof(void *), MEMORY_INCR))) @@ -975,7 +982,7 @@ PROCPS_EXPORT struct pids_reap *procps_pids_reap ( if (!oldproc_open(info, 0)) return NULL; toggle_history(info); - read_something = which ? readeither : readproc; + reap_something = which ? readeither : readproc; for (n_inuse = 0; ; n_inuse++) { if (n_inuse == n_alloc) { @@ -985,7 +992,7 @@ PROCPS_EXPORT struct pids_reap *procps_pids_reap ( return NULL; memcpy(info->anchor + n_inuse, ext->stacks, sizeof(void *) * MEMORY_INCR); } - if (NULL == read_something(info->PT, &task)) + if (NULL == reap_something(info->PT, &task)) break; if (!tally_proc(info, &info->reaped.counts, &task)) return NULL; @@ -1131,6 +1138,7 @@ PROCPS_EXPORT int procps_pids_stacks_dealloc ( struct pids_stacks **these) { struct stacks_extent *ext; + int rc; if (info == NULL || these == NULL) return -EINVAL; @@ -1138,7 +1146,7 @@ PROCPS_EXPORT int procps_pids_stacks_dealloc ( return -EINVAL; ext = (struct stacks_extent *)(*these); - int rc = free_extent(info, ext); + rc = free_extent(info, ext); *these = NULL; return rc; } // end: procps_pids_stacks_dealloc