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 Fix a major memory leak...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/69dc35bc86fc
branches:  trunk
changeset: 533368:69dc35bc86fc
user:      joerg <joerg%pkgsrc.org@localhost>
date:      Sun Sep 16 19:03:52 2007 +0000

description:
Fix a major memory leak in Dewey that existed sine ~forever.
This was made a lot more critical with the changes to use the iteration
API, as that is running a lot more Dewey comparisions.

Thanks to adrianp and wiz for the reports.

Bump to 20070916.

diffstat:

 pkgtools/pkg_install/files/lib/defs.h    |  34 +----------------------
 pkgtools/pkg_install/files/lib/dewey.c   |  48 +++++++++++++++++++++++--------
 pkgtools/pkg_install/files/lib/version.h |   4 +-
 3 files changed, 38 insertions(+), 48 deletions(-)

diffs (158 lines):

diff -r 3e85133e7383 -r 69dc35bc86fc pkgtools/pkg_install/files/lib/defs.h
--- a/pkgtools/pkg_install/files/lib/defs.h     Sun Sep 16 17:34:32 2007 +0000
+++ b/pkgtools/pkg_install/files/lib/defs.h     Sun Sep 16 19:03:52 2007 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: defs.h,v 1.7 2007/04/20 14:25:14 joerg Exp $ */
+/* $NetBSD: defs.h,v 1.8 2007/09/16 19:03:52 joerg Exp $ */
 
 /*
  * Copyright (c) 1999-2000 Alistair G. Crooks.  All rights reserved.
@@ -47,38 +47,6 @@
 #include <string.h>
 #endif
 
-#define NEWARRAY(type,ptr,size,where,action) do {                      \
-       if ((ptr = (type *) calloc(sizeof(type), (unsigned)(size))) == NULL) { \
-               warn("%s: can't allocate %lu bytes", where,             \
-                       (unsigned long)(size * sizeof(type)));          \
-               action;                                                 \
-       }                                                               \
-} while( /* CONSTCOND */ 0)
-
-#define RENEW(type,ptr,size,where,action) do {                         \
-       type *newptr;                                                   \
-       if ((newptr = (type *) realloc(ptr, sizeof(type) * (size))) == NULL) { \
-               warn("%s: can't realloc %lu bytes", where,              \
-                       (unsigned long)(size * sizeof(type)));          \
-               action;                                                 \
-       }                                                               \
-       ptr = newptr;                                                   \
-} while( /* CONSTCOND */ 0)
-
-#define NEW(type, ptr, where, action)  NEWARRAY(type, ptr, 1, where, action)
-
-#define FREE(ptr)      (void) free(ptr)
-
-#define ALLOC(type, v, size, c, init, where, action) do {              \
-       if (size == 0) {                                                \
-               size = init;                                            \
-               NEWARRAY(type, v, size, where ": new", action);         \
-       } else if (c == size) {                                         \
-               size *= 2;                                              \
-               RENEW(type, v, size, where ": renew", action);          \
-       }                                                               \
-} while( /* CONSTCOND */ 0)
-
 #ifndef MIN
 #define MIN(a,b)       (((a) < (b)) ? (a) : (b))
 #endif
diff -r 3e85133e7383 -r 69dc35bc86fc pkgtools/pkg_install/files/lib/dewey.c
--- a/pkgtools/pkg_install/files/lib/dewey.c    Sun Sep 16 17:34:32 2007 +0000
+++ b/pkgtools/pkg_install/files/lib/dewey.c    Sun Sep 16 19:03:52 2007 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: dewey.c,v 1.6 2007/07/20 22:22:53 joerg Exp $ */
+/* $NetBSD: dewey.c,v 1.7 2007/09/16 19:03:52 joerg Exp $ */
 
 /*
  * Copyright © 2002 Alistair G. Crooks.  All rights reserved.
@@ -127,10 +127,15 @@
        int                 n;
        const char             *cp;
 
-       if (*num == 0) {
-               return 0;
+       if (ap->size == 0) {
+               ap->size = 62;
+               if ((ap->v = malloc(ap->size * sizeof(int))) == NULL)
+                       err(EXIT_FAILURE, "mkver malloc failed");
+       } else {
+               ap->size *= 2;
+               if ((ap->v = realloc(ap->v, ap->size * sizeof(int))) == NULL)
+                       err(EXIT_FAILURE, "mkver realloc failed");
        }
-       ALLOC(int, ap->v, ap->size, ap->c, 62, "mkver", exit(EXIT_FAILURE));
        if (isdigit((unsigned char)*num)) {
                for (cp = num, n = 0 ; isdigit((unsigned char)*num) ; num++) {
                        n = (n * 10) + (*num - '0');
@@ -154,7 +159,9 @@
        if (isalpha((unsigned char)*num)) {
                ap->v[ap->c++] = Dot;
                cp = strchr(alphas, tolower((unsigned char)*num));
-               ALLOC(int, ap->v, ap->size, ap->c, 62, "mkver", exit(EXIT_FAILURE));
+               ap->size *= 2;
+               if ((ap->v = realloc(ap->v, ap->size * sizeof(int))) == NULL)
+                       err(EXIT_FAILURE, "mkver realloc failed");
                ap->v[ap->c++] = (int)(cp - alphas) + 1;
                return 1;
        }
@@ -165,13 +172,26 @@
 static int
 mkversion(arr_t *ap, const char *num)
 {
-       (void) memset(ap, 0, sizeof(arr_t));
+       ap->c = 0;
+       ap->size = 0;
+       ap->v = NULL;
+       ap->netbsd = 0;
+
        while (*num) {
                num += mkcomponent(ap, num);
        }
        return 1;
 }
 
+static void
+freeversion(arr_t *ap)
+{
+       free(ap->v);
+       ap->v = NULL;
+       ap->c = 0;
+       ap->size = 0;
+}
+
 #define DIGIT(v, c, n) (((n) < (c)) ? v[n] : 0)
 
 /* compare the result against the test we were expecting */
@@ -220,16 +240,18 @@
 {
        arr_t   right;
        arr_t   left;
+       int retval;
 
-       (void) memset(&left, 0, sizeof(left));
-       if (!mkversion(&left, lhs)) {
+       if (!mkversion(&left, lhs))
+               return 0;
+       if (!mkversion(&right, rhs)) {
+               freeversion(&left);
                return 0;
        }
-       (void) memset(&right, 0, sizeof(right));
-       if (!mkversion(&right, rhs)) {
-               return 0;
-       }
-        return vtest(&left, op, &right);
+        retval = vtest(&left, op, &right);
+       freeversion(&left);
+       freeversion(&right);
+       return retval;
 }
 
 /*
diff -r 3e85133e7383 -r 69dc35bc86fc pkgtools/pkg_install/files/lib/version.h
--- a/pkgtools/pkg_install/files/lib/version.h  Sun Sep 16 17:34:32 2007 +0000
+++ b/pkgtools/pkg_install/files/lib/version.h  Sun Sep 16 19:03:52 2007 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: version.h,v 1.81 2007/09/11 14:55:52 joerg Exp $       */
+/*     $NetBSD: version.h,v 1.82 2007/09/16 19:03:52 joerg Exp $       */
 
 /*
  * Copyright (c) 2001 Thomas Klausner.  All rights reserved.
@@ -33,6 +33,6 @@
 #ifndef _INST_LIB_VERSION_H_
 #define _INST_LIB_VERSION_H_
 
-#define PKGTOOLS_VERSION "20070911"
+#define PKGTOOLS_VERSION "20070916"
 
 #endif /* _INST_LIB_VERSION_H_ */



Home | Main Index | Thread Index | Old Index