Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/stdlib _env: use reallocarr



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

description:
_env: use reallocarr

diffstat:

 lib/libc/stdlib/_env.c |  19 ++++++++++++-------
 1 files changed, 12 insertions(+), 7 deletions(-)

diffs (47 lines):

diff -r 5295318454b7 -r 0de95b0537c8 lib/libc/stdlib/_env.c
--- a/lib/libc/stdlib/_env.c    Sat Mar 12 08:43:11 2022 +0000
+++ b/lib/libc/stdlib/_env.c    Sat Mar 12 08:44:38 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: _env.c,v 1.11 2021/04/20 21:42:32 christos Exp $ */
+/*     $NetBSD: _env.c,v 1.12 2022/03/12 08:44:38 nia Exp $ */
 
 /*-
  * Copyright (c) 2010 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: _env.c,v 1.11 2021/04/20 21:42:32 christos Exp $");
+__RCSID("$NetBSD: _env.c,v 1.12 2022/03/12 08:44:38 nia Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -296,18 +296,23 @@
 
        /* Allocate a new environment array. */
        if (environ == allocated_environ) {
-               new_environ = realloc(environ,
-                   new_size * sizeof(*new_environ));
-               if (new_environ == NULL)
+               new_environ = environ;
+               if (reallocarr(&new_environ,
+                   new_size, sizeof(*new_environ)) != 0) {
+                       errno = ENOMEM;
                        return -1;
+               }
        } else {
                free(allocated_environ);
                allocated_environ = NULL;
                allocated_environ_size = 0;
 
-               new_environ = malloc(new_size * sizeof(*new_environ));
-               if (new_environ == NULL)
+               new_environ = NULL;
+               if (reallocarr(&new_environ,
+                   new_size, sizeof(*new_environ)) != 0) {
+                       errno = ENOMEM;
                        return -1;
+               }
                (void)memcpy(new_environ, environ,
                    num_entries * sizeof(*new_environ));
        }



Home | Main Index | Thread Index | Old Index