fix a few stray unguarded strdup's

This commit is contained in:
Denis Vlasenko
2008-03-28 02:24:59 +00:00
parent 2570b2e575
commit c4523c2b3d
4 changed files with 140 additions and 147 deletions

View File

@@ -285,7 +285,7 @@ static char *string_copy(const char *s)
if (!s) if (!s)
return 0; return 0;
ret = strdup(s); ret = xstrdup(s);
return ret; return ret;
} }

View File

@@ -119,7 +119,7 @@ int ed_main(int argc ATTRIBUTE_UNUSED, char **argv)
static void doCommands(void) static void doCommands(void)
{ {
const char *cp; const char *cp;
char *endbuf, *newname, buf[USERSIZE]; char *endbuf, buf[USERSIZE];
int len, num1, num2; int len, num1, num2;
smallint have1, have2; smallint have1, have2;
@@ -194,13 +194,8 @@ static void doCommands(void)
printf("No file name\n"); printf("No file name\n");
break; break;
} }
newname = strdup(cp);
if (newname == NULL) {
bb_error_msg("no memory for file name");
break;
}
free(fileName); free(fileName);
fileName = newname; fileName = xstrdup(cp);
break; break;
case 'i': case 'i':
@@ -256,7 +251,7 @@ static void doCommands(void)
if (readLines(cp, num1 + 1)) if (readLines(cp, num1 + 1))
break; break;
if (fileName == NULL) if (fileName == NULL)
fileName = strdup(cp); fileName = xstrdup(cp);
break; break;
case 's': case 's':

View File

@@ -800,7 +800,7 @@ static char *encodeString(const char *string)
/* /*
* Given a URL encoded string, convert it to plain ascii. * Given a URL encoded string, convert it to plain ascii.
* Since decoding always makes strings smaller, the decode is done in-place. * Since decoding always makes strings smaller, the decode is done in-place.
* Thus, callers should strdup() the argument if they do not want the * Thus, callers should xstrdup() the argument if they do not want the
* argument modified. The return is the original pointer, allowing this * argument modified. The return is the original pointer, allowing this
* function to be easily used as arguments to other functions. * function to be easily used as arguments to other functions.
* *
@@ -1725,9 +1725,7 @@ static int checkPerm(const char *path, const char *request)
if (strcmp(p, request) == 0) { if (strcmp(p, request) == 0) {
set_remoteuser_var: set_remoteuser_var:
remoteuser = strdup(request); remoteuser = xstrndup(request, u - request);
if (remoteuser)
remoteuser[u - request] = '\0';
return 1; /* Ok */ return 1; /* Ok */
} }
/* unauthorized */ /* unauthorized */
@@ -1990,13 +1988,13 @@ static void handle_incoming_and_exit(const len_and_sockaddr *fromAddr)
#endif #endif
#if ENABLE_FEATURE_HTTPD_CGI #if ENABLE_FEATURE_HTTPD_CGI
else if (STRNCASECMP(iobuf, "Cookie:") == 0) { else if (STRNCASECMP(iobuf, "Cookie:") == 0) {
cookie = strdup(skip_whitespace(iobuf + sizeof("Cookie:")-1)); cookie = xstrdup(skip_whitespace(iobuf + sizeof("Cookie:")-1));
} else if (STRNCASECMP(iobuf, "Content-Type:") == 0) { } else if (STRNCASECMP(iobuf, "Content-Type:") == 0) {
content_type = strdup(skip_whitespace(iobuf + sizeof("Content-Type:")-1)); content_type = xstrdup(skip_whitespace(iobuf + sizeof("Content-Type:")-1));
} else if (STRNCASECMP(iobuf, "Referer:") == 0) { } else if (STRNCASECMP(iobuf, "Referer:") == 0) {
referer = strdup(skip_whitespace(iobuf + sizeof("Referer:")-1)); referer = xstrdup(skip_whitespace(iobuf + sizeof("Referer:")-1));
} else if (STRNCASECMP(iobuf, "User-Agent:") == 0) { } else if (STRNCASECMP(iobuf, "User-Agent:") == 0) {
user_agent = strdup(skip_whitespace(iobuf + sizeof("User-Agent:")-1)); user_agent = xstrdup(skip_whitespace(iobuf + sizeof("User-Agent:")-1));
} }
#endif #endif
#if ENABLE_FEATURE_HTTPD_BASIC_AUTH #if ENABLE_FEATURE_HTTPD_BASIC_AUTH
@@ -2377,7 +2375,7 @@ int httpd_main(int argc ATTRIBUTE_UNUSED, char **argv)
* Besides, it is also smaller. */ * Besides, it is also smaller. */
{ {
char *p = getenv("PATH"); char *p = getenv("PATH");
/* env strings themself are not freed, no need to strdup(p): */ /* env strings themself are not freed, no need to xstrdup(p): */
clearenv(); clearenv();
if (p) if (p)
putenv(p - 5); putenv(p - 5);

View File

@@ -78,10 +78,10 @@ static void read_config(char **pc, int npc, char **fc, int nfc)
section = 2; section = 2;
} else { } else {
if (section == 1 && pc_ofs < npc -1) { if (section == 1 && pc_ofs < npc -1) {
pc[pc_ofs++] = strdup(buf); pc[pc_ofs++] = xstrdup(buf);
pc[pc_ofs] = NULL; pc[pc_ofs] = NULL;
} else if (section == 2 && fc_ofs < nfc - 1) { } else if (section == 2 && fc_ofs < nfc - 1) {
fc[fc_ofs++] = strdup(buf); fc[fc_ofs++] = xstrdup(buf);
fc[fc_ofs] = NULL; fc[fc_ofs] = NULL;
} }
} }