Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/string Arrange that strerror(-1) prints "Unknown er...



details:   https://anonhg.NetBSD.org/src/rev/32ab444115f7
branches:  trunk
changeset: 1008517:32ab444115f7
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Mar 25 16:15:41 2020 +0000

description:
Arrange that strerror(-1) prints "Unknown error: -1" and not the
unsigned equivalent of -1.

While here, guarantee, even when !NLS, that nothing here (not even
snprintf deciding to complain about EILSEQ or something) can ever
alter errno (ie: always save and restore it, not only in the NLS case).
The functions here must never alter errno, whatever happens.

diffstat:

 lib/libc/string/strerror_r.c |  16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)

diffs (64 lines):

diff -r eaf26f898039 -r 32ab444115f7 lib/libc/string/strerror_r.c
--- a/lib/libc/string/strerror_r.c      Wed Mar 25 16:10:17 2020 +0000
+++ b/lib/libc/string/strerror_r.c      Wed Mar 25 16:15:41 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: strerror_r.c,v 1.4 2017/01/10 20:25:48 christos Exp $  */
+/*     $NetBSD: strerror_r.c,v 1.5 2020/03/25 16:15:41 kre Exp $       */
 
 /*
  * Copyright (c) 1988 Regents of the University of California.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: strerror_r.c,v 1.4 2017/01/10 20:25:48 christos Exp $");
+__RCSID("$NetBSD: strerror_r.c,v 1.5 2020/03/25 16:15:41 kre Exp $");
 
 #include "namespace.h"
 #include <assert.h>
@@ -53,12 +53,12 @@
 int
 _strerror_lr(int num, char *buf, size_t buflen, locale_t loc)
 {
-#define        UPREFIX "Unknown error: %u"
+#define        UPREFIX "Unknown error: %d"
        unsigned int errnum = num;
        int retval = 0;
        size_t slen;
+       int saved_errno = errno;
 #ifdef NLS
-       int saved_errno = errno;
        nl_catd catd;
        catd = catopen_l("libc", NL_CAT_LOCALE, loc);
 #endif
@@ -66,7 +66,7 @@
 
        if (errnum < (unsigned int) sys_nerr) {
 #ifdef NLS
-               slen = strlcpy(buf, catgets(catd, 1, (int)errnum,
+               slen = strlcpy(buf, catgets(catd, 1, num,
                    sys_errlist[errnum]), buflen); 
 #else
                slen = strlcpy(buf, sys_errlist[errnum], buflen); 
@@ -74,9 +74,9 @@
        } else {
 #ifdef NLS
                slen = snprintf_l(buf, buflen, loc,
-                   catgets(catd, 1, 0xffff, UPREFIX), errnum);
+                   catgets(catd, 1, 0xffff, UPREFIX), num);
 #else
-               slen = snprintf(buf, buflen, UPREFIX, errnum);
+               slen = snprintf(buf, buflen, UPREFIX, num);
 #endif
                retval = EINVAL;
        }
@@ -86,8 +86,8 @@
 
 #ifdef NLS
        catclose(catd);
+#endif
        errno = saved_errno;
-#endif
 
        return retval;
 }



Home | Main Index | Thread Index | Old Index