strerror_r questions

Eric Blake eblake@redhat.com
Thu Feb 10 16:48:00 GMT 2011


On 02/10/2011 09:16 AM, Eric Blake wrote:
>> That's not how I understand the sentence
>>
>>   If the function stores a string in buf, then at most buflen bytes are
>>   stored (the string may  be  truncated  if buflen  is too small) and
>>   the string always includes a terminating null byte.
> 
> Glibc documentation is ambiguous - first it states that if n is too
> small, then buf is not modified nor returned; then it states that if n
> is too small, buf gets a truncated string.  An actual test program
> proves the point:
> 
> $ cat foo.c
> #define _GNU_SOURCE 1
> #include <stdio.h>
> #include <string.h>
> #include <errno.h>
> int main (void) {
>   char buf[80] = "Hello world";
>   char *p = strerror_r (1, buf, 4);
>   printf ("buf=%s\np=%s\n", buf, p);
>   return 0;
> }
> $ ./foo
> buf=Hello world
> p=Operation not permitted
> 
> I'll be submitting a bug report to the Linux man-pages project.

Or not; change 1 to -1 in the above program, and the output becomes:

buf=Unk
p=Unk

That is, glibc's formatting of "Unknown error -1" is done into the
provided buffer, and not into temporary thread-safe storage, and is
indeed truncated in that case.

-- 
Eric Blake   eblake@redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://sourceware.org/pipermail/newlib/attachments/20110210/bf8c1ce0/attachment.sig>


More information about the Newlib mailing list