[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 4/8] posix: Remove alloca usage on glob dirname
From: |
Adhemerval Zanella |
Subject: |
[PATCH 4/8] posix: Remove alloca usage on glob dirname |
Date: |
Tue, 5 Jan 2021 15:58:16 -0300 |
This patch replaces the alloca/malloc usage for dirname creation
by the char_array struct.
Checked on x86_64-linux-gnu.
---
posix/glob.c | 28 +++++++++-------------------
1 file changed, 9 insertions(+), 19 deletions(-)
diff --git a/posix/glob.c b/posix/glob.c
index 41e1aeacad..d734ca977c 100644
--- a/posix/glob.c
+++ b/posix/glob.c
@@ -1212,7 +1212,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;
# define GLOBNAMES_MEMBERS(nnames) \
struct globnames *next; size_t count; char *name[nnames];
@@ -1244,32 +1243,23 @@ glob_in_dir (const char *pattern, const char
*directory, int flags,
}
else if (meta == GLOBPAT_NONE)
{
- 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 (glob_lstat (pglob, flags, fullname) == 0
+ if (glob_lstat (pglob, flags, char_array_str (&fullname)) == 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
{
--
2.25.1
- [PATCH 0/8] Remove alloca usage from glob, Adhemerval Zanella, 2021/01/05
- [PATCH 1/8] malloc: Add specialized dynarray for C strings, Adhemerval Zanella, 2021/01/05
- [PATCH 3/8] posix: Remove alloca usage for GLOB_BRACE on glob, Adhemerval Zanella, 2021/01/05
- [PATCH 4/8] posix: Remove alloca usage on glob dirname,
Adhemerval Zanella <=
- [PATCH 2/8] posix: Use char_array for internal glob dirname, Adhemerval Zanella, 2021/01/05
- [PATCH 6/8] posix: Remove alloca usage on glob user_name, Adhemerval Zanella, 2021/01/05
- [PATCH 5/8] posix: Use dynarray for globname in glob, Adhemerval Zanella, 2021/01/05
- [PATCH 7/8] posix: Use char_array for home_dir in glob, Adhemerval Zanella, 2021/01/05
- [PATCH 8/8] posix: Remove all alloca usage in glob, Adhemerval Zanella, 2021/01/05
- Re: [PATCH 0/8] Remove alloca usage from glob, Paul Eggert, 2021/01/13