pkgsrc-Changes-HG archive

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

[pkgsrc/trunk]: pkgsrc/pkgtools/pkg_install/files/lib Don't cache the allocat...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/15906891b7a6
branches:  trunk
changeset: 533400:15906891b7a6
user:      joerg <joerg%pkgsrc.org@localhost>
date:      Tue Sep 18 15:44:18 2007 +0000

description:
Don't cache the allocated argv in a local static variable, just allocate
and free it.

diffstat:

 pkgtools/pkg_install/files/lib/fexec.c |  42 ++++++++++++---------------------
 1 files changed, 15 insertions(+), 27 deletions(-)

diffs (72 lines):

diff -r c2b4814fef64 -r 15906891b7a6 pkgtools/pkg_install/files/lib/fexec.c
--- a/pkgtools/pkg_install/files/lib/fexec.c    Tue Sep 18 15:40:47 2007 +0000
+++ b/pkgtools/pkg_install/files/lib/fexec.c    Tue Sep 18 15:44:18 2007 +0000
@@ -65,7 +65,7 @@
 #include "lib.h"
 
 #ifndef lint
-__RCSID("$NetBSD: fexec.c,v 1.8 2005/12/06 01:08:09 ben Exp $");
+__RCSID("$NetBSD: fexec.c,v 1.9 2007/09/18 15:44:18 joerg Exp $");
 #endif
 
 static int     vfcexec(const char *, int, const char *, va_list);
@@ -108,37 +108,23 @@
 static int
 vfcexec(const char *path, int skipempty, const char *arg, va_list ap)
 {
-       static unsigned int     max = 4;
-       static const char       **argv = NULL;
-       unsigned int            argc;
+       const char **argv;
+       size_t argv_size, argc;
+       int retval;
 
-       if (argv == NULL) {
-               argv = malloc(max * sizeof(const char *));
-               if (argv == NULL) {
-                       warn("vfcexec: Can't alloc arg space");
-                       return -1;
-               }
-       }
+       argv_size = 16;
+       if ((argv = malloc(argv_size * sizeof(*argv))) == NULL)
+               err(EXIT_FAILURE, "vfcexec: malloc failed");
 
        argv[0] = arg;
        argc = 1;
 
        do {
-               if (argc == max) {
-                       unsigned int    new;
-                       const char      **ptr;
-
-                       new = max * 2;
-                       ptr = realloc(argv, new * sizeof(const char *));
-                       if (ptr == NULL) {
-                               warn("vfcexec: Can't alloc arg space");
-                               free(argv);
-                               argv = NULL;
-                               max = 4;
-                               return -1;
-                       }
-                       argv = ptr;
-                       max = new;
+               if (argc == argv_size) {
+                       argv_size *= 2;
+                       argv = realloc(argv, argv_size * sizeof(*argv));
+                       if (argv == NULL)
+                               err(EXIT_FAILURE, "vfcexec: realloc failed");
                }
                arg = va_arg(ap, const char *);
                if (skipempty && arg && strlen(arg) == 0)
@@ -146,7 +132,9 @@
                argv[argc++] = arg;
        } while (arg != NULL);
 
-       return pfcexec(path, argv[0], argv);
+       retval = pfcexec(path, argv[0], argv);
+       free(argv);
+       return retval;
 }
 
 int



Home | Main Index | Thread Index | Old Index