Replace malloc with xmalloc

* argv.c (dupargv): Replace malloc with xmalloc.  Don't check
	xmalloc return.
	(buildargv): Likewise.  Also replace strdup with xstrdup.
	(expandargv): Don't check dupargv return.

From-SVN: r190767
This commit is contained in:
H.J. Lu 2012-08-29 01:03:21 +00:00 committed by H.J. Lu
parent 55529d369c
commit fabfa16b37
2 changed files with 16 additions and 38 deletions

View File

@ -1,3 +1,10 @@
2011-08-28 H.J. Lu <hongjiu.lu@intel.com>
* argv.c (dupargv): Replace malloc with xmalloc. Don't check
xmalloc return.
(buildargv): Likewise. Also replace strdup with xstrdup.
(expandargv): Don't check dupargv return.
2011-08-28 H.J. Lu <hongjiu.lu@intel.com> 2011-08-28 H.J. Lu <hongjiu.lu@intel.com>
PR binutils/14526 PR binutils/14526

View File

@ -1,5 +1,5 @@
/* Create and destroy argument vectors (argv's) /* Create and destroy argument vectors (argv's)
Copyright (C) 1992, 2001, 2010 Free Software Foundation, Inc. Copyright (C) 1992, 2001, 2010, 2012 Free Software Foundation, Inc.
Written by Fred Fish @ Cygnus Support Written by Fred Fish @ Cygnus Support
This file is part of the libiberty library. This file is part of the libiberty library.
@ -72,20 +72,13 @@ dupargv (char **argv)
/* the vector */ /* the vector */
for (argc = 0; argv[argc] != NULL; argc++); for (argc = 0; argv[argc] != NULL; argc++);
copy = (char **) malloc ((argc + 1) * sizeof (char *)); copy = (char **) xmalloc ((argc + 1) * sizeof (char *));
if (copy == NULL)
return NULL;
/* the strings */ /* the strings */
for (argc = 0; argv[argc] != NULL; argc++) for (argc = 0; argv[argc] != NULL; argc++)
{ {
int len = strlen (argv[argc]); int len = strlen (argv[argc]);
copy[argc] = (char *) malloc (len + 1); copy[argc] = (char *) xmalloc (len + 1);
if (copy[argc] == NULL)
{
freeargv (copy);
return NULL;
}
strcpy (copy[argc], argv[argc]); strcpy (copy[argc], argv[argc]);
} }
copy[argc] = NULL; copy[argc] = NULL;
@ -149,7 +142,7 @@ remains unchanged. The last element of the vector is followed by a
@code{NULL} element. @code{NULL} element.
All of the memory for the pointer array and copies of the string All of the memory for the pointer array and copies of the string
is obtained from @code{malloc}. All of the memory can be returned to the is obtained from @code{xmalloc}. All of the memory can be returned to the
system with the single function call @code{freeargv}, which takes the system with the single function call @code{freeargv}, which takes the
returned result of @code{buildargv}, as it's argument. returned result of @code{buildargv}, as it's argument.
@ -205,21 +198,12 @@ char **buildargv (const char *input)
if (argv == NULL) if (argv == NULL)
{ {
maxargc = INITIAL_MAXARGC; maxargc = INITIAL_MAXARGC;
nargv = (char **) malloc (maxargc * sizeof (char *)); nargv = (char **) xmalloc (maxargc * sizeof (char *));
} }
else else
{ {
maxargc *= 2; maxargc *= 2;
nargv = (char **) realloc (argv, maxargc * sizeof (char *)); nargv = (char **) xrealloc (argv, maxargc * sizeof (char *));
}
if (nargv == NULL)
{
if (argv != NULL)
{
freeargv (argv);
argv = NULL;
}
break;
} }
argv = nargv; argv = nargv;
argv[argc] = NULL; argv[argc] = NULL;
@ -284,13 +268,7 @@ char **buildargv (const char *input)
} }
} }
*arg = EOS; *arg = EOS;
argv[argc] = strdup (copybuf); argv[argc] = xstrdup (copybuf);
if (argv[argc] == NULL)
{
freeargv (argv);
argv = NULL;
break;
}
argc++; argc++;
argv[argc] = NULL; argv[argc] = NULL;
@ -457,14 +435,7 @@ expandargv (int *argcp, char ***argvp)
file_argv = buildargv (buffer); file_argv = buildargv (buffer);
/* If *ARGVP is not already dynamically allocated, copy it. */ /* If *ARGVP is not already dynamically allocated, copy it. */
if (!argv_dynamic) if (!argv_dynamic)
{ *argvp = dupargv (*argvp);
*argvp = dupargv (*argvp);
if (!*argvp)
{
fputs ("\nout of memory\n", stderr);
xexit (1);
}
}
/* Count the number of arguments. */ /* Count the number of arguments. */
file_argc = 0; file_argc = 0;
while (file_argv[file_argc]) while (file_argv[file_argc])