Module Name:    src
Committed By:   christos
Date:           Mon Mar 22 03:28:55 UTC 2021

Modified Files:
        src/usr.sbin/services_mkdb: uniq.c

Log Message:
Avoid memory leak on empty lines (https://reviews.freebsd.org/D29370)


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/usr.sbin/services_mkdb/uniq.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/services_mkdb/uniq.c
diff -u src/usr.sbin/services_mkdb/uniq.c:1.6 src/usr.sbin/services_mkdb/uniq.c:1.7
--- src/usr.sbin/services_mkdb/uniq.c:1.6	Sat Jun 21 13:48:07 2014
+++ src/usr.sbin/services_mkdb/uniq.c	Sun Mar 21 23:28:55 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: uniq.c,v 1.6 2014/06/21 17:48:07 christos Exp $	*/
+/*	$NetBSD: uniq.c,v 1.7 2021/03/22 03:28:55 christos Exp $	*/
 
 /*-
  * Copyright (c) 2007 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: uniq.c,v 1.6 2014/06/21 17:48:07 christos Exp $");
+__RCSID("$NetBSD: uniq.c,v 1.7 2021/03/22 03:28:55 christos Exp $");
 
 #include <stdio.h>
 #include <string.h>
@@ -126,11 +126,12 @@ comp(const char *origline, char **compli
 	for (p = (const unsigned char *)origline; l && *p && isspace(*p);
 	    p++, l--)
 		continue;
+	if (*p == '\0' || l == 0)
+		return 0;
+
 	cline = emalloc(l + 1);
 	(void)memcpy(cline, p, l);
 	cline[l] = '\0';
-	if (*cline == '\0')
-		return 0;
 
 	complen = 0;
 	hasalnum = 0;
@@ -160,6 +161,11 @@ comp(const char *origline, char **compli
 		--complen;
 	}
 	*q = '\0';
+	if (!hasalnum) {
+		free(cline);
+		cline = NULL;
+		complen = 0;
+	}
 	*compline = cline;
 	*len = complen;
 	return hasalnum;

Reply via email to