Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/netstat netstat(1): convert malloc(x * y) and reallo...



details:   https://anonhg.NetBSD.org/src/rev/eb6c00f59fcd
branches:  trunk
changeset: 990562:eb6c00f59fcd
user:      nia <nia%NetBSD.org@localhost>
date:      Sat Oct 30 11:23:07 2021 +0000

description:
netstat(1): convert malloc(x * y) and realloc(x * y) to reallocarr

diffstat:

 usr.bin/netstat/inet.c  |  16 +++++++---------
 usr.bin/netstat/inet6.c |  16 +++++++---------
 usr.bin/netstat/vtw.c   |  49 +++++++++++++++++++++++++++++--------------------
 3 files changed, 43 insertions(+), 38 deletions(-)

diffs (193 lines):

diff -r ae0ae8fc16f8 -r eb6c00f59fcd usr.bin/netstat/inet.c
--- a/usr.bin/netstat/inet.c    Sat Oct 30 11:18:51 2021 +0000
+++ b/usr.bin/netstat/inet.c    Sat Oct 30 11:23:07 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: inet.c,v 1.113 2020/08/28 07:23:48 ozaki-r Exp $       */
+/*     $NetBSD: inet.c,v 1.114 2021/10/30 11:23:07 nia Exp $   */
 
 /*
  * Copyright (c) 1983, 1988, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "from: @(#)inet.c       8.4 (Berkeley) 4/20/94";
 #else
-__RCSID("$NetBSD: inet.c,v 1.113 2020/08/28 07:23:48 ozaki-r Exp $");
+__RCSID("$NetBSD: inet.c,v 1.114 2021/10/30 11:23:07 nia Exp $");
 #endif
 #endif /* not lint */
 
@@ -293,8 +293,9 @@
        next = TAILQ_FIRST(head);
        prev = TAILQ_END(head);
 
-       if ((pcblist = malloc(size * sizeof(*pcblist))) == NULL)
-               err(1, "malloc");
+       pcblist = NULL;
+       if (reallocarr(&pcblist, size, sizeof(*pcblist)) != 0)
+               err(1, "reallocarr");
 
        i = 0;
        while (next != TAILQ_END(head)) {
@@ -325,11 +326,8 @@
                pcblist[i].ki_pflags = inpcb.inp_flags;
                if (i++ == size) {
                        size += 100;
-                       struct kinfo_pcb *n = realloc(pcblist,
-                           size * sizeof(*pcblist));
-                       if (n == NULL)
-                               err(1, "realloc");
-                       pcblist = n;
+                       if (reallocarr(&pcblist, size, sizeof(*pcblist)) != 0)
+                               err(1, "reallocarr");
                }
        }
        *len = i;
diff -r ae0ae8fc16f8 -r eb6c00f59fcd usr.bin/netstat/inet6.c
--- a/usr.bin/netstat/inet6.c   Sat Oct 30 11:18:51 2021 +0000
+++ b/usr.bin/netstat/inet6.c   Sat Oct 30 11:23:07 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: inet6.c,v 1.78 2021/09/10 21:52:18 rillig Exp $        */
+/*     $NetBSD: inet6.c,v 1.79 2021/10/30 11:23:07 nia Exp $   */
 /*     BSDI inet.c,v 2.3 1995/10/24 02:19:29 prb Exp   */
 
 /*
@@ -64,7 +64,7 @@
 #if 0
 static char sccsid[] = "@(#)inet.c     8.4 (Berkeley) 4/20/94";
 #else
-__RCSID("$NetBSD: inet6.c,v 1.78 2021/09/10 21:52:18 rillig Exp $");
+__RCSID("$NetBSD: inet6.c,v 1.79 2021/10/30 11:23:07 nia Exp $");
 #endif
 #endif /* not lint */
 
@@ -302,8 +302,9 @@
        next = TAILQ_FIRST(head);
        prev = TAILQ_END(head);
 
-       if ((pcblist = malloc(size * sizeof(*pcblist))) == NULL)
-               err(1, "malloc");
+       pcblist = NULL;
+       if (reallocarr(&pcblist, size, sizeof(*pcblist)) != 0)
+               err(1, "reallocarr");
 
        i = 0;
        while (next != TAILQ_END(head)) {
@@ -338,11 +339,8 @@
                pcblist[i].ki_tstate = tcpcb.t_state;
                if (i++ == size) {
                        size += 100;
-                       struct kinfo_pcb *n = realloc(pcblist,
-                           size * sizeof(*pcblist));
-                       if (n == NULL)
-                               err(1, "realloc");
-                       pcblist = n;
+                       if (reallocarr(&pcblist, size, sizeof(*pcblist)) != 0)
+                               err(1, "reallocarr");
                }
        }
        *len = i;
diff -r ae0ae8fc16f8 -r eb6c00f59fcd usr.bin/netstat/vtw.c
--- a/usr.bin/netstat/vtw.c     Sat Oct 30 11:18:51 2021 +0000
+++ b/usr.bin/netstat/vtw.c     Sat Oct 30 11:23:07 2021 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vtw.c,v 1.11 2021/03/02 01:02:12 simonb Exp $  */
+/*     $NetBSD: vtw.c,v 1.12 2021/10/30 11:23:07 nia Exp $     */
 
 /*
  * Copyright (c) 2011 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
 #if 0
 static char sccsid[] = "from: @(#)inet.c       8.4 (Berkeley) 4/20/94";
 #else
-__RCSID("$NetBSD: vtw.c,v 1.11 2021/03/02 01:02:12 simonb Exp $");
+__RCSID("$NetBSD: vtw.c,v 1.12 2021/10/30 11:23:07 nia Exp $");
 #endif
 #endif /* not lint */
 
@@ -303,8 +303,9 @@
                n = (klim - kbase + 1);
 
                if (!i) {
-                       if ((ubase = malloc(n * sizeof(*kbase))) == NULL)
-                               err(EXIT_FAILURE, NULL);
+                       ubase = NULL;
+                       if (reallocarr(&ubase, n, sizeof(*kbase)) != 0)
+                               err(EXIT_FAILURE, "reallocarr");
                        snarf(kbase, ubase, n * sizeof(*ubase));
 
                        mem += n * sizeof(*ubase);
@@ -333,9 +334,9 @@
 
        mem += (lim - base + 1) * sizeof(*base);
 
-       fat_tcpv4.base = malloc((lim - base + 1) * sizeof(*base));
-       if (fat_tcpv4.base == NULL)
-               err(EXIT_FAILURE, NULL);
+       fat_tcpv4.base = NULL;
+       if (reallocarr(&fat_tcpv4.base, lim - base + 1, sizeof(*base)) != 0)
+               err(EXIT_FAILURE, "reallocarr");
        fat_tcpv4.lim = fat_tcpv4.base + (lim - base);
 
        snarf(base, fat_tcpv4.base, sizeof(*base) * (lim - base + 1));
@@ -347,10 +348,13 @@
        hash = fat_tcpv4.hash;
        port = fat_tcpv4.port;
 
-       fat_tcpv4.hash = malloc(n * sizeof(*hash));
-       fat_tcpv4.port = malloc(n * sizeof(*port));
-       if (fat_tcpv4.hash == NULL || fat_tcpv4.port == NULL)
-               err(EXIT_FAILURE, NULL);
+       fat_tcpv4.hash = NULL;
+       if (reallocarr(&fat_tcpv4.hash, n, sizeof(*hash)) != 0)
+               err(EXIT_FAILURE, "reallocarr");
+
+       fat_tcpv4.port = NULL;
+       if (reallocarr(&fat_tcpv4.port, n, sizeof(*port)) != 0)
+               err(EXIT_FAILURE, "reallocarr");
 
        snarf(hash, fat_tcpv4.hash, n * sizeof(*hash));
        snarf(port, fat_tcpv4.port, n * sizeof(*port));
@@ -406,8 +410,9 @@
                n = (klim - kbase + 1);
 
                if (!i) {
-                       if ((ubase = malloc(n * sizeof(*kbase))) == NULL)
-                               err(EXIT_FAILURE, NULL);
+                       ubase = NULL;
+                       if (reallocarr(&ubase, n, sizeof(*kbase)) != 0)
+                               err(EXIT_FAILURE, "reallocarr");
 
                        snarf(kbase, ubase, n * sizeof(*ubase));
 
@@ -435,9 +440,10 @@
 
        mem += (lim - base + 1) * sizeof(*base);
 
-       fat_tcpv6.base = malloc((lim - base + 1) * sizeof(*base));
-       if (fat_tcpv6.base == NULL)
-               err(EXIT_FAILURE, NULL);
+       fat_tcpv6.base = NULL;
+       if (reallocarr(&fat_tcpv6.base, lim - base + 1, sizeof(*base)) != 0)
+               err(EXIT_FAILURE, "reallocarr");
+
        fat_tcpv6.lim = fat_tcpv6.base + (lim - base);
 
        snarf(base, fat_tcpv6.base, sizeof(*base) * (lim - base + 1));
@@ -449,10 +455,13 @@
        hash = fat_tcpv6.hash;
        port = fat_tcpv6.port;
 
-       fat_tcpv6.hash = malloc(n * sizeof(*hash));
-       fat_tcpv6.port = malloc(n * sizeof(*port));
-       if (fat_tcpv6.hash == NULL || fat_tcpv6.port == NULL)
-               err(EXIT_FAILURE, NULL);
+       fat_tcpv6.hash = NULL;
+       if (reallocarr(&fat_tcpv6.hash, n, sizeof(*hash)) != 0)
+               err(EXIT_FAILURE, "reallocarr");
+
+       fat_tcpv6.port = NULL;
+       if (reallocarr(&fat_tcpv6.port, n, sizeof(*port)) != 0)
+               err(EXIT_FAILURE, "reallocarr");
 
        snarf(hash, fat_tcpv6.hash, n * sizeof(*hash));
        snarf(port, fat_tcpv6.port, n * sizeof(*port));



Home | Main Index | Thread Index | Old Index