Source-Changes-HG archive

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

[src/trunk]: src/lib/libc reallocarr returns errno. preserve it.



details:   https://anonhg.NetBSD.org/src/rev/0beaf94e8a5f
branches:  trunk
changeset: 363445:0beaf94e8a5f
user:      christos <christos%NetBSD.org@localhost>
date:      Sat Mar 12 17:31:39 2022 +0000

description:
reallocarr returns errno. preserve it.

diffstat:

 lib/libc/gen/fts.c           |  11 ++++++-----
 lib/libc/gen/scandir.c       |  15 +++++++++++----
 lib/libc/gen/setmode.3       |   6 +++---
 lib/libc/gen/setmode.c       |  16 +++++++---------
 lib/libc/gen/stringlist.c    |  10 ++++++----
 lib/libc/net/getprotoent_r.c |  30 ++++++++++++++++--------------
 lib/libc/net/getservent_r.c  |  16 +++++++---------
 lib/libc/stdio/vfwprintf.c   |  22 ++++++++++++----------
 lib/libc/stdlib/_env.c       |  18 ++++++++----------
 lib/libc/stdlib/hcreate.c    |  14 +++++---------
 lib/libc/stdlib/radixsort.c  |  11 +++++------
 lib/libc/string/wcsdup.c     |   9 ++++-----
 12 files changed, 90 insertions(+), 88 deletions(-)

diffs (truncated from 538 to 300 lines):

diff -r eb512b7fc88f -r 0beaf94e8a5f lib/libc/gen/fts.c
--- a/lib/libc/gen/fts.c        Sat Mar 12 17:27:50 2022 +0000
+++ b/lib/libc/gen/fts.c        Sat Mar 12 17:31:39 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fts.c,v 1.50 2021/11/02 08:39:20 nia Exp $     */
+/*     $NetBSD: fts.c,v 1.51 2022/03/12 17:31:39 christos Exp $        */
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -38,7 +38,7 @@
 #if 0
 static char sccsid[] = "@(#)fts.c      8.6 (Berkeley) 8/14/94";
 #else
-__RCSID("$NetBSD: fts.c,v 1.50 2021/11/02 08:39:20 nia Exp $");
+__RCSID("$NetBSD: fts.c,v 1.51 2022/03/12 17:31:39 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -1015,9 +1015,10 @@
         * 40 so don't realloc one entry at a time.
         */
        if (nitems > sp->fts_nitems) {
-               if (reallocarr(&sp->fts_array,
-                   nitems + 40, sizeof(FTSENT *)) != 0)
-                       return (head);
+               errno = reallocarr(&sp->fts_array,
+                   nitems + 40, sizeof(*sp->fts_array));
+               if (errno)
+                       return head;
                sp->fts_nitems = fts_nitems_truncate(nitems + 40);
        }
        for (ap = sp->fts_array, p = head; p; p = p->fts_link)
diff -r eb512b7fc88f -r 0beaf94e8a5f lib/libc/gen/scandir.c
--- a/lib/libc/gen/scandir.c    Sat Mar 12 17:27:50 2022 +0000
+++ b/lib/libc/gen/scandir.c    Sat Mar 12 17:31:39 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: scandir.c,v 1.29 2021/10/29 10:40:00 nia Exp $ */
+/*     $NetBSD: scandir.c,v 1.30 2022/03/12 17:31:39 christos Exp $    */
 
 /*
  * Copyright (c) 1983, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)scandir.c  8.3 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: scandir.c,v 1.29 2021/10/29 10:40:00 nia Exp $");
+__RCSID("$NetBSD: scandir.c,v 1.30 2022/03/12 17:31:39 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -94,6 +94,7 @@
        struct dirent *d, *p, **names;
        size_t nitems, arraysz;
        DIR *dirp;
+       int serrno;
 
        _DIAGASSERT(dirname != NULL);
        _DIAGASSERT(namelist != NULL);
@@ -105,7 +106,8 @@
                goto bad;
 
        names = NULL;
-       if (reallocarr(&names, arraysz, sizeof(*names)) != 0)
+       errno = reallocarr(&names, arraysz, sizeof(*names));
+       if (errno)
                goto bad;
 
        nitems = 0;
@@ -120,7 +122,8 @@
                if (nitems >= arraysz) {
                        if ((arraysz = dirsize(dirp->dd_fd, arraysz)) == 0)
                                goto bad2;
-                       if (reallocarr(&names, arraysz, sizeof(*names)) != 0)
+                       errno = reallocarr(&names, arraysz, sizeof(*names));
+                       if (errno)
                                goto bad2;
                }
 
@@ -146,10 +149,14 @@
        return (int)nitems;
 
 bad2:
+       serrno = errno;
        while (nitems-- > 0)
                free(names[nitems]);
        free(names);
+       errno = serrno;
 bad:
+       serrno = errno;
        (void)closedir(dirp);
+       errno = serrno;
        return -1;
 }
diff -r eb512b7fc88f -r 0beaf94e8a5f lib/libc/gen/setmode.3
--- a/lib/libc/gen/setmode.3    Sat Mar 12 17:27:50 2022 +0000
+++ b/lib/libc/gen/setmode.3    Sat Mar 12 17:31:39 2022 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: setmode.3,v 1.22 2017/07/03 21:32:49 wiz Exp $
+.\"    $NetBSD: setmode.3,v 1.23 2022/03/12 17:31:39 christos Exp $
 .\"
 .\" Copyright (c) 1989, 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)setmode.3  8.2 (Berkeley) 4/28/95
 .\"
-.Dd January 4, 2009
+.Dd March 12, 2022
 .Dt SETMODE 3
 .Os
 .Sh NAME
@@ -111,7 +111,7 @@
 may fail and set
 .Va errno
 for any of the errors specified for the library routines
-.Xr malloc 3
+.Xr reallocarr 3
 or
 .Xr strtol 3 .
 In addition,
diff -r eb512b7fc88f -r 0beaf94e8a5f lib/libc/gen/setmode.c
--- a/lib/libc/gen/setmode.c    Sat Mar 12 17:27:50 2022 +0000
+++ b/lib/libc/gen/setmode.c    Sat Mar 12 17:31:39 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: setmode.c,v 1.36 2022/03/12 08:32:02 nia Exp $ */
+/*     $NetBSD: setmode.c,v 1.37 2022/03/12 17:31:39 christos Exp $    */
 
 /*
  * Copyright (c) 1989, 1993, 1994
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)setmode.c  8.2 (Berkeley) 3/25/94";
 #else
-__RCSID("$NetBSD: setmode.c,v 1.36 2022/03/12 08:32:02 nia Exp $");
+__RCSID("$NetBSD: setmode.c,v 1.37 2022/03/12 17:31:39 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -164,10 +164,9 @@
                BITCMD *newset;                                         \
                setlen += SET_LEN_INCR;                                 \
                newset = saveset;                                       \
-               if (reallocarr(&newset, setlen, sizeof(BITCMD)) != 0) { \
-                       errno = ENOMEM;                                 \
+               errno = reallocarr(&newset, setlen, sizeof(*newset));   \
+               if (errno)                                              \
                        goto out;                                       \
-               }                                                       \
                set = newset + (set - saveset);                         \
                saveset = newset;                                       \
                endset = newset + (setlen - 2);                         \
@@ -208,10 +207,9 @@
 
        setlen = SET_LEN + 2;
        set = NULL;
-       if (reallocarr(&set, setlen, sizeof(BITCMD)) != 0) {
-               errno = ENOMEM;
-               return (NULL);
-       }
+       errno = reallocarr(&set, setlen, sizeof(*set));
+       if (errno)
+               return NULL;
        saveset = set;
        endset = set + (setlen - 2);
 
diff -r eb512b7fc88f -r 0beaf94e8a5f lib/libc/gen/stringlist.c
--- a/lib/libc/gen/stringlist.c Sat Mar 12 17:27:50 2022 +0000
+++ b/lib/libc/gen/stringlist.c Sat Mar 12 17:31:39 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: stringlist.c,v 1.14 2015/05/21 01:29:13 christos Exp $ */
+/*     $NetBSD: stringlist.c,v 1.15 2022/03/12 17:31:39 christos Exp $ */
 
 /*-
  * Copyright (c) 1994, 1999 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: stringlist.c,v 1.14 2015/05/21 01:29:13 christos Exp $");
+__RCSID("$NetBSD: stringlist.c,v 1.15 2022/03/12 17:31:39 christos Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -69,9 +69,11 @@
        sl->sl_cur = 0;
        sl->sl_max = _SL_CHUNKSIZE;
        sl->sl_str = NULL;
-       errno = reallocarr(&sl->sl_str, sl->sl_max, sizeof(char *));
+       errno = reallocarr(&sl->sl_str, sl->sl_max, sizeof(*sl->sl_str));
        if (errno) {
+               int serrno = errno;
                free(sl);
+               errno = serrno;
                sl = NULL;
        }
        return sl;
@@ -91,7 +93,7 @@
                char    **new = sl->sl_str;
 
                errno = reallocarr(&new, (sl->sl_max + _SL_CHUNKSIZE),
-                   sizeof(char *));
+                   sizeof(*new));
                if (errno)
                        return -1;
                sl->sl_max += _SL_CHUNKSIZE;
diff -r eb512b7fc88f -r 0beaf94e8a5f lib/libc/net/getprotoent_r.c
--- a/lib/libc/net/getprotoent_r.c      Sat Mar 12 17:27:50 2022 +0000
+++ b/lib/libc/net/getprotoent_r.c      Sat Mar 12 17:31:39 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getprotoent_r.c,v 1.7 2022/03/12 08:41:38 nia Exp $    */
+/*     $NetBSD: getprotoent_r.c,v 1.8 2022/03/12 17:31:39 christos Exp $       */
 
 /*
  * Copyright (c) 1983, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)getprotoent.c      8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: getprotoent_r.c,v 1.7 2022/03/12 08:41:38 nia Exp $");
+__RCSID("$NetBSD: getprotoent_r.c,v 1.8 2022/03/12 17:31:39 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -87,6 +87,7 @@
 {
        char *p, *cp, **q;
        size_t i = 0;
+       int serrno;
 
        if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "re")) == NULL)
                return NULL;
@@ -112,12 +113,10 @@
                if (pd->aliases == NULL) {
                        pd->aliases = NULL;
                        pd->maxaliases = 10;
-                       if (reallocarr(&pd->aliases,
-                           pd->maxaliases, sizeof(char *)) != 0) {
-                               endprotoent_r(pd);
-                               errno = ENOMEM;
-                               return NULL;
-                       }
+                       errno = reallocarr(&pd->aliases,
+                           pd->maxaliases, sizeof(*pd->aliases));
+                       if (errno)
+                               goto bad;
                }
                q = pr->p_aliases = pd->aliases;
                if (p != NULL) {
@@ -129,12 +128,10 @@
                                }
                                if (i == pd->maxaliases - 2) {
                                        pd->maxaliases *= 2;
-                                       if (reallocarr(&q,
-                                           pd->maxaliases, sizeof(char *)) != 0) {
-                                               endprotoent_r(pd);
-                                               errno = ENOMEM;
-                                               return NULL;
-                                       }
+                                       errno = reallocarr(&q,
+                                           pd->maxaliases, sizeof(*q));
+                                       if (errno)
+                                               goto bad;
                                        pr->p_aliases = pd->aliases = q;
                                }
                                q[i++] = cp;
@@ -147,4 +144,9 @@
                q[i] = NULL;
                return pr;
        }
+bad:
+       serrno = errno;
+       endprotoent_r(pd);
+       errno = serrno;
+       return NULL;
 }
diff -r eb512b7fc88f -r 0beaf94e8a5f lib/libc/net/getservent_r.c
--- a/lib/libc/net/getservent_r.c       Sat Mar 12 17:27:50 2022 +0000
+++ b/lib/libc/net/getservent_r.c       Sat Mar 12 17:31:39 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getservent_r.c,v 1.12 2022/03/12 08:43:11 nia Exp $    */
+/*     $NetBSD: getservent_r.c,v 1.13 2022/03/12 17:31:39 christos Exp $       */
 
 /*
  * Copyright (c) 1983, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)getservent.c       8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: getservent_r.c,v 1.12 2022/03/12 08:43:11 nia Exp $");
+__RCSID("$NetBSD: getservent_r.c,v 1.13 2022/03/12 17:31:39 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -292,11 +292,10 @@
        if (sd->aliases == NULL) {
                sd->maxaliases = 10;
                sd->aliases = NULL;
-               if (reallocarr(&sd->aliases,
-                   sd->maxaliases, sizeof(char *)) != 0) {
-                       errno = ENOMEM;



Home | Main Index | Thread Index | Old Index