diff --git a/lib/libjail/jail.c b/lib/libjail/jail.c index a3745634dcf1..46da6b49e3f4 100644 --- a/lib/libjail/jail.c +++ b/lib/libjail/jail.c @@ -719,6 +719,7 @@ jailparam_get(struct jailparam *jp, unsigned njp, int flags) char * jailparam_export(struct jailparam *jp) { + size_t *valuelens; char *value, *tvalue, **values; size_t valuelen; int i, nval, ival; @@ -740,6 +741,7 @@ jailparam_export(struct jailparam *jp) return (value); } values = alloca(nval * sizeof(char *)); + valuelens = alloca(nval * sizeof(size_t)); valuelen = 0; for (i = 0; i < nval; i++) { switch (jp->jp_ctltype & CTLTYPE) { @@ -809,11 +811,12 @@ jailparam_export(struct jailparam *jp) errno = ENOENT; return (NULL); } - valuelen += strlen(valbuf) + 1; - values[i] = alloca(valuelen); + valuelens[i] = strlen(valbuf) + 1; + valuelen += valuelens[i]; + values[i] = alloca(valuelens[i]); strcpy(values[i], valbuf); } - value = malloc(valuelen + 1); + value = malloc(valuelen); if (value == NULL) strerror_r(errno, jail_errmsg, JAIL_ERRMSGLEN); else { @@ -821,8 +824,8 @@ jailparam_export(struct jailparam *jp) for (i = 0; i < nval; i++) { strcpy(tvalue, values[i]); if (i < nval - 1) { - tvalue += strlen(values[i]); - *tvalue++ = ','; + tvalue += valuelens[i]; + tvalue[-1] = ','; } } } @@ -830,7 +833,7 @@ jailparam_export(struct jailparam *jp) } /* - * Free the contents of a jail parameter list (but not thst list itself). + * Free the contents of a jail parameter list (but not the list itself). */ void jailparam_free(struct jailparam *jp, unsigned njp)