Source-Changes-HG archive

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

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



details:   https://anonhg.NetBSD.org/src/rev/e9a80a4fd062
branches:  trunk
changeset: 363423:e9a80a4fd062
user:      nia <nia%NetBSD.org@localhost>
date:      Sat Mar 12 08:41:38 2022 +0000

description:
getprotoent_r: use reallocarr

diffstat:

 lib/libc/net/getprotoent_r.c |  21 +++++++++------------
 1 files changed, 9 insertions(+), 12 deletions(-)

diffs (59 lines):

diff -r 90b218822a9b -r e9a80a4fd062 lib/libc/net/getprotoent_r.c
--- a/lib/libc/net/getprotoent_r.c      Sat Mar 12 08:36:52 2022 +0000
+++ b/lib/libc/net/getprotoent_r.c      Sat Mar 12 08:41:38 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getprotoent_r.c,v 1.6 2011/10/15 23:00:02 christos Exp $       */
+/*     $NetBSD: getprotoent_r.c,v 1.7 2022/03/12 08:41:38 nia 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.6 2011/10/15 23:00:02 christos Exp $");
+__RCSID("$NetBSD: getprotoent_r.c,v 1.7 2022/03/12 08:41:38 nia Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -87,7 +87,6 @@
 {
        char *p, *cp, **q;
        size_t i = 0;
-       int oerrno;
 
        if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "re")) == NULL)
                return NULL;
@@ -111,12 +110,12 @@
                        *p++ = '\0';
                pr->p_proto = atoi(cp);
                if (pd->aliases == NULL) {
+                       pd->aliases = NULL;
                        pd->maxaliases = 10;
-                       pd->aliases = malloc(pd->maxaliases * sizeof(char *));
-                       if (pd->aliases == NULL) {
-                               oerrno = errno;
+                       if (reallocarr(&pd->aliases,
+                           pd->maxaliases, sizeof(char *)) != 0) {
                                endprotoent_r(pd);
-                               errno = oerrno;
+                               errno = ENOMEM;
                                return NULL;
                        }
                }
@@ -130,12 +129,10 @@
                                }
                                if (i == pd->maxaliases - 2) {
                                        pd->maxaliases *= 2;
-                                       q = realloc(q,
-                                           pd->maxaliases * sizeof(char *));
-                                       if (q == NULL) {
-                                               oerrno = errno;
+                                       if (reallocarr(&q,
+                                           pd->maxaliases, sizeof(char *)) != 0) {
                                                endprotoent_r(pd);
-                                               errno = oerrno;
+                                               errno = ENOMEM;
                                                return NULL;
                                        }
                                        pr->p_aliases = pd->aliases = q;



Home | Main Index | Thread Index | Old Index