The xalloc.h provides necessary error checking.
Signed-off-by: Jim Warner <james.warner@comcast.net>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
--- /dev/null
+/*
+ * General memory allocation wrappers for malloc, realloc, calloc
+ * and strdup.
+ */
+
+#ifndef PROCPS_NG_XALLOC_H
+#define PROCPS_NG_XALLOC_H
+
+#include <stdlib.h>
+#include <string.h>
+
+#include "c.h"
+
+#ifndef XALLOC_EXIT_CODE
+# define XALLOC_EXIT_CODE EXIT_FAILURE
+#endif
+
+static inline __ul_alloc_size(1)
+void *xmalloc(const size_t size)
+{
+ void *ret = malloc(size);
+ if (!ret && size)
+ err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
+ return ret;
+}
+
+static inline __ul_alloc_size(2)
+void *xrealloc(void *ptr, const size_t size)
+{
+ void *ret = realloc(ptr, size);
+ if (!ret && size)
+ err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
+ return ret;
+}
+
+static inline __ul_calloc_size(1, 2)
+void *xcalloc(const size_t nelems, const size_t size)
+{
+ void *ret = calloc(nelems, size);
+ if (!ret && size && nelems)
+ err(XALLOC_EXIT_CODE, "cannot allocate %zu bytes", size);
+ return ret;
+}
+
+static inline char *xstrdup(const char *str)
+{
+ char *ret;
+ if (!str)
+ return NULL;
+ ret = strdup(str);
+ if (!ret)
+ err(XALLOC_EXIT_CODE, "cannot duplicate string");
+ return ret;
+}
+
+#endif /* PROCPS_NG_XALLOC_H */
#include <errno.h>
#include <getopt.h>
+// EXIT_SUCCESS is 0
+// EXIT_FAILURE is 1
+#define EXIT_USAGE 2
+#define EXIT_FATAL 3
+#define XALLOC_EXIT_CODE EXIT_FATAL
+
#include "c.h"
#include "nls.h"
+#include "xalloc.h"
#include "proc/readproc.h"
#include "proc/sig.h"
#include "proc/devname.h"
#include "proc/sysinfo.h"
#include "proc/version.h" /* procps_version */
-// EXIT_SUCCESS is 0
-// EXIT_FAILURE is 1
-#define EXIT_USAGE 2
-#define EXIT_FATAL 3
-
static int i_am_pkill = 0;
static const char *progname = "pgrep";
static union el *split_list (const char *restrict str, int (*convert)(const char *, union el *))
{
- char *copy = strdup (str);
+ char *copy = xstrdup (str);
char *ptr = copy;
char *sep_pos;
int i = 0;
if (i == size) {
size = size * 5 / 4 + 4;
// add 1 because slot zero is a count
- list = realloc (list, 1 + size * sizeof *list);
- if (list == NULL)
- exit (EXIT_FATAL);
+ list = xrealloc (list, 1 + size * sizeof *list);
}
sep_pos = strchr (ptr, ',');
if (sep_pos)
goto out;
if(*endp && !isspace(*endp))
goto out;
- list = malloc(2 * sizeof *list);
+ list = xmalloc(2 * sizeof *list);
list[0].num = 1;
list[1].num = pid;
out:
static int conv_str (const char *restrict name, union el *restrict e)
{
- e->str = strdup (name);
+ e->str = xstrdup (name);
return 1;
}
if (opt_euid && !opt_negate) {
int num = opt_euid[0].num;
int i = num;
- uid_t *uids = malloc (num * sizeof (uid_t));
- if (uids == NULL)
- exit (EXIT_FATAL);
+ uid_t *uids = xmalloc (num * sizeof (uid_t));
while (i-- > 0) {
uids[i] = opt_euid[i+1].num;
}
char errbuf[256];
int re_err;
- preg = malloc (sizeof (regex_t));
- if (preg == NULL)
- exit (EXIT_FATAL);
+ preg = xmalloc (sizeof (regex_t));
if (opt_exact) {
- re = malloc (strlen (opt_pattern) + 5);
- if (re == NULL)
- exit (EXIT_FATAL);
+ re = xmalloc (strlen (opt_pattern) + 5);
sprintf (re, "^(%s)$", opt_pattern);
} else {
re = opt_pattern;
}
if (matches == size) {
size = size * 5 / 4 + 4;
- list = realloc(list, size * sizeof *list);
- if (list == NULL)
- exit (EXIT_FATAL);
+ list = xrealloc(list, size * sizeof *list);
}
if (opt_long) {
char buff[5096]; // FIXME
sprintf (buff, "%d %s", task.XXXID, cmd);
- list[matches++].str = strdup (buff);
+ list[matches++].str = xstrdup (buff);
} else {
list[matches++].num = task.XXXID;
}
// case 'D': // FreeBSD: print info about non-matches for debugging
// break;
case 'F': // FreeBSD: the arg is a file containing a PID to match
- opt_pidfile = strdup (optarg);
+ opt_pidfile = xstrdup (optarg);
++criteria_count;
break;
case 'G': // Solaris: match rgid/rgroup
opt_count = 1;
break;
case 'd': // Solaris: change the delimiter
- opt_delim = strdup (optarg);
+ opt_delim = xstrdup (optarg);
break;
case 'f': // Solaris: match full process name (as in "ps -f")
opt_full = 1;
#include "c.h"
#include "nls.h"
#include "proc/escape.h"
+#include "xalloc.h"
#include "proc/readproc.h"
#include "proc/version.h"
if (d_option && x_option)
errx(EXIT_FAILURE, _("options -d and -x cannot coexist"));
- pidlist = malloc(sizeof(unsigned) * argc);
- if (pidlist == NULL)
- err(EXIT_FAILURE, _("cannot allocate %zu bytes"),
- sizeof(unsigned) * argc);
+ pidlist = xmalloc(sizeof(unsigned) * argc);
while (*argv) {
char *walk = *argv++;
#include "c.h"
#include "nls.h"
+#include "xalloc.h"
#include "proc/pwcache.h"
#include "proc/sig.h"
#include "proc/devname.h"
static int *pids;
#define ENLIST(thing,addme) do{ \
-if(!thing##s) thing##s = malloc(sizeof(*thing##s)*saved_argc); \
-if(!thing##s) fprintf(stderr,_("No memory.\n")),exit(2); \
+if(!thing##s) thing##s = xmalloc(sizeof(*thing##s)*saved_argc); \
thing##s[thing##_count++] = addme; \
}while(0)
#include "c.h"
#include "nls.h"
+#include "xalloc.h"
#include "proc/procps.h"
#include "proc/version.h"
}
/* used to display the output */
- outname = strdup(name);
+ outname = xstrdup(name);
slashdot(outname,'/','.'); /* change / to . */
if (pattern && !pattern_match(outname, pattern)){
}
/* used to open the file */
- tmpname = malloc(strlen(name)+strlen(PROC_PATH)+2);
+ tmpname = xmalloc(strlen(name)+strlen(PROC_PATH)+2);
strcpy(tmpname, PROC_PATH);
strcat(tmpname, name);
slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */
/* used to display the output */
- outname = strdup(name);
+ outname = xstrdup(name);
slashdot(outname,'/','.'); /* change / to . */
if (stat(tmpname, &ts) < 0) {
readdir(dp); // skip ..
while (( de = readdir(dp) )) {
char *restrict tmpdir;
- tmpdir = (char *restrict)malloc(strlen(path)+strlen(de->d_name)+2);
+ tmpdir = (char *restrict)xmalloc(strlen(path)+strlen(de->d_name)+2);
sprintf(tmpdir, "%s%s", path, de->d_name);
rc2 = stat(tmpdir, &ts);
if (rc2 != 0) {
}
/* used to open the file */
- tmpname = malloc(equals-name+1+strlen(PROC_PATH));
+ tmpname = xmalloc(equals-name+1+strlen(PROC_PATH));
strcpy(tmpname, PROC_PATH);
strncat(tmpname, name, (int)(equals-name));
tmpname[equals-name+strlen(PROC_PATH)] = 0;
slashdot(tmpname+strlen(PROC_PATH),'.','/'); /* change . to / */
/* used to display the output */
- outname = malloc(equals-name+1);
+ outname = xmalloc(equals-name+1);
strncpy(outname, name, (int)(equals-name));
outname[equals-name] = 0;
slashdot(outname,'/','.'); /* change / to . */
continue;
if (ncfgs % nprealloc == 0) {
- cfgs = realloc(cfgs, sizeof(struct pair*)*(ncfgs+nprealloc));
+ cfgs = xrealloc(cfgs, sizeof(struct pair*)*(ncfgs+nprealloc));
}
- cfgs[ncfgs] = malloc(sizeof(struct pair) + strlen(de->d_name)*2+2 + strlen(dirs[di])+1);
+ cfgs[ncfgs] = xmalloc(sizeof(struct pair) + strlen(de->d_name)*2+2 + strlen(dirs[di])+1);
cfgs[ncfgs]->name = (char*)cfgs[ncfgs]+sizeof(struct pair);
strcpy(cfgs[ncfgs]->name, de->d_name);
cfgs[ncfgs]->value = (char*)cfgs[ncfgs]+sizeof(struct pair) + strlen(cfgs[ncfgs]->name)+1;
IgnoreError = true;
return PreloadSystem();
case 'r':
- pattern = strdup(optarg);
+ pattern = xstrdup(optarg);
break;
case 'V':
printf(PROCPS_NG_VERSION);
#include "proc/sysinfo.h"
#include "c.h"
#include "nls.h"
+#include "xalloc.h"
#include <errno.h>
#include <fcntl.h>
}
scr_size = nrows * ncols;
if (screen == NULL)
- screen = (char *)malloc(scr_size);
+ screen = (char *)xmalloc(scr_size);
else
- screen = (char *)realloc(screen, scr_size);
-
- if (screen == NULL)
- err(EXIT_FAILURE, _("cannot allocate %zu bytes"), scr_size);
+ screen = (char *)xrealloc(screen, scr_size);
memset(screen, ' ', scr_size - 1);
*(screen + scr_size - 2) = '\0';
#include "config.h"
#include "nls.h"
#include "proc/procps.h"
+#include "xalloc.h"
#include <ctype.h>
#include <errno.h>
#include <errno.h>
/* save for later */
command_argv = &(argv[optind]);
- command = strdup(argv[optind++]);
+ command = xstrdup(argv[optind++]);
command_length = strlen(command);
for (; optind < argc; optind++) {
char *endp;
int s = strlen(argv[optind]);
/* space and \0 */
- command = realloc(command, command_length + s + 2);
+ command = xrealloc(command, command_length + s + 2);
endp = command + command_length;
*endp = ' ';
memcpy(endp + 1, argv[optind], s);