library: respond to coverity (reluctantly), <PIDS> api

Calls to free() have now been reintroduce in the new()
function to quiet coverity warnings. Those free's were
removed originally as that library 'new' was returning
with a fatal error and a caller should end abnormally.

Plus, it is virtually impossible to fail a malloc call
under linux. And lastly, they required braces with the
if statement making the code considerably less pretty.

[ commit also includes 2 unrelated whitespace tweaks ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2016-05-16 14:14:14 -05:00 committed by Craig Small
parent 0580a7b4c6
commit 0511ab0245

View File

@ -885,7 +885,7 @@ static inline int items_check_failed (
* offer any sort of warning like the following:
*
* warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'enum pids_item *'
* if (procps_pids_new(&info, 3, PROCPS_PIDS_noop) < 0)
* if (procps_pids_new(&info, PROCPS_PIDS_noop, 3) < 0)
* ^~~~~~~~~~~~~~~~
*/
if (numitems < 1
@ -1136,20 +1136,27 @@ PROCPS_EXPORT int procps_pids_new (
/* if we're without items or numitems, a later call to
procps_pids_reset() will become mandatory */
if (items && numitems) {
if (items_check_failed(numitems, items))
if (items_check_failed(numitems, items)) {
free(p);
return -EINVAL;
}
// allow for our PROCPS_PIDS_logical_end
p->maxitems = numitems + 1;
if (!(p->items = calloc(p->maxitems, sizeof(enum pids_item))))
if (!(p->items = calloc(p->maxitems, sizeof(enum pids_item)))) {
free(p);
return -ENOMEM;
}
memcpy(p->items, items, sizeof(enum pids_item) * numitems);
p->items[numitems] = PROCPS_PIDS_logical_end;
p->curitems = p->maxitems;
libflags_set(p);
}
if (!(p->hist = calloc(MEMORY_INCR, sizeof(struct history_info))))
if (!(p->hist = calloc(MEMORY_INCR, sizeof(struct history_info)))) {
free(p->items);
free(p);
return -ENOMEM;
}
config_history(p);
pgsz = getpagesize();