Commit a753a904 authored by Bernd Eckenfels's avatar Bernd Eckenfels

lib/util.c: introduxe xstrdup which catches NULL and use it in all places

parent e8510bc4
...@@ -208,8 +208,7 @@ void aftrans_def(char *tool, char *argv0, char *dflt) ...@@ -208,8 +208,7 @@ void aftrans_def(char *tool, char *argv0, char *dflt)
else else
tmp++; tmp++;
if (!(buf = strdup(tmp))) buf = xstrdup(tmp);
return;
if (strlen(tool) >= strlen(tmp)) { if (strlen(tool) >= strlen(tmp)) {
free(buf); free(buf);
......
...@@ -224,7 +224,7 @@ static int INET_rresolve(char *name, size_t len, struct sockaddr_in *sin, ...@@ -224,7 +224,7 @@ static int INET_rresolve(char *name, size_t len, struct sockaddr_in *sin,
pn->addr = *sin; pn->addr = *sin;
pn->next = INET_nn; pn->next = INET_nn;
pn->host = host; pn->host = host;
pn->name = strdup(name); pn->name = xstrdup(name);
INET_nn = pn; INET_nn = pn;
return (0); return (0);
...@@ -395,7 +395,7 @@ static int read_services(void) ...@@ -395,7 +395,7 @@ static int read_services(void)
while ((se = getservent())) { while ((se = getservent())) {
/* Allocate a service entry. */ /* Allocate a service entry. */
item = (struct service *) xmalloc(sizeof(struct service)); item = (struct service *) xmalloc(sizeof(struct service));
item->name = strdup(se->s_name); item->name = xstrdup(se->s_name);
item->number = se->s_port; item->number = se->s_port;
/* Fill it in. */ /* Fill it in. */
...@@ -415,7 +415,7 @@ static int read_services(void) ...@@ -415,7 +415,7 @@ static int read_services(void)
while ((pe = getprotoent())) { while ((pe = getprotoent())) {
/* Allocate a service entry. */ /* Allocate a service entry. */
item = (struct service *) xmalloc(sizeof(struct service)); item = (struct service *) xmalloc(sizeof(struct service));
item->name = strdup(pe->p_name); item->name = xstrdup(pe->p_name);
item->number = htons(pe->p_proto); item->number = htons(pe->p_proto);
add2list(&raw_name, item); add2list(&raw_name, item);
} }
......
...@@ -45,8 +45,8 @@ int rindex_nondigit(char *name) ...@@ -45,8 +45,8 @@ int rindex_nondigit(char *name)
/* like strcmp(), but knows about numbers and ':' alias suffix */ /* like strcmp(), but knows about numbers and ':' alias suffix */
int nstrcmp(const char *ap, const char *bp) int nstrcmp(const char *ap, const char *bp)
{ {
char *a = (char*)strdup(ap); char *a = xstrdup(ap);
char *b = (char*)strdup(bp); char *b = xstrdup(bp);
char *an, *bn; char *an, *bn;
int av = 0, bv = 0; int av = 0, bv = 0;
char *aalias=cutalias(a); char *aalias=cutalias(a);
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include <unistd.h> #include <unistd.h>
#include "util.h"
/* Caller must free return string. */ /* Caller must free return string. */
...@@ -48,7 +49,7 @@ char *proc_gen_fmt(char *name, int more, FILE * fh,...) ...@@ -48,7 +49,7 @@ char *proc_gen_fmt(char *name, int more, FILE * fh,...)
name, title); name, title);
return NULL; return NULL;
} }
return strdup(format); return xstrdup(format);
} }
/* /*
......
...@@ -22,6 +22,15 @@ void *xmalloc(size_t sz) ...@@ -22,6 +22,15 @@ void *xmalloc(size_t sz)
return p; return p;
} }
/* Like strdup, but oom() instead of NULL */
char *xstrdup(char *s)
{
char *d = strdup(s);
if (!d)
oom();
return d;
}
void *xrealloc(void *oldp, size_t sz) void *xrealloc(void *oldp, size_t sz)
{ {
void *p = realloc(oldp, sz); void *p = realloc(oldp, sz);
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
void *xmalloc(size_t sz); void *xmalloc(size_t sz);
void *xrealloc(void *p, size_t sz); void *xrealloc(void *p, size_t sz);
char *xstrdup(char *src);
#define new(p) ((p) = xmalloc(sizeof(*(p)))) #define new(p) ((p) = xmalloc(sizeof(*(p))))
......
...@@ -475,7 +475,7 @@ static void prg_cache_load(void) ...@@ -475,7 +475,7 @@ static void prg_cache_load(void)
snprintf(finbuf, sizeof(finbuf), "%s/%s", direproc->d_name, cmdlp); snprintf(finbuf, sizeof(finbuf), "%s/%s", direproc->d_name, cmdlp);
#if HAVE_SELINUX #if HAVE_SELINUX
if (getpidcon(atoi(direproc->d_name), &scon) == -1) { if (getpidcon(atoi(direproc->d_name), &scon) == -1) {
scon=strdup("-"); scon=xstrdup("-");
} }
prg_cache_add(inode, finbuf, scon); prg_cache_add(inode, finbuf, scon);
freecon(scon); freecon(scon);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment