Although POSIX permits 'buf' to remain untouched on error, it does not require this; in fact, it recommends that buf still be populated insofar as possible for a better quality of implementation (since applications are likely to use the contents of buffer without checking for errors). Furthermore, this is more in line with what glibc does for GNU strerror_r (a useful result at all costs, even if the result had to be truncated): XSH line 63300: "Similarly, implementations are encouraged to have strerror_r( ) return [EINVAL] and put a string like "unknown error" or "error number xxx" in the buffer pointed to by strerrbuf when the value of errnum is not a valid error number." and http://austingroupbugs.net/view.php?id=398: "Additionally, implementations are encouraged to NUL-terminate strerrbuf when failing with ERANGE for any size other than buflen of zero." If you use the example program of http://sourceware.org/bugzilla/show_bug.cgi?id=12204, then I argue that the results on glibc should be: result: failure 22 Unknown error 18446744073709551614 result: failure 34 Pe rather than Bruno's claim of result: failure 22 ______ result: failure 34 ______ Additionally, that bug showed prior art of other platforms that (almost) have this QoI issue resolved according to POSIX recommendations: FreeBSD 6.4 (albeit buggy for returning EINVAL on 0), AIX 6.1 (albeit buggy for not returning ERANGE on short buffer), Tru64 5.1 (albeit buggy for wrong return values).
Created attachment 5736 [details] proposed patch
I've applied a simpler patch.
The new code at http://sourceware.org/git/?p=glibc.git;a=blob;f=string/xpg-strerror.c;h=00256c3dac2591e99b504c97b05945e27aefecce;hb=7ea72f99966a65a56aedba817ee2413ff9b1f23c has three flaws: - It overwrites buf[0] when buflen == 0. This is an out-of-bounds access. - If errnum is invalid, it wastes time calling __strerror_r and strlen before then returning EINVAL and ignoring the result of __strerror_r. - It includes <libintl.h> and <locale.h> and defines dgettext(), although these are not needed any more, because the localization is now done by __strerror_r.
The first and third reported flaw were fixed by Ulrich in http://sourceware.org/git/?p=glibc.git;a=commitdiff;h=adcd5c15d2a37794d021104160b425ff61f88219 For the second one, see bug 14317.