* Convert all uses of strdup() to estrdup();

* Use estrndup() in a few cases where it simplifies the code;
* Provide compatibility definitions of strndup and estrndup;
This commit is contained in:
apb 2007-10-13 16:16:41 +00:00
parent ed29af22c3
commit 67bb324312
4 changed files with 62 additions and 24 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.143 2007/10/05 15:27:45 sjg Exp $ */
/* $NetBSD: main.c,v 1.144 2007/10/13 16:16:41 apb Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,7 +69,7 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: main.c,v 1.143 2007/10/05 15:27:45 sjg Exp $";
static char rcsid[] = "$NetBSD: main.c,v 1.144 2007/10/13 16:16:41 apb Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993\n\
#if 0
static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: main.c,v 1.143 2007/10/05 15:27:45 sjg Exp $");
__RCSID("$NetBSD: main.c,v 1.144 2007/10/13 16:16:41 apb Exp $");
#endif
#endif /* not lint */
#endif
@ -939,7 +939,7 @@ main(int argc, char **argv)
if (syspath == NULL || *syspath == '\0')
syspath = defsyspath;
else
syspath = strdup(syspath);
syspath = estrdup(syspath);
for (start = syspath; *start != '\0'; start = cp) {
for (cp = start; *cp != '\0' && *cp != ':'; cp++)
@ -1688,6 +1688,20 @@ estrdup(const char *str)
return(p);
}
/*
* estrndup --
* strndup, but die on error.
*/
char *
estrndup(const char *str, size_t len)
{
char *p;
if ((p = strndup(str, len)) == NULL)
enomem();
return(p);
}
/*
* erealloc --
* realloc, but die on error.

View File

@ -1,4 +1,4 @@
/* $NetBSD: nonints.h,v 1.44 2007/10/05 15:27:45 sjg Exp $ */
/* $NetBSD: nonints.h,v 1.45 2007/10/13 16:16:41 apb Exp $ */
/*-
* Copyright (c) 1988, 1989, 1990, 1993
@ -118,6 +118,7 @@ int PrintAddr(ClientData, ClientData);
void Finish(int);
#ifndef HAVE_EMALLOC
char *estrdup(const char *);
char *estrndup(const char *, size_t);
void *emalloc(size_t);
void *erealloc(void *, size_t);
void enomem(void);

View File

@ -1,15 +1,15 @@
/* $NetBSD: util.c,v 1.40 2007/01/17 00:21:44 hubertf Exp $ */
/* $NetBSD: util.c,v 1.41 2007/10/13 16:16:41 apb Exp $ */
/*
* Missing stuff from OS's
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: util.c,v 1.40 2007/01/17 00:21:44 hubertf Exp $";
static char rcsid[] = "$NetBSD: util.c,v 1.41 2007/10/13 16:16:41 apb Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: util.c,v 1.40 2007/01/17 00:21:44 hubertf Exp $");
__RCSID("$NetBSD: util.c,v 1.41 2007/10/13 16:16:41 apb Exp $");
#endif
#endif
@ -61,6 +61,33 @@ strdup(const char *str)
}
#endif
#if !defined(MAKE_NATIVE) && !defined(HAVE_STRNDUP)
#include <string.h>
/* strndup
*
* Make a duplicate of a string, up to a maximum length.
* For systems which lack this function.
*/
char *
strndup(const char *str, size_t maxlen)
{
size_t len;
char *p;
if (str == NULL)
return NULL;
len = strlen(str);
if (len > maxlen)
len = maxlen;
p = emalloc(len + 1);
memcpy(p, str, len);
p[len] = '\0';
return p;
}
#endif
#if !defined(MAKE_NATIVE) && !defined(HAVE_SETENV)
int
setenv(const char *name, const char *value, int dum)

View File

@ -1,4 +1,4 @@
/* $NetBSD: var.c,v 1.121 2007/10/13 14:32:18 apb Exp $ */
/* $NetBSD: var.c,v 1.122 2007/10/13 16:16:41 apb Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
@ -69,14 +69,14 @@
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: var.c,v 1.121 2007/10/13 14:32:18 apb Exp $";
static char rcsid[] = "$NetBSD: var.c,v 1.122 2007/10/13 16:16:41 apb Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: var.c,v 1.121 2007/10/13 14:32:18 apb Exp $");
__RCSID("$NetBSD: var.c,v 1.122 2007/10/13 16:16:41 apb Exp $");
#endif
#endif /* not lint */
#endif
@ -1643,7 +1643,7 @@ VarSelectWords(GNode *ctx __unused, Var_Parse_State *vpstate,
/* fake what brk_string() would do if there were only one word */
ac = 1;
av = emalloc((ac + 1) * sizeof(char *));
as = strdup(str);
as = estrdup(str);
av[0] = as;
av[1] = NULL;
} else {
@ -1736,7 +1736,7 @@ VarModify(GNode *ctx, Var_Parse_State *vpstate,
/* fake what brk_string() would do if there were only one word */
ac = 1;
av = emalloc((ac + 1) * sizeof(char *));
as = strdup(str);
as = estrdup(str);
av[0] = as;
av[1] = NULL;
} else {
@ -2259,11 +2259,11 @@ ApplyModifiers(char *nstr, const char *tstr,
++tstr;
if (v->flags & VAR_JUNK) {
/*
* We need to strdup() it incase
* We need to estrdup() it incase
* VarGetPattern() recurses.
*/
sv_name = v->name;
v->name = strdup(v->name);
v->name = estrdup(v->name);
} else if (ctxt != VAR_GLOBAL) {
Var *gv = VarFind(v->name, ctxt, 0);
if (gv == (Var *)NIL)
@ -2423,7 +2423,7 @@ ApplyModifiers(char *nstr, const char *tstr,
{
if ((v->flags & VAR_JUNK) != 0)
v->flags |= VAR_KEEP;
newStr = strdup(v->name);
newStr = estrdup(v->name);
cp = ++tstr;
termc = *tstr;
break;
@ -2438,12 +2438,12 @@ ApplyModifiers(char *nstr, const char *tstr,
if (gn == NILGNODE || gn->type & OP_NOPATH) {
newStr = NULL;
} else if (gn->path) {
newStr = strdup(gn->path);
newStr = estrdup(gn->path);
} else {
newStr = Dir_FindFile(v->name, Suff_FindPath(gn));
}
if (!newStr) {
newStr = strdup(v->name);
newStr = estrdup(v->name);
}
cp = ++tstr;
termc = *tstr;
@ -3436,9 +3436,7 @@ Var_Parse(const char *str, GNode *ctxt, Boolean errnum, int *lengthPtr,
*lengthPtr = tstr - start + 1;
*WR(tstr) = endc;
if (dynamic) {
char *pstr = emalloc(*lengthPtr + 1);
strncpy(pstr, start, *lengthPtr);
pstr[*lengthPtr] = '\0';
char *pstr = estrndup(start, *lengthPtr);
*freePtr = pstr;
Buf_Destroy(buf, TRUE);
return(pstr);
@ -3530,9 +3528,7 @@ Var_Parse(const char *str, GNode *ctxt, Boolean errnum, int *lengthPtr,
*freePtr = NULL;
}
if (dynamic) {
nstr = emalloc(*lengthPtr + 1);
strncpy(nstr, start, *lengthPtr);
nstr[*lengthPtr] = '\0';
nstr = estrndup(start, *lengthPtr);
*freePtr = nstr;
} else {
nstr = var_Error;