Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/net getservent_r: use reallocarr



details:   https://anonhg.NetBSD.org/src/rev/5295318454b7
branches:  trunk
changeset: 363424:5295318454b7
user:      nia <nia%NetBSD.org@localhost>
date:      Sat Mar 12 08:43:11 2022 +0000

description:
getservent_r: use reallocarr
CVSv ----------------------------------------------------------------------

diffstat:

 lib/libc/net/getservent_r.c |  17 +++++++++++------
 1 files changed, 11 insertions(+), 6 deletions(-)

diffs (47 lines):

diff -r e9a80a4fd062 -r 5295318454b7 lib/libc/net/getservent_r.c
--- a/lib/libc/net/getservent_r.c       Sat Mar 12 08:41:38 2022 +0000
+++ b/lib/libc/net/getservent_r.c       Sat Mar 12 08:43:11 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getservent_r.c,v 1.11 2011/10/15 23:00:02 christos Exp $       */
+/*     $NetBSD: getservent_r.c,v 1.12 2022/03/12 08:43:11 nia 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.11 2011/10/15 23:00:02 christos Exp $");
+__RCSID("$NetBSD: getservent_r.c,v 1.12 2022/03/12 08:43:11 nia Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -291,9 +291,12 @@
 
        if (sd->aliases == NULL) {
                sd->maxaliases = 10;
-               sd->aliases = malloc(sd->maxaliases * sizeof(char *));
-               if (sd->aliases == NULL)
+               sd->aliases = NULL;
+               if (reallocarr(&sd->aliases,
+                   sd->maxaliases, sizeof(char *)) != 0) {
+                       errno = ENOMEM;
                        goto fail;
+               }
        }
        sp->s_aliases = sd->aliases;
        i = 0;
@@ -302,9 +305,11 @@
                        goto fail;
                if (i == sd->maxaliases - 2) {
                        sd->maxaliases *= 2;
-                       q = realloc(sd->aliases, sd->maxaliases * sizeof(*q));
-                       if (q == NULL)
+                       q = sd->aliases;
+                       if (reallocarr(&q, sd->maxaliases, sizeof(*q)) != 0) {
+                               errno = ENOMEM;
                                goto fail;
+                       }
                        sp->s_aliases = sd->aliases = q;
                }
                sp->s_aliases[i++] = __UNCONST(data + 1);



Home | Main Index | Thread Index | Old Index