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

@ -474,7 +474,7 @@ static struct {
{ RS(SUPGROUPS), x_supgrp, FF(str), QS(str), 0, ref_SUPGROUPS }, { RS(SUPGROUPS), x_supgrp, FF(str), QS(str), 0, ref_SUPGROUPS },
{ RS(TICS_ALL), f_stat, NULL, QS(ull_int), 0, -1 }, { RS(TICS_ALL), f_stat, NULL, QS(ull_int), 0, -1 },
{ RS(TICS_ALL_C), f_stat, NULL, QS(ull_int), 0, -1 }, { RS(TICS_ALL_C), f_stat, NULL, QS(ull_int), 0, -1 },
{ RS(TICS_DELTA), f_stat, NULL, QS(sl_int), +1, -1 }, { RS(TICS_DELTA), f_stat, NULL, QS(sl_int), +1, -1 },
{ RS(TICS_SYSTEM), f_stat, NULL, QS(ull_int), 0, -1 }, { RS(TICS_SYSTEM), f_stat, NULL, QS(ull_int), 0, -1 },
{ RS(TICS_SYSTEM_C), f_stat, NULL, QS(ull_int), 0, -1 }, { RS(TICS_SYSTEM_C), f_stat, NULL, QS(ull_int), 0, -1 },
{ RS(TICS_USER), f_stat, NULL, QS(ull_int), 0, -1 }, { RS(TICS_USER), f_stat, NULL, QS(ull_int), 0, -1 },
@ -885,8 +885,8 @@ static inline int items_check_failed (
* offer any sort of warning like the following: * offer any sort of warning like the following:
* *
* warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'enum pids_item *' * 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 if (numitems < 1
|| (void *)items < (void *)0x8000) // twice as big as our largest enum || (void *)items < (void *)0x8000) // twice as big as our largest enum
@ -1136,20 +1136,27 @@ PROCPS_EXPORT int procps_pids_new (
/* if we're without items or numitems, a later call to /* if we're without items or numitems, a later call to
procps_pids_reset() will become mandatory */ procps_pids_reset() will become mandatory */
if (items && numitems) { if (items && numitems) {
if (items_check_failed(numitems, items)) if (items_check_failed(numitems, items)) {
free(p);
return -EINVAL; return -EINVAL;
}
// allow for our PROCPS_PIDS_logical_end // allow for our PROCPS_PIDS_logical_end
p->maxitems = numitems + 1; 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; return -ENOMEM;
}
memcpy(p->items, items, sizeof(enum pids_item) * numitems); memcpy(p->items, items, sizeof(enum pids_item) * numitems);
p->items[numitems] = PROCPS_PIDS_logical_end; p->items[numitems] = PROCPS_PIDS_logical_end;
p->curitems = p->maxitems; p->curitems = p->maxitems;
libflags_set(p); 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; return -ENOMEM;
}
config_history(p); config_history(p);
pgsz = getpagesize(); pgsz = getpagesize();