From: Jaromir Capik Date: Fri, 31 Jan 2014 11:45:26 +0000 (+1100) Subject: Fixing null pointer dereference X-Git-Tag: v22.21~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4b50aac325fb56c3188b12fadcca1dec1adba17c;p=psmisc Fixing null pointer dereference Introduced with the namespaces support References: https://sourceforge.net/p/psmisc/patches/32/ https://sourceforge.net/p/psmisc/code/ci/e64b282643afecb7060c321f8401872feaf07dbb/log/?path= Signed-off-by: Craig Small --- diff --git a/src/pstree.c b/src/pstree.c index 2d5c05e..1cbe131 100644 --- a/src/pstree.c +++ b/src/pstree.c @@ -220,7 +220,12 @@ static void find_ns_and_add(struct ns_entry **root, PROC *r, enum ns_type id) } if (!ptr) { - ptr = malloc(sizeof(*ptr)); + + if (!(ptr = malloc(sizeof(*ptr)))) { + perror("malloc"); + exit(1); + } + memset(ptr, 0, sizeof(*ptr)); ptr->number = r->ns[id]; if (*root == NULL) @@ -232,7 +237,12 @@ static void find_ns_and_add(struct ns_entry **root, PROC *r, enum ns_type id) /* move the child to under the namespace's umbrella */ for (c = &ptr->children; *c; c = &(*c)->next) ; - *c = malloc(sizeof(CHILD)); + + if (!(*c = malloc(sizeof(CHILD)))) { + perror("malloc"); + exit(1); + } + (*c)->child = r; (*c)->next = NULL;