Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 5 Feb 2013 09:50:33 +0000 (UTC)
From:      Xin LI <delphij@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org
Subject:   svn commit: r246356 - in stable: 7/lib/libc/gen 8/lib/libc/gen 9/lib/libc/gen
Message-ID:  <201302050950.r159oXOR020634@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: delphij
Date: Tue Feb  5 09:50:33 2013
New Revision: 246356
URL: http://svnweb.freebsd.org/changeset/base/246356

Log:
  MFC r243758 (marcel):
  
  In globextend() when the pathv vector cannot be (re-)allocated, don't
  free and clear the gl_pathv pointer in the glob_t structure. Such
  breaks the invariant of the glob_t structure, as stated in the comment
  right in front of the globextend() function. If gl_pathv was non-NULL,
  then gl_pathc was > 0. Making gl_pathv a NULL pointer without also
  setting gl_pathc to 0 is wrong.
  
  Since we otherwise don't free the memory associated with a glob_t in
  error cases, it's unlikely that this change will cause a memory leak
  that wasn't already there to begin with. Callers of glob(3) must
  call globfree(3) irrespective of whether glob(3) returned an error
  or not.
  
  MFC r243759 (marcel):
  
  In globextend(), take advantage of the fact that realloc(NULL, size) is
  equivalent to malloc(size). This eliminates the conditional expression
  used for calling either realloc() or malloc() when realloc() will do
  all the time.

Modified:
  stable/8/lib/libc/gen/glob.c
Directory Properties:
  stable/8/lib/libc/   (props changed)

Changes in other areas also in this revision:
Modified:
  stable/7/lib/libc/gen/glob.c
  stable/9/lib/libc/gen/glob.c
Directory Properties:
  stable/7/lib/libc/   (props changed)
  stable/9/lib/libc/   (props changed)

Modified: stable/8/lib/libc/gen/glob.c
==============================================================================
--- stable/8/lib/libc/gen/glob.c	Tue Feb  5 09:40:31 2013	(r246355)
+++ stable/8/lib/libc/gen/glob.c	Tue Feb  5 09:50:33 2013	(r246356)
@@ -710,16 +710,10 @@ globextend(const Char *path, glob_t *pgl
 	}
 
 	newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs);
-	pathv = pglob->gl_pathv ?
-		    realloc((char *)pglob->gl_pathv, newsize) :
-		    malloc(newsize);
-	if (pathv == NULL) {
-		if (pglob->gl_pathv) {
-			free(pglob->gl_pathv);
-			pglob->gl_pathv = NULL;
-		}
+	/* realloc(NULL, newsize) is equivalent to malloc(newsize). */
+	pathv = realloc((void *)pglob->gl_pathv, newsize);
+	if (pathv == NULL)
 		return(GLOB_NOSPACE);
-	}
 
 	if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) {
 		/* first time around -- clear initial gl_offs items */



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