Subject: bin/34965: sysctl(1) small memory leak
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: None <njoly@pasteur.fr>
List: netbsd-bugs
Date: 11/01/2006 23:45:00
>Number:         34965
>Category:       bin
>Synopsis:       sysctl(1) small memory leak
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Wed Nov 01 23:45:00 +0000 2006
>Originator:     Nicolas Joly
>Release:        NetBSD 4.99.3
>Organization:
Institut Pasteur, Paris.
>Environment:
System: NetBSD cixy.dial.pasteur.fr 4.99.3 NetBSD 4.99.3 (CIXY) #0: Mon Oct 30 09:51:35 CET 2006 njoly@cixy.dial.pasteur.fr:/local/src/NetBSD/obj/i386/sys/arch/i386/compile/CIXY i386
Architecture: i386
Machine: i386
>Description:
while reading sysctl(1) code, i noticed a small memory leak. The
findhandler() function use the regcomp() function to process
regular-expressions, but the generated RE are never freed.
>How-To-Repeat:
Check sysctl(1) findhandler() function.
>Fix:
Index: sbin/sysctl/sysctl.c
===================================================================
RCS file: /cvsroot/src/sbin/sysctl/sysctl.c,v
retrieving revision 1.119
diff -u -r1.119 sysctl.c
--- sbin/sysctl/sysctl.c	1 Nov 2006 22:26:36 -0000	1.119
+++ sbin/sysctl/sysctl.c	1 Nov 2006 22:54:54 -0000
@@ -395,13 +395,16 @@
 		j = regexec(&re, s, 1, match, 0);
 		if (j == 0) {
 			if (match[0].rm_so == 0 && match[0].rm_eo == l &&
-			    (w ? p[i].ps_w : p[i].ps_p) != NULL)
+			    (w ? p[i].ps_w : p[i].ps_p) != NULL) {
+				regfree(&re);
 				return (&p[i]);
+			}
 		}
 		else if (j != REG_NOMATCH) {
 			regerror(j, &re, eb, sizeof(eb));
 			errx(1, "regexec: %s: %s", p[i].ps_re, eb);
 		}
+		regfree(&re);
 	}
 
 	return (NULL);