cpplib: patch: three long-standing bugs

Jeff Garzik jgarzik@pobox.com
Sat Sep 4 20:24:00 GMT 1999


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


More information about the Gcc-patches mailing list