unnecessary use of stpncpy

Bruno Haible haible@ilog.fr
Mon Aug 28 02:55:00 GMT 2000


stpncpy has the same bad behaviour as strncpy: It clears the rest of the
buffer past the result, regardless how large. Since it is common to pass
large buffers to the functions strxfrm(), wcsxfrm(), getdomainname(),
strerror(), and the result normally is much smaller than the buffer, here
is an optimization: don't clear the rest of the buffer.


2000-08-27  Bruno Haible  <haible@clisp.cons.org>

	* string/strxfrm.c (strxfrm, wcsxfrm): Include <sys/param.h>.
	If nrules == 0 and srclen < n, copy only srclen + 1 characters.

	* sysdeps/generic/getdomain.c (getdomainname): Include <sys/param.h>.
	If the result is fits in the buffer, copy only as many bytes as needed.

	* sysdeps/generic/_strerror.c (__strerror_r): Don't zero-fill the
	buffer after copying numbuf into it.
	* sysdeps/mach/_strerror.c (__strerror_r): Likewise.

*** glibc-20000826/string/strxfrm.c.bak	Tue Aug  1 13:53:18 2000
--- glibc-20000826/string/strxfrm.c	Sat Aug 26 18:46:44 2000
***************
*** 22,27 ****
--- 22,28 ----
  #include <stdint.h>
  #include <stdlib.h>
  #include <string.h>
+ #include <sys/param.h>
  
  #ifndef STRING_TYPE
  # define STRING_TYPE char
***************
*** 124,130 ****
    if (nrules == 0)
      {
        if (n != 0)
! 	STPNCPY (dest, src, n);
  
        return srclen;
      }
--- 125,131 ----
    if (nrules == 0)
      {
        if (n != 0)
! 	STPNCPY (dest, src, MIN (srclen + 1, n));
  
        return srclen;
      }
*** glibc-20000826/sysdeps/generic/getdomain.c.bak	Tue Sep  7 17:10:12 1999
--- glibc-20000826/sysdeps/generic/getdomain.c	Sat Aug 26 18:46:44 2000
***************
*** 1,4 ****
! /* Copyright (C) 1994, 1995, 1997 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
  
     The GNU C Library is free software; you can redistribute it and/or
--- 1,4 ----
! /* Copyright (C) 1994, 1995, 1997, 2000 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
  
     The GNU C Library is free software; you can redistribute it and/or
***************
*** 22,27 ****
--- 22,28 ----
  
  #include <errno.h>
  #include <unistd.h>
+ #include <sys/param.h>
  #include <sys/utsname.h>
  #include <string.h>
  
***************
*** 34,44 ****
      size_t len;
  {
    struct utsname u;
  
    if (uname (&u) < 0)
      return -1;
  
!   strncpy (name, u.domainname, len);
    return 0;
  }
  
--- 35,47 ----
      size_t len;
  {
    struct utsname u;
+   size_t u_len;
  
    if (uname (&u) < 0)
      return -1;
  
!   u_len = strlen (u.domainname);
!   memcpy (name, u.domainname, MIN (u_len + 1, len));
    return 0;
  }
  
*** glibc-20000826/sysdeps/generic/_strerror.c.bak	Tue Sep  7 17:09:42 1999
--- glibc-20000826/sysdeps/generic/_strerror.c	Sat Aug 26 18:46:44 2000
***************
*** 1,4 ****
! /* Copyright (C) 1991, 93, 95, 96, 97, 98 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
  
     The GNU C Library is free software; you can redistribute it and/or
--- 1,4 ----
! /* Copyright (C) 1991, 93, 95, 96, 97, 98, 2000 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
  
     The GNU C Library is free software; you can redistribute it and/or
***************
*** 56,62 ****
  	 buffer size.  */
        q = __mempcpy (buf, unk, MIN (unklen, buflen));
        if (unklen < buflen)
! 	__stpncpy (q, p, buflen - unklen);
  
        /* Terminate the string in any case.  */
        if (buflen > 0)
--- 56,62 ----
  	 buffer size.  */
        q = __mempcpy (buf, unk, MIN (unklen, buflen));
        if (unklen < buflen)
! 	memcpy (q, p, MIN (&numbuf[21] - p, buflen - unklen));
  
        /* Terminate the string in any case.  */
        if (buflen > 0)
*** glibc-20000826/sysdeps/mach/_strerror.c.bak	Tue Sep  7 17:19:19 1999
--- glibc-20000826/sysdeps/mach/_strerror.c	Sat Aug 26 18:46:44 2000
***************
*** 1,4 ****
! /* Copyright (C) 1993, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
     This file is part of the GNU C Library.
  
     The GNU C Library is free software; you can redistribute it and/or
--- 1,5 ----
! /* Copyright (C) 1993, 1995, 1996, 1997, 1998, 2000
!    Free Software Foundation, Inc.
     This file is part of the GNU C Library.
  
     The GNU C Library is free software; you can redistribute it and/or
***************
*** 65,71 ****
  	 buffer size.  */
        q = __mempcpy (buf, unk, MIN (unklen, buflen));
        if (unklen < buflen)
! 	__stpncpy (q, p, buflen - unklen);
  
        /* Terminate the string in any case.  */
        if (buflen > 0)
--- 66,72 ----
  	 buffer size.  */
        q = __mempcpy (buf, unk, MIN (unklen, buflen));
        if (unklen < buflen)
! 	memcpy (q, p, MIN (&numbuf[21] - p, buflen - unklen));
  
        /* Terminate the string in any case.  */
        if (buflen > 0)
***************
*** 103,109 ****
  	    {
  	      *q++ = ' ';
  	      if (unklen + len + 1 < buflen)
! 		__stpncpy (q, p, buflen - unklen - len - 1);
  	    }
  	}
  
--- 104,111 ----
  	    {
  	      *q++ = ' ';
  	      if (unklen + len + 1 < buflen)
! 		memcpy (q, p,
! 			MIN (&numbuf[21] - p, buflen - unklen - len - 1));
  	    }
  	}
  


More information about the Libc-alpha mailing list