Bug#47214: dpkg: Memleak in libdpkg when using parseversion() (lib/parsehelp.c)
Package: dpkg
Version: 1.4.1.13
Severity: wishlist
parseversion calls nfstrsave which is a strdup() clone which calls
nfmalloc(), dpkg's non-freeing malloc.
When using a cmpversions() based on dpkg's (see main/enquire.c) in a long
lived process that does a lot of comparisons, all this memory is completely
wasted. The program I'm working loses a good 3MB every 5s or so thanks to
this. (What can I say? I do a lot of version comparisons)
A workaround is to use libapt-pkg's version comparison stuff instead, which
doesn't leak memory in any noticable amount.
Something like:
#include <apt-pkg/version.h>
extern "C" {
int cmpversions(char *left, char *op, char *right) {
int i = pkgVersionCompare(left, right);
if (!strcmp(op, "<=")) return i <= 0;
if (!strcmp(op, ">=")) return i >= 0;
if (!strcmp(op, "<<")) return i < 0;
if (!strcmp(op, ">>")) return i > 0;
if (!strcmp(op, "=")) return i == 0;
}
}
in a C++ source file gives similar behaviour to the original (although
it returns 1/0 for true/false (C-style) instead of 0/1 (shell style),
and doesn't handle as many operators, or error conditions).
Cheers,
aj
-- System Information
Debian Release: potato
Kernel Version: Linux azure 2.0.33 #1 Fri May 15 19:45:40 EST 1998 i586 unknown
Versions of the packages dpkg depends on:
ii libc6 2.1.2-0pre12 GNU C Library: Shared libraries and timezone
ii libncurses4 4.2-3.2 Shared libraries for terminal handling
ii libstdc++2.10 2.95.1-1 The GNU stdc++ library
Reply to: