1 This is https://gitlab.com/procps-ng/procps/-/commit/2c933ecba3bb1d3041a5a7a53a7b4078a6003413.diff
2 back-ported to procps 3.3.17. That commit changes xmalloc to xcalloc. This patch differs in two ways:
4 * We modify it to change malloc (no x-) to xcalloc instead
5 * We pull in procps-4's definition of xcalloc
7 Alternative considered: Also pull in commits that changed malloc to xmalloc and defined xcalloc.
8 This alternative is rejected because those commits contain many other unrelated changes.
10 diff --git a/ps/parser.c b/ps/parser.c
11 index 4263a1fb..ee9a57d9 100644
18 +static void *xxcalloc(const size_t nelems, const size_t size)
20 + void *ret = calloc(nelems, size);
21 + if (!ret && size && nelems)
22 + xerrx(EXIT_FAILURE, "cannot allocate %zu bytes", nelems*size);
29 @@ -184,7 +192,6 @@ static const char *parse_list(const char *arg, const char *(*parse_fn)(char *, s
30 const char *err; /* error code that could or did happen */
31 /*** prepare to operate ***/
32 node = malloc(sizeof(selection_node));
33 - node->u = malloc(strlen(arg)*sizeof(sel_union)); /* waste is insignificant */
36 /*** sanity check and count items ***/
37 @@ -205,6 +212,7 @@ static const char *parse_list(const char *arg, const char *(*parse_fn)(char *, s
39 if(need_item) goto parse_error;
41 + node->u = xxcalloc(items, sizeof(sel_union));
42 /*** actually parse the list ***/
45 @@ -1031,15 +1039,15 @@ static const char *parse_trailing_pids(void){
46 thisarg = ps_argc - 1; /* we must be at the end now */
48 pidnode = malloc(sizeof(selection_node));
49 - pidnode->u = malloc(i*sizeof(sel_union)); /* waste is insignificant */
50 + pidnode->u = xxcalloc(i, sizeof(sel_union)); /* waste is insignificant */
53 grpnode = malloc(sizeof(selection_node));
54 - grpnode->u = malloc(i*sizeof(sel_union)); /* waste is insignificant */
55 + grpnode->u = xxcalloc(i, sizeof(sel_union)); /* waste is insignificant */
58 sidnode = malloc(sizeof(selection_node));
59 - sidnode->u = malloc(i*sizeof(sel_union)); /* waste is insignificant */
60 + sidnode->u = xxcalloc(i, sizeof(sel_union)); /* waste is insignificant */