[Bug libc/364] strerror_r does not copy error string to user supplied buffer

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[Bug libc/364] strerror_r does not copy error string to user supplied buffer

tim@mr-dog.net

------- Additional Comments From vda dot linux at googlemail dot com  2006-07-04 11:22 -------
glibc 2.4 include/string.h says:

#if defined __USE_XOPEN2K || defined __USE_MISC
/* Reentrant version of `strerror'.
   There are 2 flavors of `strerror_r', GNU which returns the string
   and may or may not use the supplied temporary buffer and POSIX one
   which fills the string into the buffer.
   To use the POSIX version, -D_XOPEN_SOURCE=600 or -D_POSIX_C_SOURCE=200112L
   without -D_GNU_SOURCE is needed, otherwise the GNU version is
   preferred.  */
# if defined __USE_XOPEN2K && !defined __USE_GNU
/* Fill BUF with a string describing the meaning of the `errno' code in
   ERRNUM.  */
#  ifdef __REDIRECT_NTH
extern int __REDIRECT_NTH (strerror_r,
                           (int __errnum, char *__buf, size_t __buflen),
                           __xpg_strerror_r) __nonnull ((2));
#  else
extern int __xpg_strerror_r (int __errnum, char *__buf, size_t __buflen)
     __THROW __nonnull ((2));
#   define strerror_r __xpg_strerror_r
#  endif
# else
/* If a temporary buffer is required, at most BUFLEN bytes of BUF will be
   used.  */
extern char *strerror_r (int __errnum, char *__buf, size_t __buflen)
     __THROW __nonnull ((2));
# endif
#endif

Manpage says the same. However the testcase below shows that
POSIX version of strerr_r is selected _without_ -D_XOPEN_SOURCE=600
or -D_POSIX_C_SOURCE=200112L:

#include <errno.h>
#include <string.h>
#include <stdio.h>
main()
{
  int i;
  char buf[1024] = "junk";
  i = strerror_r(ERANGE, buf, 5);
  printf("%d:%s\n",i,buf);
  i = strerror_r(ERANGE, buf, 1024);
  printf("%d:%s\n",i,buf);
  return 0;
}

See? It returns int, not char*.

# ./a.out
-1:junk
0:Numerical result out of range


--
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |


http://sourceware.org/bugzilla/show_bug.cgi?id=364

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.