From 3c2d9ffdc7bd3577ec2f5bf3c9ed31ec92d62639 Mon Sep 17 00:00:00 2001 From: joerg Date: Sun, 16 Sep 2007 19:03:52 +0000 Subject: 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. --- pkgtools/pkg_install/files/lib/defs.h | 34 +----------------------- pkgtools/pkg_install/files/lib/dewey.c | 44 ++++++++++++++++++++++++-------- pkgtools/pkg_install/files/lib/version.h | 4 +-- 3 files changed, 36 insertions(+), 46 deletions(-) (limited to 'pkgtools') diff --git a/pkgtools/pkg_install/files/lib/defs.h b/pkgtools/pkg_install/files/lib/defs.h index 73ad73400bd..44dbf284999 100644 --- a/pkgtools/pkg_install/files/lib/defs.h +++ b/pkgtools/pkg_install/files/lib/defs.h @@ -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 #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 --git a/pkgtools/pkg_install/files/lib/dewey.c b/pkgtools/pkg_install/files/lib/dewey.c index 87ebbafcc82..015dc0da163 100644 --- a/pkgtools/pkg_install/files/lib/dewey.c +++ b/pkgtools/pkg_install/files/lib/dewey.c @@ -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 @@ mkcomponent(arr_t *ap, const char *num) 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 @@ mkcomponent(arr_t *ap, const char *num) 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 @@ mkcomponent(arr_t *ap, const char *num) 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 @@ dewey_cmp(const char *lhs, int op, const char *rhs) { arr_t right; arr_t left; + int retval; - (void) memset(&left, 0, sizeof(left)); - if (!mkversion(&left, lhs)) { + if (!mkversion(&left, lhs)) return 0; - } - (void) memset(&right, 0, sizeof(right)); if (!mkversion(&right, rhs)) { + freeversion(&left); return 0; } - return vtest(&left, op, &right); + retval = vtest(&left, op, &right); + freeversion(&left); + freeversion(&right); + return retval; } /* diff --git a/pkgtools/pkg_install/files/lib/version.h b/pkgtools/pkg_install/files/lib/version.h index 445c6dd0cab..30cb41b3fd3 100644 --- a/pkgtools/pkg_install/files/lib/version.h +++ b/pkgtools/pkg_install/files/lib/version.h @@ -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_ */ -- cgit v1.2.3