[11/18] posix: Remove alloca usage on glob dirname

Submitted by Adhemerval Zanella on Aug. 11, 2017, 2:50 p.m.

Details

Message ID 1502463044-4042-12-git-send-email-adhemerval.zanella@linaro.org
State New
Headers show

Commit Message

Adhemerval Zanella Aug. 11, 2017, 2:50 p.m.
This patch replaces the alloca/malloc usage for dirname creation
by the char_array struct.

Checked on x86_64-linux-gnu.

	* posix/glob.c (glob_in_dir): Remove alloca usage for fullname.
	* malloc/char_array-skeleton.c (char_array_init_str): Remove unused
	attribute.
	(char_array_append_str): Likewise.
---
 posix/glob.c | 32 +++++++++++---------------------
 1 file changed, 11 insertions(+), 21 deletions(-)

Patch hide | download patch | download mbox

diff --git a/posix/glob.c b/posix/glob.c
index 1892f48..c09b347 100644
--- a/posix/glob.c
+++ b/posix/glob.c
@@ -1274,7 +1274,6 @@  glob_in_dir (const char *pattern, const char *directory, int flags,
 	     int (*errfunc) (const char *, int),
 	     glob_t *pglob, size_t alloca_used)
 {
-  size_t dirlen = strlen (directory);
   void *stream = NULL;
   struct globnames
     {
@@ -1314,35 +1313,26 @@  glob_in_dir (const char *pattern, const char *directory, int flags,
 	struct stat st;
 	struct_stat64 st64;
       } ust;
-      size_t patlen = strlen (pattern);
-      size_t fullsize;
-      bool alloca_fullname
-	= (! size_add_wrapv (dirlen + 1, patlen + 1, &fullsize)
-	   && glob_use_alloca (alloca_used, fullsize));
-      char *fullname;
-      if (alloca_fullname)
-	fullname = alloca_account (fullsize, alloca_used);
-      else
+      struct char_array fullname;
+
+      if (!char_array_init_str (&fullname, directory)
+	  || !char_array_append_str (&fullname, "/")
+	  || !char_array_append_str (&fullname, pattern))
 	{
-	  fullname = malloc (fullsize);
-	  if (fullname == NULL)
-	    return GLOB_NOSPACE;
+	  char_array_free (&fullname);
+	  return GLOB_NOSPACE;
 	}
 
-      mempcpy (mempcpy (mempcpy (fullname, directory, dirlen),
-			"/", 1),
-	       pattern, patlen + 1);
-      if (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)
-	   ? (*pglob->gl_stat) (fullname, &ust.st)
-	   : __lstat64 (fullname, &ust.st64))
+      if (((__glibc_likely (flags & GLOB_ALTDIRFUNC)
+	   ? (*pglob->gl_lstat) (char_array_str (&fullname), &ust.st)
+	   : __lstat64 (char_array_str (&fullname), &ust.st64))
 	  == 0)
 	  || errno == EOVERFLOW)
 	/* We found this file to be existing.  Now tell the rest
 	   of the function to copy this name into the result.  */
 	flags |= GLOB_NOCHECK;
 
-      if (__glibc_unlikely (!alloca_fullname))
-	free (fullname);
+      char_array_free (&fullname);
     }
   else
     {