Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 25 May 2018 02:07:05 +0000 (UTC)
From:      Marcelo Araujo <araujo@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r334199 - head/usr.sbin/bhyve
Message-ID:  <201805250207.w4P275Pf060725@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: araujo
Date: Fri May 25 02:07:05 2018
New Revision: 334199
URL: https://svnweb.freebsd.org/changeset/base/334199

Log:
  Fix a memory leak on topology_parse().
  
  strdup(3) allocates memory for a copy of the string, does the copy and
  returns a pointer to it. If there is no sufficient memory NULL is returned
  and the global errno is set to ENOMEM.
  We do a sanity check to see if it was possible to allocate enough memory.
  
  Also as we allocate memory, we need to free this memory used. Or it will
  going out of scope leaks the storage it points to.
  
  Reviewed by:	rgrimes
  MFC after:	3 weeks.
  X-MFC:		r332298
  Sponsored by:	iXsystems Inc.
  Differential Revision:	https://reviews.freebsd.org/D15550

Modified:
  head/usr.sbin/bhyve/bhyverun.c

Modified: head/usr.sbin/bhyve/bhyverun.c
==============================================================================
--- head/usr.sbin/bhyve/bhyverun.c	Fri May 25 01:38:59 2018	(r334198)
+++ head/usr.sbin/bhyve/bhyverun.c	Fri May 25 02:07:05 2018	(r334199)
@@ -193,6 +193,7 @@ topology_parse(const char *opt)
 	c = 1, n = 1, s = 1, t = 1;
 	ns = false, scts = false;
 	str = strdup(opt);
+	assert(str != NULL);
 
 	while ((cp = strsep(&str, ",")) != NULL) {
 		if (sscanf(cp, "%i%n", &tmp, &chk) == 1) {
@@ -218,11 +219,13 @@ topology_parse(const char *opt)
 		} else if (cp[0] == '\0')
 			continue;
 		else
-			return (-1);
+			goto out;
 		/* Any trailing garbage causes an error */
 		if (cp[chk] != '\0')
-			return (-1);
+			goto out;
 	}
+	free(str);
+
 	/*
 	 * Range check 1 <= n <= UINT16_MAX all values
 	 */
@@ -248,6 +251,10 @@ topology_parse(const char *opt)
 	cores = c;
 	threads = t;
 	return(0);
+
+out:
+	free(str);
+	return (-1);
 }
 
 static int



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201805250207.w4P275Pf060725>