Skip to contentSkip to conversation

strerror: improve strerror_r description

PATCH In review Eric Blake 3 participants 7 messages Mar 17, 2011 – Apr 22, 2012 5 messages not in the archive Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 strerror: improve strerror_r description

---

POSIX requires that perror() not modify the static storage returned by strerror(). POSIX 2008 and C99 both require that strerror() never return NULL (a strerror() that always returns "" for all inputs is valid for C99, but not for POSIX).

http://sourceware.org/bugzilla/show_bug.cgi?id=12204
documents glibc's change to come into compliance with POSIX regarding strerror_r return value. The GNU strerror_r use of buf was confusing - I ended up writing a test program that proves that buf is unused for valid errnum, but contains truncated "unknown message" for out-of-range errnum.

See also http://austingroupbugs.net/view.php?id=382

 man3/strerror.3 |   18 +++++++++++-------
 1 files changed, 11 insertions(+), 7 deletions(-)
man3/strerror.3 +11 -7
index 37fc95d..8928a1e 100644
@@ -71,10 +71,10 @@ code passed in the argument \fIerrnum\fP, possibly using the
 part of the current locale to select the appropriate language.
 This string must not be modified by the application, but may be
 modified by a subsequent call to
-.BR perror (3)
-or
 .BR strerror ().
-No library function will modify this string.
+No other library function, including
+.BR perror (3),
+will modify this string.
 
 The
 .BR strerror_r ()
@@ -84,7 +84,7 @@ but is
 thread safe.
 This function is available in two versions:
 an XSI-compliant version specified in POSIX.1-2001
-(available since glibc 2.3.4),
+(available since glibc 2.3.4, but not compliant until glibc 2.13),
 and a GNU-specific version (available since glibc 2.0).
 The XSI-compliant version is provided with the feature test macros
 settings shown in the SYNOPSIS;
@@ -120,7 +120,9 @@ then at most
 .I buflen
 bytes are stored (the string may be truncated if
 .I buflen
-is too small) and the string always includes a terminating null byte.
+is too small and
+.I errnum
+is unknown).  The string always includes a terminating null byte.
 .SH "RETURN VALUE"
 The
 .BR strerror ()
@@ -133,9 +135,10 @@ or an "Unknown error nnn" message if the error number is unknown.
 The XSI-compliant
 .BR strerror_r ()
 function returns 0 on success;
-on error, \-1 is returned and
+on error, glibc 2.13 returns a positive error number, while
+older versions returned \-1 and set
 .I errno
-is set to indicate the error.
+indicate the error instead.
 .SH ERRORS
 .TP
 .B EINVAL
@@ -165,6 +168,7 @@ On some systems,
 .\" e.g., Solaris 8, HP-UX 11
 .BR strerror ()
 returns NULL if the error number is unknown.
+POSIX.1-2008 requires the result to be non-NULL for all inputs.
 On other systems,
 .\" e.g., FreeBSD 5.4, Tru64 5.1B
 .BR strerror ()
Signature · 6 lines
1.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#2 lore raw
In reply to a message not in the archive

Hi,

Quoted 1 line · Eric Blake

[...]

Quoted 7 lines

http://sourceware.org/bugzilla/show_bug.cgi?id=12204
documents glibc's change to come into compliance with POSIX regarding strerror_r return value.  The GNU strerror_r use of buf was confusing - I ended up writing a test program that proves that buf is unused for valid errnum, but contains truncated "unknown message" for out-of-range errnum.

Yeah, the only use of the buffer is to format "unknown error: %d" when the errno is out of range - in all other cases it returns a constant string (e.g. "Resource temporary unavailable"). strerror() behaves in a similar way, and IIRC only when errno is out of bounds, strerror() is not thread-safe on Linux - at least it was the last time I checked.

I did point this out in a glibc bug, but the reception was not that warm :). I guess it's too late to change things at this point. OTOH, this also makes using strerror/strerror_r portably more interesting.

Quoted 52 lines

See also http://austingroupbugs.net/view.php?id=382

 man3/strerror.3 |   18 +++++++++++-------

 1 files changed, 11 insertions(+), 7 deletions(-)
man3/strerror.3 +0 -0
index 37fc95d..8928a1e 100644
@@ -71,10 +71,10 @@ code passed in the argument \fIerrnum\fP, possibly using the

 part of the current locale to select the appropriate language. This string must not be modified by the application, but may be modified by a subsequent call to
-.BR perror (3)
-or
 .BR strerror ().

man3/strerror.3 +3 -1
-No library function will modify this string.
+No other library function, including
+.BR perror (3),
+will modify this string.

 The
 .BR strerror_r ()

man3/strerror.3 +0 -0
@@ -84,7 +84,7 @@ but is

 thread safe.
 This function is available in two versions:
 an XSI-compliant version specified in POSIX.1-2001

man3/strerror.3 +1 -1
-(available since glibc 2.3.4),
+(available since glibc 2.3.4, but not compliant until glibc 2.13),

 and a GNU-specific version (available since glibc 2.0). The XSI-compliant version is provided with the feature test macros settings shown in the SYNOPSIS;

man3/strerror.3 +0 -0
@@ -120,7 +120,9 @@ then at most

 .I buflen
 bytes are stored (the string may be truncated if
 .I buflen

man3/strerror.3 +3 -1
-is too small) and the string always includes a terminating null byte.
+is too small and
+.I errnum
+is unknown).  The string always includes a terminating null byte.

 .SH "RETURN VALUE"
 The
 .BR strerror ()

man3/strerror.3 +0 -0
@@ -133,9 +135,10 @@ or an "Unknown error nnn" message if the error number is unknown.

 The XSI-compliant
 .BR strerror_r ()
 function returns 0 on success;

man3/strerror.3 +2 -1
-on error, \-1 is returned and
+on error, glibc 2.13 returns a positive error number, while
+older versions returned \-1 and set

 .I errno

man3/strerror.3 or an "Unknown error nnn" message if the error number is unknown. · line 141

Quoted 2 lines
man3/strerror.3 +1 -1
-is set to indicate the error.
+indicate the error instead.

to indicate the error instead?

Quoted 8 lines

 .SH ERRORS
 .TP
 .B EINVAL

man3/strerror.3 +0 -0
@@ -165,6 +168,7 @@ On some systems,

 .\" e.g., Solaris 8, HP-UX 11
 .BR strerror ()
 returns NULL if the error number is unknown. +POSIX.1-2008 requires the result to be non-NULL for all inputs.

Maybe you could mention C99 as well, since you were saying it also mandates this behavior?

Otherwise, the patch looks ok to me.

Quoted 11 lines

 On other systems,
 .\" e.g., FreeBSD 5.4, Tru64 5.1B
 .BR strerror ()
--
1.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#4 lore raw
In reply to a message not in the archive
Quoted 11 lines · Stefan Puiu
Quoted 1 line

[...]

Quoted 7 lines

http://sourceware.org/bugzilla/show_bug.cgi?id=12204
documents glibc's change to come into compliance with POSIX regarding strerror_r return value. The GNU strerror_r use of buf was confusing - I ended up writing a test program that proves that buf is unused for valid errnum, but contains truncated "unknown message" for out-of-range errnum.

Quoted 7 lines

man3/strerror.3 or an "Unknown error nnn" message if the error number is unknown. · line 141

Quoted 5 lines
man3/strerror.3 +3 -1
+on error, glibc 2.13 returns a positive error number, while
+older versions returned \-1 and set
 .I errno
-is set to indicate the error.
+indicate the error instead.

to indicate the error instead?

Yes.

Quoted 12 lines

man3/strerror.3 On some systems, · line 171

Quoted 9 lines

.SH ERRORS
.TP
.B EINVAL

man3/strerror.3 +1 -0
@@ -165,6 +168,7 @@ On some systems,
 .\" e.g., Solaris 8, HP-UX 11
 .BR strerror ()
 returns NULL if the error number is unknown.
+POSIX.1-2008 requires the result to be non-NULL for all inputs.

Maybe you could mention C99 as well, since you were saying it also mandates this behavior?

Would make sense. Do you need me to resubmit with those corrections, or can you squash it in yourself when applying?

Quoted 1 line

Otherwise, the patch looks ok to me.

Signature · 2 lines
Eric Blake   [email protected]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org
#6 lore raw
In reply to a message not in the archive
Quoted 5 lines · Eric Blake
Quoted 2 lines · Stefan Puiu
Quoted 1 line

[...]

Would make sense.  Do you need me to resubmit with those corrections, or can you squash it in yourself when applying?

I don't have write access to man-pages, so I guess it's better if you resubmit.

Thanks,
Stefan.
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw
In reply to a message not in the archive

Eric,

Quoted 35 lines · Eric Blake
Quoted 11 lines · Stefan Puiu
Quoted 1 line

[...]

Quoted 7 lines

http://sourceware.org/bugzilla/show_bug.cgi?id=12204
documents glibc's change to come into compliance with POSIX regarding strerror_r return value.  The GNU strerror_r use of buf was confusing - I ended up writing a test program that proves that buf is unused for valid errnum, but contains truncated "unknown message" for out-of-range errnum.

Quoted 7 lines

Review

Quoted 5 lines
(quoted diff) +2 -0
+on error, glibc 2.13 returns a positive error number, while
+older versions returned \-1 and set

 .I errno

(quoted diff) +1 -1
-is set to indicate the error.
+indicate the error instead.

to indicate the error instead?

Yes.

Quoted 12 lines
Quoted 9 lines

 .SH ERRORS
 .TP
 .B EINVAL

(quoted diff) +0 -0
@@ -165,6 +168,7 @@ On some systems,

 .\" e.g., Solaris 8, HP-UX 11
 .BR strerror ()
 returns NULL if the error number is unknown. +POSIX.1-2008 requires the result to be non-NULL for all inputs.

Maybe you could mention C99 as well, since you were saying it also mandates this behavior?

Quoted 4 lines

Would make sense.  Do you need me to resubmit with those corrections, or can you squash it in yourself when applying?

Quoted 1 line

Otherwise, the patch looks ok to me.

Thanks for this patch. I happened to apply another fix from Bernhard Walle just beforehand, so modified your patch slightly (and included Stefan's input), to the following:

=====

man3/strerror.3 +8 -5
@@ -71,10 +71,10 @@ code passed in the argument \fIerrnum\fP, possibly using the
 part of the current locale to select the appropriate language.
 This string must not be modified by the application, but may be
 modified by a subsequent call to
-.BR perror (3)
-or
 .BR strerror ().
-No library function will modify this string.
+No library function, including
+.BR perror (3),
+will modify this string.
 
 The
 .BR strerror_r ()
@@ -84,7 +84,7 @@ but is
 thread safe.
 This function is available in two versions:
 an XSI-compliant version specified in POSIX.1-2001
-(available since glibc 2.3.4),
+(available since glibc 2.3.4, but not POSIX-compliant until glibc 2.13),
 and a GNU-specific version (available since glibc 2.0).
 The XSI-compliant version is provided with the feature test macros
 settings shown in the SYNOPSIS;
@@ -120,7 +120,10 @@ then at most
 .I buflen
 bytes are stored (the string may be truncated if
 .I buflen
-is too small) and the string always includes a terminating null byte.
+is too small and
+.I errnum
+is unknown).
+The string always includes a terminating null byte.
 .SH "RETURN VALUE"
 The
 .BR strerror ()
@@ -132,8 +135,12 @@ or an "Unknown error nnn" message if the error

number is unknown.

The XSI-compliant
.BR strerror_r ()
-function returns 0 on success;
-on error, a (positive) error number is returned. +function returns 0 on success.
+On error,
+a (positive) error number is returned (since glibc 2.13), +or \-1 is returned and
+.I errno
+is set to indicate the error (glibc versions before 2.13).
.SH ERRORS
.TP
.B EINVAL
@@ -171,6 +178,7 @@ returns a string something like "Error nnn occurred" and sets
to
.B EINVAL
if the error number is unknown.
+C99 and POSIX.1-2008 require the return value to be non-NULL.
.SH "SEE ALSO"
.BR err (3),
.BR errno (3),
=====

In addition to the above, I added the following, and I'd be happy if you could ack it.

=====

man3/strerror.3 +13 -0
@@ -133,6 +133,19 @@ functions return
 the appropriate error description string,
 or an "Unknown error nnn" message if the error number is unknown.
 
+POSIX.1-2001 and POSIX.1-2008 require that a successful call to
+.BR strerror (3)
+shall leave
+.I errno
+unchanged, and note that,
+since no function return value is reserved to indicate an error,
+an application that wishes to check for errors should initialize
+.I errno
+to zero before the call,
+and then check
+.I errno
+after the call.
+
 The XSI-compliant
 .BR strerror_r ()
 function returns 0 on success.

=====

These changes will be in man-pages-3.40.

Cheers,

Michael

Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Author of "The Linux Programming Interface"; http://man7.org/tlpi/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#9 lore raw
In reply to a message not in the archive
Quoted 29 lines · Michael Kerrisk (man-pages)

Eric,

In addition to the above, I added the following, and I'd be happy if you could ack it.

=====

man3/strerror.3 +13 -0
@@ -133,6 +133,19 @@ functions return
 the appropriate error description string,
 or an "Unknown error nnn" message if the error number is unknown.
 
+POSIX.1-2001 and POSIX.1-2008 require that a successful call to
+.BR strerror (3)
+shall leave
+.I errno
+unchanged, and note that,
+since no function return value is reserved to indicate an error,
+an application that wishes to check for errors should initialize
+.I errno
+to zero before the call,
+and then check
+.I errno
+after the call.
+
 The XSI-compliant
 .BR strerror_r ()
 function returns 0 on success.
Quoted 1 line

=====

Correct.

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#11 lore raw
In reply to a message not in the archive
Quoted 23 lines · Eric Blake
Quoted 22 lines · Michael Kerrisk (man-pages)

Eric,

In addition to the above, I added the following, and I'd be happy if you could ack it.

=====

man3/strerror.3 +0 -0
@@ -133,6 +133,19 @@ functions return
  the appropriate error description string,

 or an "Unknown error nnn" message if the error number is unknown.

man3/strerror.3 +9 -0
+POSIX.1-2001 and POSIX.1-2008 require that a successful call to
+.BR strerror (3)
+shall leave
+.I errno
+unchanged, and note that,
+since no function return value is reserved to indicate an error,
+an application that wishes to check for errors should initialize
+.I errno
+to zero before the call,
Quoted 8 lines
Quoted 8 lines
man3/strerror.3 +4 -0
+and then check
+.I errno
+after the call.
+

 The XSI-compliant
 .BR strerror_r ()
 function returns 0 on success.
=====

Thanks Eric!

Signature · 7 lines
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Author of "The Linux Programming Interface"; http://man7.org/tlpi/
--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html