]> granicus.if.org Git - procps-ng/commitdiff
library: respond to coverity (reluctantly), <PIDS> api
authorJim Warner <james.warner@comcast.net>
Mon, 16 May 2016 19:14:14 +0000 (14:14 -0500)
committerCraig Small <csmall@dropbear.xyz>
Tue, 17 May 2016 11:13:34 +0000 (21:13 +1000)
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>
proc/pids.c

index 499c03f94959e5ad80f0925177b944cc55a0d3c9..658f0524844be20ec3e212bb34b3e9896d1e9c27 100644 (file)
@@ -474,7 +474,7 @@ static struct {
     { 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_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_C),     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:
      *
      * 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
     || (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
        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();