}
#endif
-#ifndef xrealloc
-void *
-xrealloc(void *ptr, size_t s)
-{
- void *value = realloc(ptr, s);
-
- if (value != NULL)
- return value;
- syslog(LOG_ERR, "memory exhausted (xrealloc %zu bytes)", s);
- exit(EXIT_FAILURE);
- /* NOTREACHED */
-}
-#endif
-
#ifndef xstrdup
char *
xstrdup(const char *str)
ssize_t setvard(char ***, const char *, const char *, int);
time_t uptime(void);
int writepid(int, pid_t);
-void *xrealloc(void *, size_t);
void *xmalloc(size_t);
char *xstrdup(const char *);
char **newlist;
char **lst = ifo->environ;
size_t i = 0, l, lv;
- char *match = NULL, *p;
+ char *match = NULL, *p, *n;
match = xstrdup(value);
p = strchr(match, '=');
/* Append a space and the value to it */
l = strlen(lst[i]);
lv = strlen(p);
- lst[i] = xrealloc(lst[i], l + lv + 2);
+ n = realloc(lst[i], l + lv + 2);
+ if (n == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return NULL;
+ }
+ lst[i] = n;
lst[i][l] = ' ';
memcpy(lst[i] + l + 1, p, lv);
lst[i][l + lv + 1] = '\0';
i++;
}
- newlist = xrealloc(lst, sizeof(char *) * (i + 2));
+ newlist = realloc(lst, sizeof(char *) * (i + 2));
+ if (newlist == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return NULL;
+ }
newlist[i] = xstrdup(value);
newlist[i + 1] = NULL;
ifo->environ = newlist;
static char **
splitv(int *argc, char **argv, const char *arg)
{
- char **v = argv;
- char *o = xstrdup(arg), *p, *t;
+ char **n, **v = argv;
+ char *o = xstrdup(arg), *p, *t, *nt;
p = o;
while ((t = strsep(&p, ", "))) {
+ nt = strdup(t);
+ if (nt == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return NULL;
+ }
(*argc)++;
- v = xrealloc(v, sizeof(char *) * ((*argc)));
- v[(*argc) - 1] = xstrdup(t);
+ n = realloc(v, sizeof(char *) * ((*argc)));
+ if (n == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return NULL;
+ }
+ v = n;
+ v[(*argc) - 1] = nt;
}
free(o);
return v;
parse_option(struct if_options *ifo, int opt, const char *arg)
{
int i;
- char *p = NULL, *fp, *np;
+ char *p = NULL, *fp, *np, **nconf;
ssize_t s;
struct in_addr addr, addr2;
+ in_addr_t *naddr;
struct rt *rt;
const struct dhcp_opt const *d;
uint8_t *request, *require, *no;
s++;
}
}
- ifo->config = xrealloc(ifo->config,
- sizeof(char *) * (s + 2));
+ nconf = realloc(ifo->config, sizeof(char *) * (s + 2));
+ if (p == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
+ ifo->config = nconf;
ifo->config[s] = xstrdup(arg);
ifo->config[s + 1] = NULL;
}
return -1;
if (strchr(arg, '/') == NULL)
addr2.s_addr = INADDR_BROADCAST;
- ifo->whitelist = xrealloc(ifo->whitelist,
+ naddr = realloc(ifo->whitelist,
sizeof(in_addr_t) * (ifo->whitelist_len + 2));
+ if (naddr == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
+ ifo->whitelist = naddr;
ifo->whitelist[ifo->whitelist_len++] = addr.s_addr;
ifo->whitelist[ifo->whitelist_len++] = addr2.s_addr;
break;
return -1;
if (strchr(arg, '/') == NULL)
addr2.s_addr = INADDR_BROADCAST;
- ifo->blacklist = xrealloc(ifo->blacklist,
+ naddr = realloc(ifo->blacklist,
sizeof(in_addr_t) * (ifo->blacklist_len + 2));
+ if (naddr == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
+ ifo->blacklist = naddr;
ifo->blacklist[ifo->blacklist_len++] = addr.s_addr;
ifo->blacklist[ifo->blacklist_len++] = addr2.s_addr;
break;
case O_ARPING:
if (parse_addr(&addr, NULL, arg) != 0)
return -1;
- ifo->arping = xrealloc(ifo->arping,
+ naddr = realloc(ifo->arping,
sizeof(in_addr_t) * (ifo->arping_len + 1));
+ if (naddr == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ return -1;
+ }
+ ifo->arping = naddr;
ifo->arping[ifo->arping_len++] = addr.s_addr;
break;
case O_DESTINATION:
ntohl(pi->nd_opt_pi_preferred_time);
if (opt) {
l = strlen(opt);
- opt = xrealloc(opt,
+ tmp = realloc(opt,
l + strlen(ap->saddr) + 2);
- opt[l] = ' ';
- strcpy(opt + l + 1, ap->saddr);
+ if (tmp) {
+ opt = tmp;
+ opt[l] = ' ';
+ strcpy(opt + l + 1, ap->saddr);
+ }
} else
opt = xstrdup(ap->saddr);
lifetime = ap->prefix_vltime;
const char *prefix, const char *const *config)
{
ssize_t i, j, e1;
- char **ne, *eq;
+ char **ne, *eq, **nep;
if (config == NULL)
return;
}
if (j == *len) {
j++;
- ne = xrealloc(ne, sizeof(char *) * (j + 1));
+ nep = realloc(ne, sizeof(char *) * (j + 1));
+ if (nep == NULL) {
+ syslog(LOG_ERR, "%s: %m", __func__);
+ break;
+ }
+ ne = nep;
ne[j - 1] = make_var(prefix, config[i]);
*len = j;
}
static ssize_t
make_env(const struct interface *ifp, const char *reason, char ***argv)
{
- char **env, *p;
+ char **env, **nenv, *p;
ssize_t e, elen, l;
const struct if_options *ifo = ifp->options;
const struct interface *ifp2;
elen = 10;
/* Make our env */
- env = xmalloc(sizeof(char *) * (elen + 1));
+ env = calloc(1, sizeof(char *) * (elen + 1));
+ if (env == NULL)
+ goto eexit;
e = strlen("interface") + strlen(ifp->name) + 2;
env[0] = xmalloc(e);
snprintf(env[0], e, "interface=%s", ifp->name);
if (ifp->wireless) {
e = strlen("new_ssid=") + strlen(ifp->ssid) + 2;
if (strcmp(reason, "CARRIER") == 0) {
- env = xrealloc(env, sizeof(char *) * (elen + 2));
+ nenv = realloc(env, sizeof(char *) * (elen + 2));
+ if (nenv == NULL)
+ goto eexit;
+ env = nenv;
env[elen] = xmalloc(e);
snprintf(env[elen++], e, "new_ssid=%s", ifp->ssid);
}
else if (strcmp(reason, "NOCARRIER") == 0) {
- env = xrealloc(env, sizeof(char *) * (elen + 2));
+ nenv = realloc(env, sizeof(char *) * (elen + 2));
+ if (nenv == NULL)
+ goto eexit;
+ env = nenv;
env[elen] = xmalloc(e);
snprintf(env[elen++], e, "old_ssid=%s", ifp->ssid);
}
if (dhcp && state && state->old) {
e = dhcp_env(NULL, NULL, state->old, ifp);
if (e > 0) {
- env = xrealloc(env, sizeof(char *) * (elen + e + 1));
- elen += dhcp_env(env + elen, "old",
- state->old, ifp);
+ nenv = realloc(env, sizeof(char *) * (elen + e + 1));
+ if (nenv == NULL)
+ goto eexit;
+ env = nenv;
+ elen += dhcp_env(env + elen, "old", state->old, ifp);
}
append_config(&env, &elen, "old",
(const char *const *)ifo->config);
e = dhcp6_env(NULL, NULL, ifp,
d6_state->old, d6_state->old_len);
if (e > 0) {
- env = xrealloc(env, sizeof(char *) * (elen + e + 1));
+ nenv = realloc(env, sizeof(char *) * (elen + e + 1));
+ if (nenv == NULL)
+ goto eexit;
+ env = nenv;
elen += dhcp6_env(env + elen, "old", ifp,
d6_state->old, d6_state->old_len);
}
if (dhcp && state && state->new) {
e = dhcp_env(NULL, NULL, state->new, ifp);
if (e > 0) {
- env = xrealloc(env, sizeof(char *) * (elen + e + 1));
+ nenv = realloc(env, sizeof(char *) * (elen + e + 1));
+ if (nenv == NULL)
+ goto eexit;
+ env = nenv;
elen += dhcp_env(env + elen, "new",
state->new, ifp);
}
e = dhcp6_env(NULL, NULL, ifp,
d6_state->new, d6_state->new_len);
if (e > 0) {
- env = xrealloc(env, sizeof(char *) * (elen + e + 1));
+ nenv = realloc(env, sizeof(char *) * (elen + e + 1));
+ if (nenv == NULL)
+ goto eexit;
+ env = nenv;
elen += dhcp6_env(env + elen, "new", ifp,
d6_state->new, d6_state->new_len);
}
if (ra) {
e = ipv6rs_env(NULL, NULL, ifp);
if (e > 0) {
- env = xrealloc(env, sizeof(char *) * (elen + e + 1));
+ nenv = realloc(env, sizeof(char *) * (elen + e + 1));
+ if (nenv == NULL)
+ goto eexit;
+ env = nenv;
elen += ipv6rs_env(env + elen, NULL, ifp);
}
}
e = 0;
while (ifo->environ[e++])
;
- env = xrealloc(env, sizeof(char *) * (elen + e + 1));
+ nenv = realloc(env, sizeof(char *) * (elen + e + 1));
+ if (nenv == NULL)
+ goto eexit;
+ env = nenv;
e = 0;
while (ifo->environ[e]) {
env[elen + e] = xstrdup(ifo->environ[e]);
*argv = env;
return elen;
+
+eexit:
+ syslog(LOG_ERR, "%s: %m", __func__);
+ nenv = env;
+ while (*nenv)
+ free(*nenv++);
+ free(env);
+ return -1;
}
static int
/* Make our env */
elen = make_env(ifp, reason, &env);
- env = xrealloc(env, sizeof(char *) * (elen + 2));
+ ep = realloc(env, sizeof(char *) * (elen + 2));
+ if (ep == NULL) {
+ elen = -1;
+ goto out;
+ }
+ env = ep;
/* Add path to it */
path = getenv("PATH");
if (path) {
}
free(bigenv);
+out:
/* Cleanup */
ep = env;
while (*ep)
free(*ep++);
free(env);
+ if (elen == -1)
+ return -1;
return WEXITSTATUS(status);
}