This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: cpplib: patch: three long-standing bugs


Zack Weinberg wrote:
> 3) xstrdup does need to be modified, but not the way you did it.  The
> proper change is
> 
> @@ -74,8 +74,8 @@ char *
>  xstrdup (input)
>    const char *input;
>  {
> -  unsigned size = strlen (input);
> -  char *output = xmalloc (size + 1);
> -  strcpy (output, input);
> +  size_t size = strlen (input) + 1;
> +  char *output = xmalloc (size);
> +  memcpy (output, input, size);
>    return output;
>  }
> 
> Do you see why this version is better?

Yes.

cpplib code has to be portable, right?  I don't think all compilers can
call functions from variable declarations IIRC.

Thanks for the feedback.

	Jeff




-- 
Custom driver development	|    Never worry about theory as long
Open source programming		|    as the machinery does what it's
				|    supposed to do.  -- R. A. Heinlein


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]