From patchwork Thu Jun 8 21:13:15 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103408 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632363qgd; Thu, 8 Jun 2017 14:14:35 -0700 (PDT) X-Received: by 10.84.217.93 with SMTP id e29mr37162882plj.287.1496956475304; Thu, 08 Jun 2017 14:14:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956475; cv=none; d=google.com; s=arc-20160816; b=PouCaSDUKgr1pTbgB0dmS4KxpNQZgKy16zBOdbNZOLcqCtRrC9MstwBE1Wieg63llU bmw1Z4dGokVBV91OcsJxHIwXejprzm9rKOJrQBZ/oz/XnwNw2qfP23Vnzn+RAUNlNCmb dE4bfNPeyaCU1l7x6d9BrfjxNjVLEdEH52CQH036XHf6rzWPITMajCA35Yu1Gg2x0rvy C9FdHLSiDALVG0sXr7TYCqJoJJFe5j3YAjUOcRXKNPCsO8UJVkScigQRHDj3ShfNcmrh xVE83olgGcwT/O5c8gQJM4w23ZHYiPvHkgVeHzT+WAhCamNJ7vL1aAXtY9fZ8katE2Ky 4XOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=af9FvmJOQCAc1ohbc9bbJ3yhNUBZTt5MGJdzyD8l8Ig=; b=Q31mmpnN2C/BujYTQBpwwy66wDwFPRBPiby4WR5jsSbEEpA1SCzwNIP/sWcu7M+n0P OznERvmfqvrPHNia2fEnWNJ18+7y9l3W445P/7BmuQP9DK3AGBhorcOJK4D5m7KpFVod 8IiAwURwqD24yONaGdf4gbrPtSI/zAnt/Z8g35Sj/OMBUVHCmHj77CMcR08A06dQ5ru8 WoBoZoRg0suG9I4OPwx/l9izbUM82LBmHNRr+LtZEOrTgXyq+OU82Hhh51OoyXZ1jsU7 1+0G01PNMqakSC2jzUSS3j4B/ir7nkZRL37rxv7glIEsBISHLkVE/NamatCUZN/5BGMi 3ZJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80202-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80202-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 1si5268977pli.41.2017.06.08.14.14.34 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:14:35 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80202-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80202-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80202-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=YG0i3kELexKrPviZe3aMNgOiOasaJMJ NP34m7Fdg47D5dCmb4eLSBfKxdH3UGF6vTJ7i/+JZq/c6uI8BFF+SiKp8Fw0rtgj hSoZjLztnUlYubNQst3SQyWmJzCBhhvvx2L/AYo0KBcfj7yiTZUyC/zN13UgHrCG UoMsyf8A9sFg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=OnvNCphjJaHAnrK096oFYoteidw=; b=JqJFX iMlaMG0WGHAGhivIcZF11ocdJ4pNVj7ua7o5OsBidOCbAeA5tcTDj6LgaMHfarET p6yPz5VrhXir1yvg/7mrqYvReTQIxeDjdtVwmiE02SLBUI6jOrK3xFlUBt8qfYqa P3Q3Mn/gRzQkw7mYBtBJ67B3Sqh3dLAI1boCs0= Received: (qmail 37828 invoked by alias); 8 Jun 2017 21:13:50 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 37704 invoked by uid 89); 8 Jun 2017 21:13:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=st64, brace, 121520 X-HELO: mail-qt0-f173.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=af9FvmJOQCAc1ohbc9bbJ3yhNUBZTt5MGJdzyD8l8Ig=; b=RsY0KdTyo/gBwelEN31L4H36+nDNMWu0EKB0uBUYJWHnCRPkNhse/VMMgbnSZvD2e+ Zj24mcBwaCemQhKpff3LAJCJBlkQLRAKWEWqlQpADtzw/0FU6ViUDD+6DyQvjNJ/YxG9 albrn+eXGxKuxpbWpOu4mbFZWicl7qzKROaLj+dEVlHwWxCkz1G2dShTEUCAhtmHfH4h mTB0Zd4InD+Y3WxUW9+0uckOjgW0GaHSpHjKaAecZ+NLX6RVDmHoWaTRVeFWjprViNcC Z5EXxe7rJimdcSA6O/yLz3pZIusxu22VnpGgOAthytiL/ECL+gfLxd4bHq2B0Iyjhb4M eX+Q== X-Gm-Message-State: AODbwcBe/ls+JGe7Dhy3SuGqk6lrgnxXHCKq/EDaQ5Qb9slnSyesXQEo pEHzhy5vDn0WqiUWP5x+1w== X-Received: by 10.237.62.8 with SMTP id l8mr46307905qtf.20.1496956424837; Thu, 08 Jun 2017 14:13:44 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 01/17] posix: Sync glob with gnulib [BZ #1062] Date: Thu, 8 Jun 2017 18:13:15 -0300 Message-Id: <1496956411-25594-2-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch syncs posix/glob.c implementation with gnulib version 1540f34. The main differences to gnulib code: 1. Commit 44c637c (Properly initialize glob structure with GLOB_BRACE|GLOB_DOOFFS) which fixes BZ# 20707. 2. No inclusion of flexmember.h header and its usage on glob. The code is meant to be rewritten and header is unrequired in next patch in this set. 3. An additional define (GLOB_COMPAT_BUILD) to avoid building size_and_wrapv and gblo_use_alloca twice on some configurations (i368 compat code) due multiple inclusion. The main changes are: - Header organization mostly due gnulib requirements. It leads to some simplification and less conditional includes. - Use of glob_use_alloca with wraps up __libc_use_alloca with saturated math for the total size calculation. - Simplify some size allocation overflow calculation. - Some fixed on non supported glibc systems. - Some comments adjustments. The changes does not alter current glob internal semantic. I also added a missing globfree on posix/globtest.c (it helps silence some valgrind or other memory profilers). [BZ #1062] * posix/glob.c: Sync with gnulib. * posix/globtest.c (main): Add final globfree. * sysdeps/unix/sysv/linux/i386/glob64.c (GLOB_COMPAT_BUILD): Define. --- posix/glob.c | 473 ++++++++++++++++++++-------------- posix/globtest.c | 3 + sysdeps/unix/sysv/linux/i386/glob64.c | 2 + 3 files changed, 285 insertions(+), 193 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index c653809..3c6b033 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -15,7 +15,10 @@ License along with the GNU C Library; if not, see . */ -#ifdef HAVE_CONFIG_H +#ifndef _LIBC +/* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc + optimizes away the pattern == NULL || pglob == NULL tests below. */ +# define _GL_ARG_NONNULL(params) # include #endif @@ -34,22 +37,19 @@ #include /* Needed on stupid SunOS for assert. */ -#if !defined _LIBC || !defined GLOB_ONLY_P -#if defined HAVE_UNISTD_H || defined _LIBC -# include -# ifndef POSIX -# ifdef _POSIX_VERSION -# define POSIX -# endif -# endif +#ifndef GLOB_ONLY_P + +#include +#if !defined POSIX && defined _POSIX_VERSION +# define POSIX #endif -#include +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +# define WINDOWS32 +#endif -#if defined HAVE_STDINT_H || defined _LIBC -# include -#elif !defined UINTPTR_MAX -# define UINTPTR_MAX (~((size_t) 0)) +#ifndef WINDOWS32 +# include #endif #include @@ -57,24 +57,7 @@ # define __set_errno(val) errno = (val) #endif -#if defined HAVE_DIRENT_H || defined __GNU_LIBRARY__ -# include -#else -# define dirent direct -# ifdef HAVE_SYS_NDIR_H -# include -# endif -# ifdef HAVE_SYS_DIR_H -# include -# endif -# ifdef HAVE_NDIR_H -# include -# endif -# ifdef HAVE_VMSDIR_H -# include "vmsdir.h" -# endif /* HAVE_VMSDIR_H */ -#endif - +#include #include #include #include @@ -93,17 +76,16 @@ # endif # define struct_stat64 struct stat64 #else /* !_LIBC */ -# include "getlogin_r.h" -# include "mempcpy.h" -# include "stat-macros.h" -# include "strdup.h" -# define __stat64(fname, buf) stat (fname, buf) -# define struct_stat64 struct stat -# define __stat(fname, buf) stat (fname, buf) -# define __alloca alloca -# define __readdir readdir -# define __readdir64 readdir64 -# define __glob_pattern_p glob_pattern_p +# define __getlogin_r(buf, len) getlogin_r (buf, len) +# define __stat64(fname, buf) stat (fname, buf) +# define __fxstatat64(_, d, f, st, flag) fstatat (d, f, st, flag) +# define struct_stat64 struct stat +# ifndef __MVS__ +# define __alloca alloca +# endif +# define __readdir readdir +# define __glob_pattern_p glob_pattern_p +# define COMPILE_GLOB64 #endif /* _LIBC */ #include @@ -186,7 +168,7 @@ readdir_result_might_be_dir (struct readdir_result d) D_INO_TO_RESULT (source) \ } -#endif /* !defined _LIBC || !defined GLOB_ONLY_P */ +#endif /* !defined GLOB_ONLY_P */ /* Call gl_readdir on STREAM. This macro can be overridden to reduce type safety if an old interface version needs to be supported. */ @@ -230,13 +212,74 @@ convert_dirent64 (const struct dirent64 *source) # define attribute_hidden #endif +#ifndef __attribute_noinline__ +# if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 1) +# define __attribute_noinline__ /* Ignore */ +#else +# define __attribute_noinline__ __attribute__ ((__noinline__)) +# endif +#endif + +#ifndef __glibc_unlikely +# define __glibc_unlikely(expr) __builtin_expect (expr, 0) +#endif + +#ifndef _LIBC +/* The results of opendir() in this file are not used with dirfd and fchdir, + and we do not leak fds to any single-threaded code that could use stdio, + therefore save some unnecessary recursion in fchdir.c and opendir_safer.c. + FIXME - if the kernel ever adds support for multi-thread safety for + avoiding standard fds, then we should use opendir_safer. */ +# ifdef GNULIB_defined_opendir +# undef opendir +# endif +# ifdef GNULIB_defined_closedir +# undef closedir +# endif + +/* Just use malloc. */ +# define __libc_use_alloca(n) false +# define alloca_account(len, avar) ((void) (len), (void) (avar), (void *) 0) +# define extend_alloca_account(buf, len, newlen, avar) \ + ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0) +#endif + +/* Set *R = A + B. Return true if the answer is mathematically + incorrect due to overflow; in this case, *R is the low order + bits of the correct answer.. */ + +static bool size_add_wrapv (size_t a, size_t b, size_t *r); +static bool glob_use_alloca (size_t alloca_used, size_t len); + +/* We must not compile this function twice. */ +#ifndef GLOB_COMPAT_BUILD +static bool +size_add_wrapv (size_t a, size_t b, size_t *r) +{ +#if 5 <= __GNUC__ + return __builtin_add_overflow (a, b, r); +#else + *r = a + b; + return *r < a; +#endif +} + +static bool +glob_use_alloca (size_t alloca_used, size_t len) +{ + size_t size; + return (!size_add_wrapv (alloca_used, len, &size) + && __libc_use_alloca (size)); +} +#endif + static int glob_in_dir (const char *pattern, const char *directory, int flags, int (*errfunc) (const char *, int), glob_t *pglob, size_t alloca_used); extern int __glob_pattern_type (const char *pattern, int quote) attribute_hidden; -#if !defined _LIBC || !defined GLOB_ONLY_P +#ifndef GLOB_ONLY_P static int prefix_array (const char *prefix, char **array, size_t n) __THROWNL; static int collated_compare (const void *, const void *) __THROWNL; @@ -265,16 +308,16 @@ next_brace_sub (const char *cp, int flags) return *cp != '\0' ? cp : NULL; } -#endif /* !defined _LIBC || !defined GLOB_ONLY_P */ +#endif /* !defined GLOB_ONLY_P */ /* Do glob searching for PATTERN, placing results in PGLOB. The bits defined above may be set in FLAGS. If a directory cannot be opened or read and ERRFUNC is not nil, it is called with the pathname that caused the error, and the - `errno' value from the failing call; if it returns non-zero - `glob' returns GLOB_ABORTED; if it returns zero, the error is ignored. + 'errno' value from the failing call; if it returns non-zero + 'glob' returns GLOB_ABORTED; if it returns zero, the error is ignored. If memory cannot be allocated for PGLOB, GLOB_NOSPACE is returned. - Otherwise, `glob' returns zero. */ + Otherwise, 'glob' returns zero. */ int #ifdef GLOB_ATTRIBUTE GLOB_ATTRIBUTE @@ -292,9 +335,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), int malloc_dirname = 0; glob_t dirs; int retval = 0; -#ifdef _LIBC size_t alloca_used = 0; -#endif if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0) { @@ -308,7 +349,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), flags |= GLOB_ONLYDIR; if (!(flags & GLOB_DOOFFS)) - /* Have to do this so `globfree' knows where to start freeing. It + /* Have to do this so 'globfree' knows where to start freeing. It also makes all the code that uses gl_offs simpler. */ pglob->gl_offs = 0; @@ -363,7 +404,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (begin != NULL) { /* Allocate working buffer large enough for our work. Note that - we have at least an opening and closing brace. */ + we have at least an opening and closing brace. */ size_t firstc; char *alt_start; const char *p; @@ -372,16 +413,21 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), size_t rest_len; char *onealt; size_t pattern_len = strlen (pattern) - 1; -#ifdef _LIBC - int alloca_onealt = __libc_use_alloca (alloca_used + pattern_len); + int alloca_onealt = glob_use_alloca (alloca_used, pattern_len); if (alloca_onealt) onealt = alloca_account (pattern_len, alloca_used); else -#endif { - onealt = (char *) malloc (pattern_len); + onealt = malloc (pattern_len); if (onealt == NULL) - return GLOB_NOSPACE; + { + if (!(flags & GLOB_APPEND)) + { + pglob->gl_pathc = 0; + pglob->gl_pathv = NULL; + } + return GLOB_NOSPACE; + } } /* We know the prefix for all sub-patterns. */ @@ -392,14 +438,11 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), next = next_brace_sub (begin + 1, flags); if (next == NULL) { - /* It is an illegal expression. */ + /* It is an invalid expression. */ illegal_brace: -#ifdef _LIBC if (__glibc_unlikely (!alloca_onealt)) -#endif free (onealt); - flags &= ~GLOB_BRACE; - goto no_brace; + return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob); } /* Now find the end of the whole brace expression. */ @@ -437,9 +480,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* If we got an error, return it. */ if (result && result != GLOB_NOMATCH) { -#ifdef _LIBC if (__glibc_unlikely (!alloca_onealt)) -#endif free (onealt); if (!(flags & GLOB_APPEND)) { @@ -458,9 +499,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), assert (next != NULL); } -#ifdef _LIBC if (__glibc_unlikely (!alloca_onealt)) -#endif free (onealt); if (pglob->gl_pathc != firstc) @@ -471,7 +510,6 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } } - no_brace: oldcount = pglob->gl_pathc + pglob->gl_offs; /* Find the filename. */ @@ -536,22 +574,20 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), char *drive_spec; ++dirlen; - drive_spec = (char *) __alloca (dirlen + 1); + drive_spec = __alloca (dirlen + 1); *((char *) mempcpy (drive_spec, pattern, dirlen)) = '\0'; /* For now, disallow wildcards in the drive spec, to prevent infinite recursion in glob. */ if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE))) return GLOB_NOMATCH; - /* If this is "d:pattern", we need to copy `:' to DIRNAME + /* If this is "d:pattern", we need to copy ':' to DIRNAME as well. If it's "d:/pattern", don't remove the slash from "d:/", since "d:" and "d:/" are not the same.*/ } #endif -#ifdef _LIBC - if (__libc_use_alloca (alloca_used + dirlen + 1)) + if (glob_use_alloca (alloca_used, dirlen + 1)) newp = alloca_account (dirlen + 1, alloca_used); else -#endif { newp = malloc (dirlen + 1); if (newp == NULL) @@ -572,6 +608,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* "pattern/". Expand "pattern", appending slashes. */ { int orig_flags = flags; + int val; if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\') { /* "pattern\\/". Remove the final backslash if it hasn't @@ -585,7 +622,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC); } } - int val = glob (dirname, flags | GLOB_MARK, errfunc, pglob); + val = glob (dirname, flags | GLOB_MARK, errfunc, pglob); if (val == 0) pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK)); @@ -602,7 +639,6 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } } -#ifndef VMS if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~') { if (dirname[1] == '\0' || dirname[1] == '/' @@ -617,95 +653,129 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), home_dir = "SYS:"; # else # ifdef WINDOWS32 + /* Windows NT defines HOMEDRIVE and HOMEPATH. But give preference + to HOME, because the user can change HOME. */ if (home_dir == NULL || home_dir[0] == '\0') - home_dir = "c:/users/default"; /* poor default */ + { + const char *home_drive = getenv ("HOMEDRIVE"); + const char *home_path = getenv ("HOMEPATH"); + + if (home_drive != NULL && home_path != NULL) + { + size_t home_drive_len = strlen (home_drive); + size_t home_path_len = strlen (home_path); + char *mem = alloca (home_drive_len + home_path_len + 1); + + memcpy (mem, home_drive, home_drive_len); + memcpy (mem + home_drive_len, home_path, home_path_len + 1); + home_dir = mem; + } + else + home_dir = "c:/users/default"; /* poor default */ + } # else if (home_dir == NULL || home_dir[0] == '\0') { int success; char *name; + int malloc_name = 0; size_t buflen = GET_LOGIN_NAME_MAX () + 1; if (buflen == 0) /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try a moderate value. */ buflen = 20; - name = alloca_account (buflen, alloca_used); + if (glob_use_alloca (alloca_used, buflen)) + name = alloca_account (buflen, alloca_used); + else + { + name = malloc (buflen); + if (name == NULL) + { + retval = GLOB_NOSPACE; + goto out; + } + malloc_name = 1; + } success = __getlogin_r (name, buflen) == 0; if (success) { struct passwd *p; -# if defined HAVE_GETPWNAM_R || defined _LIBC - long int pwbuflen = GETPW_R_SIZE_MAX (); + char *malloc_pwtmpbuf = NULL; char *pwtmpbuf; +# if defined HAVE_GETPWNAM_R || defined _LIBC + long int pwbuflenmax = GETPW_R_SIZE_MAX (); + size_t pwbuflen = pwbuflenmax; struct passwd pwbuf; - int malloc_pwtmpbuf = 0; int save = errno; # ifndef _LIBC - if (pwbuflen == -1) + if (! (0 < pwbuflenmax && pwbuflenmax <= SIZE_MAX)) /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a moderate value. */ pwbuflen = 1024; # endif - if (__libc_use_alloca (alloca_used + pwbuflen)) + if (glob_use_alloca (alloca_used, pwbuflen)) pwtmpbuf = alloca_account (pwbuflen, alloca_used); else { pwtmpbuf = malloc (pwbuflen); if (pwtmpbuf == NULL) { + if (__glibc_unlikely (malloc_name)) + free (name); retval = GLOB_NOSPACE; goto out; } - malloc_pwtmpbuf = 1; + malloc_pwtmpbuf = pwtmpbuf; } while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p) != 0) { + size_t newlen; + bool v; if (errno != ERANGE) { p = NULL; break; } - - if (!malloc_pwtmpbuf - && __libc_use_alloca (alloca_used - + 2 * pwbuflen)) + v = size_add_wrapv (pwbuflen, pwbuflen, &newlen); + if (!v && malloc_pwtmpbuf == NULL + && glob_use_alloca (alloca_used, newlen)) pwtmpbuf = extend_alloca_account (pwtmpbuf, pwbuflen, - 2 * pwbuflen, - alloca_used); + newlen, alloca_used); else { - char *newp = realloc (malloc_pwtmpbuf - ? pwtmpbuf : NULL, - 2 * pwbuflen); + char *newp = (v ? NULL + : realloc (malloc_pwtmpbuf, newlen)); if (newp == NULL) { - if (__glibc_unlikely (malloc_pwtmpbuf)) - free (pwtmpbuf); + free (malloc_pwtmpbuf); + if (__glibc_unlikely (malloc_name)) + free (name); retval = GLOB_NOSPACE; goto out; } - pwtmpbuf = newp; - pwbuflen = 2 * pwbuflen; - malloc_pwtmpbuf = 1; + malloc_pwtmpbuf = pwtmpbuf = newp; } + pwbuflen = newlen; __set_errno (save); } # else p = getpwnam (name); # endif + if (__glibc_unlikely (malloc_name)) + free (name); if (p != NULL) { - if (!malloc_pwtmpbuf) + if (malloc_pwtmpbuf == NULL) home_dir = p->pw_dir; else { size_t home_dir_len = strlen (p->pw_dir) + 1; - if (__libc_use_alloca (alloca_used + home_dir_len)) + if (glob_use_alloca (alloca_used, home_dir_len)) home_dir = alloca_account (home_dir_len, alloca_used); else @@ -720,23 +790,30 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), malloc_home_dir = 1; } memcpy (home_dir, p->pw_dir, home_dir_len); - - free (pwtmpbuf); } } + free (malloc_pwtmpbuf); + } + else + { + if (__glibc_unlikely (malloc_name)) + free (name); } } if (home_dir == NULL || home_dir[0] == '\0') { + if (__glibc_unlikely (malloc_home_dir)) + free (home_dir); if (flags & GLOB_TILDE_CHECK) { - if (__glibc_unlikely (malloc_home_dir)) - free (home_dir); retval = GLOB_NOMATCH; goto out; } else - home_dir = (char *) "~"; /* No luck. */ + { + home_dir = (char *) "~"; /* No luck. */ + malloc_home_dir = 0; + } } # endif /* WINDOWS32 */ # endif @@ -754,8 +831,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { char *newp; size_t home_len = strlen (home_dir); - int use_alloca = __libc_use_alloca (alloca_used - + home_len + dirlen); + int use_alloca = glob_use_alloca (alloca_used, home_len + dirlen); if (use_alloca) newp = alloca_account (home_len + dirlen, alloca_used); else @@ -779,6 +855,9 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), dirname = newp; dirlen += home_len - 1; malloc_dirname = !use_alloca; + + if (__glibc_unlikely (malloc_home_dir)) + free (home_dir); } dirname_modified = 1; } @@ -806,7 +885,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), else { char *newp; - if (__libc_use_alloca (alloca_used + (end_name - dirname))) + if (glob_use_alloca (alloca_used, end_name - dirname)) newp = alloca_account (end_name - dirname, alloca_used); else { @@ -851,20 +930,21 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* Look up specific user's home directory. */ { struct passwd *p; + char *malloc_pwtmpbuf = NULL; # if defined HAVE_GETPWNAM_R || defined _LIBC - long int buflen = GETPW_R_SIZE_MAX (); + long int buflenmax = GETPW_R_SIZE_MAX (); + size_t buflen = buflenmax; char *pwtmpbuf; - int malloc_pwtmpbuf = 0; struct passwd pwbuf; int save = errno; # ifndef _LIBC - if (buflen == -1) - /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a + if (! (0 <= buflenmax && buflenmax <= SIZE_MAX)) + /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a moderate value. */ buflen = 1024; # endif - if (__libc_use_alloca (alloca_used + buflen)) + if (glob_use_alloca (alloca_used, buflen)) pwtmpbuf = alloca_account (buflen, alloca_used); else { @@ -877,32 +957,32 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), retval = GLOB_NOSPACE; goto out; } - malloc_pwtmpbuf = 1; + malloc_pwtmpbuf = pwtmpbuf; } while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0) { + size_t newlen; + bool v; if (errno != ERANGE) { p = NULL; break; } - if (!malloc_pwtmpbuf - && __libc_use_alloca (alloca_used + 2 * buflen)) + v = size_add_wrapv (buflen, buflen, &newlen); + if (!v && malloc_pwtmpbuf == NULL + && glob_use_alloca (alloca_used, newlen)) pwtmpbuf = extend_alloca_account (pwtmpbuf, buflen, - 2 * buflen, alloca_used); + newlen, alloca_used); else { - char *newp = realloc (malloc_pwtmpbuf ? pwtmpbuf : NULL, - 2 * buflen); + char *newp = v ? NULL : realloc (malloc_pwtmpbuf, newlen); if (newp == NULL) { - if (__glibc_unlikely (malloc_pwtmpbuf)) - free (pwtmpbuf); + free (malloc_pwtmpbuf); goto nomem_getpw; } - pwtmpbuf = newp; - malloc_pwtmpbuf = 1; + malloc_pwtmpbuf = pwtmpbuf = newp; } __set_errno (save); } @@ -923,7 +1003,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), free (dirname); malloc_dirname = 0; - if (__libc_use_alloca (alloca_used + home_len + rest_len + 1)) + if (glob_use_alloca (alloca_used, home_len + rest_len + 1)) dirname = alloca_account (home_len + rest_len + 1, alloca_used); else @@ -931,8 +1011,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), dirname = malloc (home_len + rest_len + 1); if (dirname == NULL) { - if (__glibc_unlikely (malloc_pwtmpbuf)) - free (pwtmpbuf); + free (malloc_pwtmpbuf); retval = GLOB_NOSPACE; goto out; } @@ -944,13 +1023,11 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), dirlen = home_len + rest_len; dirname_modified = 1; - if (__glibc_unlikely (malloc_pwtmpbuf)) - free (pwtmpbuf); + free (malloc_pwtmpbuf); } else { - if (__glibc_unlikely (malloc_pwtmpbuf)) - free (pwtmpbuf); + free (malloc_pwtmpbuf); if (flags & GLOB_TILDE_CHECK) /* We have to regard it as an error if we cannot find the @@ -961,7 +1038,6 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } # endif /* Not Amiga && not WINDOWS32. */ } -#endif /* Not VMS. */ /* Now test whether we looked for "~" or "~NAME". In this case we can give the answer now. */ @@ -980,19 +1056,19 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), size_t newcount = pglob->gl_pathc + pglob->gl_offs; char **new_gl_pathv; - if (newcount > UINTPTR_MAX - (1 + 1) - || newcount + 1 + 1 > ~((size_t) 0) / sizeof (char *)) + if (newcount > SIZE_MAX / sizeof (char *) - 2) { nospace: + if (__glibc_unlikely (malloc_dirname)) + free (dirname); free (pglob->gl_pathv); pglob->gl_pathv = NULL; pglob->gl_pathc = 0; return GLOB_NOSPACE; } - new_gl_pathv - = (char **) realloc (pglob->gl_pathv, - (newcount + 1 + 1) * sizeof (char *)); + new_gl_pathv = realloc (pglob->gl_pathv, + (newcount + 2) * sizeof (char *)); if (new_gl_pathv == NULL) goto nospace; pglob->gl_pathv = new_gl_pathv; @@ -1009,9 +1085,14 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } else { - pglob->gl_pathv[newcount] = strdup (dirname); - if (pglob->gl_pathv[newcount] == NULL) - goto nospace; + if (__glibc_unlikely (malloc_dirname)) + pglob->gl_pathv[newcount] = dirname; + else + { + pglob->gl_pathv[newcount] = strdup (dirname); + if (pglob->gl_pathv[newcount] == NULL) + goto nospace; + } } pglob->gl_pathv[++newcount] = NULL; ++pglob->gl_pathc; @@ -1078,7 +1159,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { size_t old_pathc; -#ifdef SHELL +#ifdef SHELL { /* Make globbing interruptible in the bash shell. */ extern int interrupt_state; @@ -1134,22 +1215,20 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), size_t newcount = pglob->gl_pathc + pglob->gl_offs; char **new_gl_pathv; - if (newcount > UINTPTR_MAX - 2 - || newcount + 2 > ~((size_t) 0) / sizeof (char *)) + if (newcount > SIZE_MAX / sizeof (char *) - 2) { nospace2: globfree (&dirs); return GLOB_NOSPACE; } - new_gl_pathv = (char **) realloc (pglob->gl_pathv, - (newcount + 2) - * sizeof (char *)); + new_gl_pathv = realloc (pglob->gl_pathv, + (newcount + 2) * sizeof (char *)); if (new_gl_pathv == NULL) goto nospace2; pglob->gl_pathv = new_gl_pathv; - pglob->gl_pathv[newcount] = __strdup (pattern); + pglob->gl_pathv[newcount] = strdup (pattern); if (pglob->gl_pathv[newcount] == NULL) { globfree (&dirs); @@ -1276,7 +1355,7 @@ libc_hidden_def (glob) #endif -#if !defined _LIBC || !defined GLOB_ONLY_P +#ifndef GLOB_ONLY_P /* Free storage allocated in PGLOB by a previous `glob' call. */ void @@ -1300,8 +1379,8 @@ libc_hidden_def (globfree) static int collated_compare (const void *a, const void *b) { - const char *const s1 = *(const char *const * const) a; - const char *const s2 = *(const char *const * const) b; + char *const *ps1 = a; char *s1 = *ps1; + char *const *ps2 = b; char *s2 = *ps2; if (s1 == s2) return 0; @@ -1351,7 +1430,7 @@ prefix_array (const char *dirname, char **array, size_t n) for (i = 0; i < n; ++i) { size_t eltlen = strlen (array[i]) + 1; - char *new = (char *) malloc (dirlen + 1 + eltlen); + char *new = malloc (dirlen + 1 + eltlen); if (new == NULL) { while (i > 0) @@ -1373,7 +1452,7 @@ prefix_array (const char *dirname, char **array, size_t n) /* We must not compile this function twice. */ -#if !defined _LIBC || !defined NO_GLOB_PATTERN_P +#ifndef NO_GLOB_PATTERN_P int __glob_pattern_type (const char *pattern, int quote) { @@ -1421,50 +1500,55 @@ weak_alias (__glob_pattern_p, glob_pattern_p) # endif #endif -#endif /* !GLOB_ONLY_P */ - /* We put this in a separate function mainly to allow the memory allocated with alloca to be recycled. */ -#if !defined _LIBC || !defined GLOB_ONLY_P static int __attribute_noinline__ link_exists2_p (const char *dir, size_t dirlen, const char *fname, glob_t *pglob -# ifndef _LIBC +# if !defined _LIBC && !HAVE_FSTATAT , int flags # endif ) { size_t fnamelen = strlen (fname); - char *fullname = (char *) __alloca (dirlen + 1 + fnamelen + 1); + char *fullname = __alloca (dirlen + 1 + fnamelen + 1); struct stat st; -# ifndef _LIBC - struct_stat64 st64; -# endif mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1), fname, fnamelen + 1); -# ifdef _LIBC - return (*pglob->gl_stat) (fullname, &st) == 0; -# else - return ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) - ? (*pglob->gl_stat) (fullname, &st) - : __stat64 (fullname, &st64)) == 0); +# if !defined _LIBC && !HAVE_FSTATAT + if (__builtin_expect ((flags & GLOB_ALTDIRFUNC) == 0, 1)) + { + struct_stat64 st64; + return __stat64 (fullname, &st64) == 0; + } # endif + return (*pglob->gl_stat) (fullname, &st) == 0; } -# ifdef _LIBC -# define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \ - (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) \ - ? link_exists2_p (dirname, dirnamelen, fname, pglob) \ - : ({ struct stat64 st64; \ - __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; })) + +/* Return true if DIR/FNAME exists. */ +static int +link_exists_p (int dfd, const char *dir, size_t dirlen, const char *fname, + glob_t *pglob, int flags) +{ +# if defined _LIBC || HAVE_FSTATAT + if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)) + return link_exists2_p (dir, dirlen, fname, pglob); + else + { + /* dfd cannot be -1 here, because dirfd never returns -1 on + glibc, or on hosts that have fstatat. */ + struct_stat64 st64; + return __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; + } # else -# define link_exists_p(dfd, dirname, dirnamelen, fname, pglob, flags) \ - link_exists2_p (dirname, dirnamelen, fname, pglob, flags) + return link_exists2_p (dir, dirlen, fname, pglob, flags); # endif -#endif +} +#endif /* !defined GLOB_ONLY_P */ /* Like `glob', but PATTERN is a final pathname component, @@ -1492,6 +1576,7 @@ glob_in_dir (const char *pattern, const char *directory, int flags, size_t cur = 0; int meta; int save; + int result; alloca_used += sizeof (init_names); @@ -1516,14 +1601,16 @@ glob_in_dir (const char *pattern, const char *directory, int flags, struct_stat64 st64; } ust; size_t patlen = strlen (pattern); - int alloca_fullname = __libc_use_alloca (alloca_used - + dirlen + 1 + patlen + 1); + 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 (dirlen + 1 + patlen + 1, alloca_used); + fullname = alloca_account (fullsize, alloca_used); else { - fullname = malloc (dirlen + 1 + patlen + 1); + fullname = malloc (fullsize); if (fullname == NULL) return GLOB_NOSPACE; } @@ -1555,10 +1642,8 @@ glob_in_dir (const char *pattern, const char *directory, int flags, } else { -#ifdef _LIBC int dfd = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) ? -1 : dirfd ((DIR *) stream)); -#endif int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) #if defined _AMIGA || defined VMS @@ -1607,7 +1692,7 @@ glob_in_dir (const char *pattern, const char *directory, int flags, size_t size = (sizeof (struct globnames) + ((count - INITIAL_COUNT) * sizeof (char *))); - if (__libc_use_alloca (alloca_used + size)) + if (glob_use_alloca (alloca_used, size)) newnames = names_alloca = alloca_account (size, alloca_used); else if ((newnames = malloc (size)) @@ -1623,6 +1708,8 @@ glob_in_dir (const char *pattern, const char *directory, int flags, goto memory_error; ++cur; ++nfound; + if (SIZE_MAX - pglob->gl_offs <= nfound) + goto memory_error; } } } @@ -1633,36 +1720,35 @@ glob_in_dir (const char *pattern, const char *directory, int flags, { size_t len = strlen (pattern); nfound = 1; - names->name[cur] = (char *) malloc (len + 1); + names->name[cur] = malloc (len + 1); if (names->name[cur] == NULL) goto memory_error; *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0'; } - int result = GLOB_NOMATCH; + result = GLOB_NOMATCH; if (nfound != 0) { + char **new_gl_pathv; result = 0; - if (pglob->gl_pathc > UINTPTR_MAX - pglob->gl_offs - || pglob->gl_pathc + pglob->gl_offs > UINTPTR_MAX - nfound - || pglob->gl_pathc + pglob->gl_offs + nfound > UINTPTR_MAX - 1 - || (pglob->gl_pathc + pglob->gl_offs + nfound + 1 - > UINTPTR_MAX / sizeof (char *))) + if (SIZE_MAX / sizeof (char *) - pglob->gl_pathc + < pglob->gl_offs + nfound + 1) goto memory_error; - char **new_gl_pathv; new_gl_pathv - = (char **) realloc (pglob->gl_pathv, - (pglob->gl_pathc + pglob->gl_offs + nfound + 1) - * sizeof (char *)); + = realloc (pglob->gl_pathv, + (pglob->gl_pathc + pglob->gl_offs + nfound + 1) + * sizeof (char *)); + if (new_gl_pathv == NULL) { memory_error: while (1) { struct globnames *old = names; - for (size_t i = 0; i < cur; ++i) + size_t i; + for (i = 0; i < cur; ++i) free (names->name[i]); names = names->next; /* NB: we will not leak memory here if we exit without @@ -1687,7 +1773,8 @@ glob_in_dir (const char *pattern, const char *directory, int flags, while (1) { struct globnames *old = names; - for (size_t i = 0; i < cur; ++i) + size_t i; + for (i = 0; i < cur; ++i) new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++] = names->name[i]; names = names->next; diff --git a/posix/globtest.c b/posix/globtest.c index 878ae33..7ffcb91 100644 --- a/posix/globtest.c +++ b/posix/globtest.c @@ -114,5 +114,8 @@ main (int argc, char *argv[]) g.gl_pathv[i] ? g.gl_pathv[i] : "(null)", quotes ? "'" : ""); } + + globfree (&g); + return 0; } diff --git a/sysdeps/unix/sysv/linux/i386/glob64.c b/sysdeps/unix/sysv/linux/i386/glob64.c index f681951..9c7abd8 100644 --- a/sysdeps/unix/sysv/linux/i386/glob64.c +++ b/sysdeps/unix/sysv/linux/i386/glob64.c @@ -70,6 +70,8 @@ int __old_glob64 (const char *__pattern, int __flags, #define GLOB_ONLY_P 1 +#define GLOB_COMPAT_BUILD 1 + #include compat_symbol (libc, __old_glob64, glob64, GLIBC_2_1); From patchwork Thu Jun 8 21:13:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103407 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632301qgd; Thu, 8 Jun 2017 14:14:22 -0700 (PDT) X-Received: by 10.84.218.72 with SMTP id f8mr37895500plm.33.1496956462473; Thu, 08 Jun 2017 14:14:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956462; cv=none; d=google.com; s=arc-20160816; b=ebE6CgNMQVo9U20a4BSeo/FhHFYRmx1anSNDNiKQmvTYlmYdS3ZpzQppN9U53TideD LiVlsXhSwW7tLLL5EG2kV3ike1CvUBk9FfGXrtJvsjUfqROacnMGj9GDK+DxjmGZ7bK8 /em9YTZJaVPGKPAhq4J/Wc3yaYd0sHu5gEvj38fUhKMAOC0XL1amASvwIDInrd7tt+5i ABTl9jET+dymF0ULTl89WpEzZp4fGAHaQVZoMPl/RBhRS1r4JMCjXhpD+kqyu34AQBr1 5gT2EdbmQqtbQzmWxLBm5pqPm10sbc2SnnsO2cCpun8C58sbZ974ghQ+RznLZ2qPDodi FNbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=NpQ2B8hzONIsIoS72b6tUIELSb18Bnfi+A7bfVBO9zI=; b=l3uF3JZOFRPy9+0uwMcu+hBrbIeKgNvrUk8hBKQZCh5XitAyhe9qM5DOLNKScX5oTu HAwrVrBH+BTcp5ZSx8s+C8q7+Z3Va85V4U4s4x9Eujqb10gvaZxJS52tiLJOQjJOS4ps 9cVQZWQSkJqwy0Vaj8APHLc06oKjtD0ATwEuRcC7CaCoIrBnBh5c+hiHAvlWSISYFaUi NUiFLdase6UQ9xrdwef40+KRHPVBolVkHoBhcJC2bKvbG0n4hQTtC0oEwTYoWySLBxbt QHWtgW3Vqu03s9hBA0ejG9I6ep4ViiSoQ+qwxdOPVSiPJrLPocfIQNPlycTyfHE7kNGG ve+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80201-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80201-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id s80si5287979pfi.388.2017.06.08.14.14.22 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:14:22 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80201-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80201-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80201-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=oDqTh+rz+ZrNbN5CuCYGqdbvu60OhV7 tABWyCGkDwWCqxN39sbidVODChZ8MiecL/kqGmsxfQrnjchQBuU5kBW+coem5mma 7Kc0a7armimfzQkHan7Kt7hmRPxFJzNXIjN412keFUMFRps+qyRto0gXJ+XMNxAZ lsdCr6X7rtpM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=YPTaOs3zAmH1fPD5ZbD/cDtDMEM=; b=BLB2C l0yOOPc3KF1/p3PH87K8r+rk49JmxT+NeFFmKGn2bE+eo7Z8ypTlHuKQeYJ/ZzG+ g5nu4ekYif5pmHAEsGpzcgBpdW5kBbT/ScnVkSdFJHSchGHwLBPKmIJzu6B8l3E8 BBEAVpyET8TZHR0xXDqXpNlJ27se8J1pL0AYvY= Received: (qmail 37783 invoked by alias); 8 Jun 2017 21:13:50 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 37677 invoked by uid 89); 8 Jun 2017 21:13:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=st64, UD:st, sk:danglin X-HELO: mail-qt0-f181.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=NpQ2B8hzONIsIoS72b6tUIELSb18Bnfi+A7bfVBO9zI=; b=JBV67lX+o+yZ4fpr6Po/HbLgn95Ka8CUo9JYd7hLRAcV467gX76Luwjw6Qa3Iu+XI+ jV/BAU5SZI8k0bVtXt5TTWpX8Br0oXpPGFZ/bi6TmS3RWambJqJlZkUNbxhy3iBGrsVU dkJRvjZExoKK64rNLQXQpFinPAnRf6BnA7V74FckP+wjxtb+fZF37McP1MFTKgneJS+y kubajF+pir1TxjcZ0M6nvDXwk5/tB5frg9a4wosSffxBvKIvxB0c9BLd7jFqOabrqGuZ TYV+UL5lmJn4tHyKml5uJafYmv0r+9+2zUETzSoVVnyy5W34yS/WSCl1poGA45WtG0Ny VaqQ== X-Gm-Message-State: AKS2vOz8l1GyvlUMGm77EtPDcSlDYcCQraeiM78h1OrR0O/g6c0dZkoy c0Kxg82rEazEy+NdotF3mg== X-Received: by 10.55.66.20 with SMTP id p20mr6876605qka.29.1496956427155; Thu, 08 Jun 2017 14:13:47 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 02/17] posix: Allow glob to match dangling symlinks [BZ #866] Date: Thu, 8 Jun 2017 18:13:16 -0300 Message-Id: <1496956411-25594-3-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch makes glob match dangling symlinks. Compared to other glob implementation (*BSD, bash, musl, and other shells as well), GLIBC seems the be the only one that does not match dangling symlinks. As for comment #5 in BZ #866, POSIX does not have any strict specification for dangling symlinks match and it is reasonable that trying to glob everything in a path should return all types of files (such as for a 'rm *'). Also, comment #7 shows even more example where GLIBC current behavior is unexepected. I avoided adding another GNU specific flag to set this behavior and instead make it the default. Although this change the semanthic from previous implementation, I think adding another compat symbol to be really unecessary as from aforementioned reasons (current behavior not defined in any standard, general idea of different implementation is to list dangling symbols). Checked on x86_64-linux-gnu. * posix/Makefile (tests): Add tst-glob_symlinks and remove tst-glob3. * posix/bug-glob1.c: Remove file. * posix/glob.c (glob): Match dangling symlinks. (link_exists2_p): Remove function. (link_exists_p): Likewise. * posix/tst-glob_symlinks.c: New file. * sysdeps/gnu/glob64.c (__stat): Redefine to __lstat. * sysdeps/unix/sysv/linux/i386/glob64.c (__stat): Likewise. --- posix/Makefile | 5 +- posix/bug-glob1.c | 88 ---------------------- posix/glob.c | 122 ++++++++----------------------- posix/tst-glob_symlinks.c | 133 ++++++++++++++++++++++++++++++++++ sysdeps/gnu/glob64.c | 4 +- sysdeps/unix/sysv/linux/i386/glob64.c | 4 +- 6 files changed, 171 insertions(+), 185 deletions(-) delete mode 100644 posix/bug-glob1.c create mode 100644 posix/tst-glob_symlinks.c -- 2.7.4 diff --git a/posix/Makefile b/posix/Makefile index 52b022c..2c0c9f9 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -69,7 +69,7 @@ tests := test-errno tstgetopt testfnm runtests runptests \ tst-mmap tst-mmap-offset tst-getaddrinfo tst-truncate \ tst-truncate64 tst-fork tst-fnmatch tst-regexloc tst-dir \ tst-chmod bug-regex1 bug-regex2 bug-regex3 bug-regex4 \ - tst-gnuglob tst-regex bug-regex6 bug-regex7 \ + tst-gnuglob tst-glob_symlinks tst-regex bug-regex6 bug-regex7 \ bug-regex8 bug-regex9 bug-regex10 bug-regex11 bug-regex12 \ bug-regex13 bug-regex14 bug-regex15 bug-regex16 \ bug-regex17 bug-regex18 bug-regex19 \ @@ -79,7 +79,7 @@ tests := test-errno tstgetopt testfnm runtests runptests \ tst-nice tst-nanosleep tst-regex2 \ transbug tst-rxspencer tst-pcre tst-boost \ bug-ga1 tst-vfork1 tst-vfork2 tst-vfork3 tst-waitid \ - tst-getaddrinfo2 bug-glob1 bug-glob2 bug-glob3 tst-sysconf \ + tst-getaddrinfo2 bug-glob2 bug-glob3 tst-sysconf \ tst-execvp1 tst-execvp2 tst-execlp1 tst-execlp2 \ tst-execv1 tst-execv2 tst-execl1 tst-execl2 \ tst-execve1 tst-execve2 tst-execle1 tst-execle2 \ @@ -249,7 +249,6 @@ tst-rxspencer-ARGS = --utf8 rxspencer/tests tst-rxspencer-no-utf8-ARGS = rxspencer/tests tst-pcre-ARGS = PCRE.tests tst-boost-ARGS = BOOST.tests -bug-glob1-ARGS = "$(objpfx)" tst-execvp3-ARGS = --test-dir=$(objpfx) testcases.h: TESTS TESTS2C.sed diff --git a/posix/bug-glob1.c b/posix/bug-glob1.c deleted file mode 100644 index 05c2da7..0000000 --- a/posix/bug-glob1.c +++ /dev/null @@ -1,88 +0,0 @@ -/* Test case for globbing dangling symlink. By Ulrich Drepper. */ -#include -#include -#include -#include -#include -#include -#include - - -static void prepare (int argc, char *argv[]); -#define PREPARE prepare -static int do_test (void); -#define TEST_FUNCTION do_test () - -#include "../test-skeleton.c" - - -static char *fname; - -static void -prepare (int argc, char *argv[]) -{ - if (argc < 2) - error (EXIT_FAILURE, 0, "missing argument"); - - size_t len = strlen (argv[1]); - static const char ext[] = "globXXXXXX"; - fname = malloc (len + sizeof (ext)); - if (fname == NULL) - error (EXIT_FAILURE, errno, "cannot create temp file"); - again: - strcpy (stpcpy (fname, argv[1]), ext); - fname = mktemp (fname); - if (fname == NULL || *fname == '\0') - error (EXIT_FAILURE, errno, "cannot create temp file name"); - if (symlink ("bug-glob1-does-not-exist", fname) != 0) - { - if (errno == EEXIST) - goto again; - - error (EXIT_FAILURE, errno, "cannot create symlink"); - } - add_temp_file (fname); -} - - -static int -do_test (void) -{ - glob_t gl; - int retval = 0; - int e; - - e = glob (fname, 0, NULL, &gl); - if (e == 0) - { - printf ("glob(\"%s\") succeeded\n", fname); - retval = 1; - } - globfree (&gl); - - size_t fnamelen = strlen (fname); - char buf[fnamelen + 2]; - - strcpy (buf, fname); - buf[fnamelen - 1] = '?'; - e = glob (buf, 0, NULL, &gl); - if (e == 0) - { - printf ("glob(\"%s\") succeeded\n", buf); - retval = 1; - } - globfree (&gl); - - strcpy (buf, fname); - buf[fnamelen] = '*'; - buf[fnamelen + 1] = '\0'; - e = glob (buf, 0, NULL, &gl); - if (e == 0) - { - printf ("glob(\"%s\") succeeded\n", buf); - retval = 1; - } - globfree (&gl); - - return retval; -} diff --git a/posix/glob.c b/posix/glob.c index 3c6b033..250bff1 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -71,8 +71,8 @@ # define readdir(str) __readdir64 (str) # define getpwnam_r(name, bufp, buf, len, res) \ __getpwnam_r (name, bufp, buf, len, res) -# ifndef __stat64 -# define __stat64(fname, buf) __xstat64 (_STAT_VER, fname, buf) +# ifndef __lstat64 +# define __lstat64(fname, buf) __lxstat64 (_STAT_VER, fname, buf) # endif # define struct_stat64 struct stat64 #else /* !_LIBC */ @@ -1049,9 +1049,9 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* Return the directory if we don't check for error or if it exists. */ if ((flags & GLOB_NOCHECK) || (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)) - ? ((*pglob->gl_stat) (dirname, &st) == 0 + ? ((*pglob->gl_lstat) (dirname, &st) == 0 && S_ISDIR (st.st_mode)) - : (__stat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode))))) + : (__lstat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode))))) { size_t newcount = pglob->gl_pathc + pglob->gl_offs; char **new_gl_pathv; @@ -1318,10 +1318,10 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), for (i = oldcount; i < pglob->gl_pathc + pglob->gl_offs; ++i) if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) - ? ((*pglob->gl_stat) (pglob->gl_pathv[i], &st) == 0 - && S_ISDIR (st.st_mode)) - : (__stat64 (pglob->gl_pathv[i], &st64) == 0 - && S_ISDIR (st64.st_mode)))) + ? ((*pglob->gl_lstat) (pglob->gl_pathv[i], &st) == 0 + && (S_ISDIR (st.st_mode) || S_ISLNK (st.st_mode))) + : (__lstat64 (pglob->gl_pathv[i], &st64) == 0 + && (S_ISDIR (st64.st_mode) || S_ISLNK (st64.st_mode))))) { size_t len = strlen (pglob->gl_pathv[i]) + 2; char *new = realloc (pglob->gl_pathv[i], len); @@ -1500,54 +1500,6 @@ weak_alias (__glob_pattern_p, glob_pattern_p) # endif #endif - -/* We put this in a separate function mainly to allow the memory - allocated with alloca to be recycled. */ -static int -__attribute_noinline__ -link_exists2_p (const char *dir, size_t dirlen, const char *fname, - glob_t *pglob -# if !defined _LIBC && !HAVE_FSTATAT - , int flags -# endif - ) -{ - size_t fnamelen = strlen (fname); - char *fullname = __alloca (dirlen + 1 + fnamelen + 1); - struct stat st; - - mempcpy (mempcpy (mempcpy (fullname, dir, dirlen), "/", 1), - fname, fnamelen + 1); - -# if !defined _LIBC && !HAVE_FSTATAT - if (__builtin_expect ((flags & GLOB_ALTDIRFUNC) == 0, 1)) - { - struct_stat64 st64; - return __stat64 (fullname, &st64) == 0; - } -# endif - return (*pglob->gl_stat) (fullname, &st) == 0; -} - -/* Return true if DIR/FNAME exists. */ -static int -link_exists_p (int dfd, const char *dir, size_t dirlen, const char *fname, - glob_t *pglob, int flags) -{ -# if defined _LIBC || HAVE_FSTATAT - if (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)) - return link_exists2_p (dir, dirlen, fname, pglob); - else - { - /* dfd cannot be -1 here, because dirfd never returns -1 on - glibc, or on hosts that have fstatat. */ - struct_stat64 st64; - return __fxstatat64 (_STAT_VER, dfd, fname, &st64, 0) == 0; - } -# else - return link_exists2_p (dir, dirlen, fname, pglob, flags); -# endif -} #endif /* !defined GLOB_ONLY_P */ @@ -1619,8 +1571,8 @@ glob_in_dir (const char *pattern, const char *directory, int flags, "/", 1), pattern, patlen + 1); if ((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) - ? (*pglob->gl_stat) (fullname, &ust.st) - : __stat64 (fullname, &ust.st64)) == 0) + ? (*pglob->gl_lstat) (fullname, &ust.st) + : __lstat64 (fullname, &ust.st64)) == 0) /* We found this file to be existing. Now tell the rest of the function to copy this name into the result. */ flags |= GLOB_NOCHECK; @@ -1642,8 +1594,6 @@ glob_in_dir (const char *pattern, const char *directory, int flags, } else { - int dfd = (__builtin_expect (flags & GLOB_ALTDIRFUNC, 0) - ? -1 : dirfd ((DIR *) stream)); int fnm_flags = ((!(flags & GLOB_PERIOD) ? FNM_PERIOD : 0) | ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) #if defined _AMIGA || defined VMS @@ -1679,38 +1629,30 @@ glob_in_dir (const char *pattern, const char *directory, int flags, if (fnmatch (pattern, d.name, fnm_flags) == 0) { - /* If the file we found is a symlink we have to - make sure the target file exists. */ - if (!readdir_result_might_be_symlink (d) - || link_exists_p (dfd, directory, dirlen, d.name, - pglob, flags)) + if (cur == names->count) { - if (cur == names->count) - { - struct globnames *newnames; - size_t count = names->count * 2; - size_t size = (sizeof (struct globnames) - + ((count - INITIAL_COUNT) - * sizeof (char *))); - if (glob_use_alloca (alloca_used, size)) - newnames = names_alloca - = alloca_account (size, alloca_used); - else if ((newnames = malloc (size)) - == NULL) - goto memory_error; - newnames->count = count; - newnames->next = names; - names = newnames; - cur = 0; - } - names->name[cur] = strdup (d.name); - if (names->name[cur] == NULL) - goto memory_error; - ++cur; - ++nfound; - if (SIZE_MAX - pglob->gl_offs <= nfound) + struct globnames *newnames; + size_t count = names->count * 2; + size_t size = (sizeof (struct globnames) + + ((count - INITIAL_COUNT) + * sizeof (char *))); + if (glob_use_alloca (alloca_used, size)) + newnames = names_alloca + = alloca_account (size, alloca_used); + else if ((newnames = malloc (size)) == NULL) goto memory_error; - } + newnames->count = count; + newnames->next = names; + names = newnames; + cur = 0; + } + names->name[cur] = strdup (d.name); + if (names->name[cur] == NULL) + goto memory_error; + ++cur; + ++nfound; + if (SIZE_MAX - pglob->gl_offs <= nfound) + goto memory_error; } } } diff --git a/posix/tst-glob_symlinks.c b/posix/tst-glob_symlinks.c new file mode 100644 index 0000000..c0f1802 --- /dev/null +++ b/posix/tst-glob_symlinks.c @@ -0,0 +1,133 @@ +/* Test glob symlinks return (BZ #866). + for the filesystem access functions. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static void do_prepare (int argc, char *argv[]); +#define PREPARE do_prepare +static int do_test (void); +#include + +static void +create_link (const char *base, const char *fname, char *linkname, + size_t linknamesize) +{ + int ntries = 0; + while (1) + { + snprintf (linkname, linknamesize, "%s/%s%02d", test_dir, base, + ntries); + if (symlink (fname, linkname) == 0) + break; + if (errno != EEXIST) + FAIL_EXIT1 ("symlink failed: %m"); + if (ntries++ == 10) + FAIL_EXIT1 ("symlink failed with EEXIST too many times"); + } + add_temp_file (linkname); +} + +static char valid_link[PATH_MAX]; +static char dangling_link[PATH_MAX]; +static char dangling_dir[PATH_MAX]; + +static void +do_prepare (int argc, char *argv[]) +{ + char *fname; + + create_temp_file ("tst-glob_symlinks.", &fname); + + /* Create a existing symlink. */ + create_link ("valid-symlink-tst-glob_symlinks", fname, valid_link, + sizeof valid_link); + + /* Create a dangling symlink to a file. */ + int fd = create_temp_file ("dangling-tst-glob_file", &fname); + TEST_VERIFY_EXIT (close (fd) == 0); + /* It throws an warning at process end due 'add_temp_file' trying to + unlink it again. */ + TEST_VERIFY_EXIT (unlink (fname) == 0); + create_link ("dangling-symlink-file-tst-glob", fname, dangling_link, + sizeof dangling_link); + + /* Create a dangling symlink to a directory. */ + char tmpdir[PATH_MAX]; + snprintf (tmpdir, sizeof tmpdir, "%s/dangling-tst-glob_folder.XXXXXX", + test_dir); + TEST_VERIFY_EXIT (mkdtemp (tmpdir) != NULL); + create_link ("dangling-symlink-dir-tst-glob", tmpdir, dangling_dir, + sizeof dangling_dir); + TEST_VERIFY_EXIT (rmdir (tmpdir) == 0); +} + +static int +do_test (void) +{ + char buf[PATH_MAX]; + glob_t gl; + + TEST_VERIFY_EXIT (glob (valid_link, 0, NULL, &gl) == 0); + TEST_VERIFY_EXIT (gl.gl_pathc == 1); + TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], valid_link) == 0); + globfree (&gl); + + TEST_VERIFY_EXIT (glob (dangling_link, 0, NULL, &gl) == 0); + TEST_VERIFY_EXIT (gl.gl_pathc == 1); + TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_link) == 0); + globfree (&gl); + + TEST_VERIFY_EXIT (glob (dangling_dir, 0, NULL, &gl) == 0); + TEST_VERIFY_EXIT (gl.gl_pathc == 1); + TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_dir) == 0); + globfree (&gl); + + snprintf (buf, sizeof buf, "%s", dangling_link); + buf[strlen(buf) - 1] = '?'; + TEST_VERIFY_EXIT (glob (buf, 0, NULL, &gl) == 0); + TEST_VERIFY_EXIT (gl.gl_pathc == 1); + TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_link) == 0); + globfree (&gl); + + /* glob should handle dangling symbol as normal file, so ? should + return an empty string. */ + snprintf (buf, sizeof buf, "%s?", dangling_link); + TEST_VERIFY_EXIT (glob (buf, 0, NULL, &gl) != 0); + globfree (&gl); + + snprintf (buf, sizeof buf, "%s*", dangling_link); + TEST_VERIFY_EXIT (glob (buf, 0, NULL, &gl) == 0); + TEST_VERIFY_EXIT (gl.gl_pathc == 1); + TEST_VERIFY_EXIT (strcmp (gl.gl_pathv[0], dangling_link) == 0); + globfree (&gl); + + return 0; +} diff --git a/sysdeps/gnu/glob64.c b/sysdeps/gnu/glob64.c index d1e4e6f..250ff07 100644 --- a/sysdeps/gnu/glob64.c +++ b/sysdeps/gnu/glob64.c @@ -12,8 +12,8 @@ #undef stat #define stat stat64 -#undef __stat -#define __stat(file, buf) __xstat64 (_STAT_VER, file, buf) +#undef __lstat +#define __lstat(file, buf) __lxstat64 (_STAT_VER, file, buf) #define NO_GLOB_PATTERN_P 1 diff --git a/sysdeps/unix/sysv/linux/i386/glob64.c b/sysdeps/unix/sysv/linux/i386/glob64.c index 9c7abd8..2dcbe33 100644 --- a/sysdeps/unix/sysv/linux/i386/glob64.c +++ b/sysdeps/unix/sysv/linux/i386/glob64.c @@ -30,8 +30,8 @@ #undef stat #define stat stat64 -#undef __stat -#define __stat(file, buf) __xstat64 (_STAT_VER, file, buf) +#undef __lstat +#define __lstat(file, buf) __lxstat64 (_STAT_VER, file, buf) #define NO_GLOB_PATTERN_P 1 From patchwork Thu Jun 8 21:13:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103406 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632244qgd; Thu, 8 Jun 2017 14:14:12 -0700 (PDT) X-Received: by 10.84.217.213 with SMTP id d21mr37111856plj.45.1496956451948; Thu, 08 Jun 2017 14:14:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956451; cv=none; d=google.com; s=arc-20160816; b=qiH5qhpuMF4xg47iP+ze7VniKYZGdPTnX9fNnpDLHaaLSnQ8AUxzF1gEd0jYJpUqaj ywuR8pTfv95cLOu8RBo1SqogniCh48OA8gOQRsrxVx5ND8y78TCk+waNqFCP2JBkgdSK e2TXnwtFas9vBFVDsNjy5FvnPHOsLkVg2wJDe8g711qvRwyu4AIbOtfaqk9VB+GdPtPR m9v19EYKTKPm2VYa76Z+uCllyAF6pUsMMIz7Q526gXae5qndSLeasNw7WhaD//fu5OtU q1rAeODmW3a7uDtetiXCMxLQ5TQVzNzMs2OUiQmWFZTMEBWaK/LcdZdNXUTE6dpi+pXx 1IfQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=B6otIg/Oo+uuQPwN4K6oSKQ8XiMRzRafFtWONWlEKTk=; b=vyaVIHqa26+RJ/qiVPVgxxSZ0KI5vHTfTwb1USJYLRZxqiip35RYpr/5+QNMQCpvvu lvhVX8nwl6B/bhg789EIhAThM4lEw9c/hae19A1NsNmW2VfdcxbmgvS3BAYXljhdzV+4 pHKRaDjaWzI+LU4qUteNGJGBI5eAQtTIzqHEAocn1AvhjbCU+M7yJ3YUZy9LvgVeyG/i vm8enbkjwUyJ87aIM2I85KIoQ/NtN9kX/MaIkwFS1K4HHX/fq80XTlyeyCkSyJRYhaJ3 op22M/YCgGHG2iSR4Z2VzaqiCKkd/05gZfS8gd71EDSeus4SKgx28C2B7PFJCyKbB96t fonQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80200-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80200-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id q2si5270533plk.300.2017.06.08.14.14.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:14:11 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80200-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80200-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80200-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=AFCcB2BX2IG0Plndc75j2In4KlojVH9 W4uBXCnx7rPY9VJm3S0sPw8RZ4uwN+xOw/T7hkuquxTf8Bsk1oAdhrl50KwpQSxW UzVhE1wSS3BBKk7s2bZ3ljYvSb2shq4ibfFJ/Sn63ivvI4QvnJhoi3cVJI3kq10g z/IMS9VejqIA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=u0GcMTF/TSkiAIxMODk3+RySQbA=; b=oxRMp y33M0GzWeb5UfrPOvA47i3Mf3M4RwJILYcQzKT0q+1eSW3trLVUfX5O6hY5ksqlw Cn/WGJC1Y5jZs6VLAxzx6mh+GmqN7VZ0EkPqiCzcLtTkLVNFsLGsDPvoGQyBPQ/6 LbFYoSWKlQmGKY18i9uBathhKy1zE876oUFMQU= Received: (qmail 37668 invoked by alias); 8 Jun 2017 21:13:49 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 37595 invoked by uid 89); 8 Jun 2017 21:13:48 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f178.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=B6otIg/Oo+uuQPwN4K6oSKQ8XiMRzRafFtWONWlEKTk=; b=TD5+KGk2au/EVfV+ubWQ/+RHA/SVQiiAGx9Xjd/psjf3HtSV95itmceMuU1R/8YKGh Y3PcD6Suw81otT3ptwwdPojx0N9BHuig2BobfWRC0OcYy0xNKDoPt0pLzaT8jNQdRRqY gxlTt8yhr0zCTReiZR3D2hCm3eXCx6l5L4F93c0BgxHRnQoQFwv20FcnlXsagpVIP7sK MBcidtNtQIUy2F9E/1xp6IeZdCOZMDtbVYEKvI2t3H3s/imp5n+u7duWZqV/EP5bLGvd MyrHD2IxWQtiayB4T/w64nBu2hOSYvcekbdSLxPKLCSqAKdsD4Zs9or4APVinWxs5AsN woMQ== X-Gm-Message-State: AKS2vOwB7QRuBLPR5waj6//q8qPxj2arTj9ehVpNiMYLBeD0BBpJDZK5 XXNQbHJLvyTqjX7tdbCZPw== X-Received: by 10.55.92.68 with SMTP id q65mr30390389qkb.205.1496956429419; Thu, 08 Jun 2017 14:13:49 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 03/17] support: Add optstring support Date: Thu, 8 Jun 2017 18:13:17 -0300 Message-Id: <1496956411-25594-4-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch adds an option to test to add small command line option through CMDLINE_OPTSTRING define. For instance: #define CMDLINE_OPTSTRING "vd" static void cmdline_process_function (int c) { switch (c): 'v': /* process '-v' option. */ break; 'd': /* process '-d' option. */ break; } #define CMDLINE_PROCESS cmdline_process_function It will add both '-v' and '-d' along with already default long options. * support/support_test_main.c (support_test_main): Use optstring member for option string in getopt_long. * support/test-driver.c: Add comment about CMDLINE_OPTSTRING. (CMDLINE_OPTSTRING): New define. * support/test-driver.h (test_config): Add optstring member. --- support/support_test_main.c | 3 ++- support/test-driver.c | 9 +++++++++ support/test-driver.h | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) -- 2.7.4 diff --git a/support/support_test_main.c b/support/support_test_main.c index 914d64f..3c411a4 100644 --- a/support/support_test_main.c +++ b/support/support_test_main.c @@ -211,7 +211,8 @@ support_test_main (int argc, char **argv, const struct test_config *config) mallopt (M_PERTURB, 42); } - while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1) + while ((opt = getopt_long (argc, argv, config->optstring, options, NULL)) + != -1) switch (opt) { case '?': diff --git a/support/test-driver.c b/support/test-driver.c index 482066d..f5a2388 100644 --- a/support/test-driver.c +++ b/support/test-driver.c @@ -93,6 +93,10 @@ has this type: void CMDLINE_PROCESS (int); + + If the program also to process custom default short command line + argument (similar to getopt) it must define CMDLINE_OPTSTRING + with the expected options (for instance "vb"). */ #include @@ -151,6 +155,11 @@ main (int argc, char **argv) #ifdef CMDLINE_PROCESS test_config.cmdline_function = CMDLINE_PROCESS; #endif +#ifdef CMDLINE_OPTSTRING + test_config.optstring = CMDLINE_OPTSTRING; +#else + test_config.optstring = "+"; +#endif return support_test_main (argc, argv, &test_config); } diff --git a/support/test-driver.h b/support/test-driver.h index af1971a..a8fe9c3 100644 --- a/support/test-driver.h +++ b/support/test-driver.h @@ -35,6 +35,7 @@ struct test_config int expected_status; /* Expected exit status. */ int expected_signal; /* If non-zero, expect termination by signal. */ char no_mallopt; /* Boolean flag to disable mallopt. */ + const char *optstring; /* Short command line options. */ }; enum From patchwork Thu Jun 8 21:13:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103410 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632511qgd; Thu, 8 Jun 2017 14:15:02 -0700 (PDT) X-Received: by 10.84.136.129 with SMTP id 1mr5023444pll.213.1496956502182; Thu, 08 Jun 2017 14:15:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956502; cv=none; d=google.com; s=arc-20160816; b=D26alCn2pjiMc34h3+4fjJppe0flkkF1NGeg2UQXWoVDXrfFQLc54B7/qCNPrCHxCi YkFCyuoA3hzDa+EMHXTtCGILULV5nG8WExVWfRKJcGNU+eJ12CCOaqdRjhAAvltWUXuE MLOr6fAdIJOENqu4we+C5Pyzyn2QqSie3U1AIgcp3QvAqOwsj0J8Yic3fZac1GhjwPo+ 1Om5K+d1+tO4tQk+PiVTh10piqPLV5yhSK6KdUBX5sH4MhkIs5Yg4nv2DsivICboaOtk zjWAxXDfK2K8HiBOYTsrm9oUWSLozE3D8cSxlxrKPhBOrATiRWwX5bvpA1gYR94vtVli UvZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=Ysg0IQQywzCBk4SeZ+wl5AXQQDipqBHQGtjtxfxtVT8=; b=ok4NDcPkCnKZCmfb7pUHKF0zUboeoyBq59p+WINxZauRNexp+oB4Wg5P+7YfYHWMD7 iDoT9c8xQGS7iCJvmWXDdaCP6C5vsIK6UZhS8jZKD7JqHVLA+1LjiKCmChwcOzXLEmxO wlDERMatxXYUmZWxOfCQVZvE2i8bCnmPmSrSN2nxJKLrRfebkOzLw0OrQflZcFYttuul kMbXzodWKuXX+CkABfr2vlWo1u80agwy7G81vLeCL8t8ZfXj9Ofih0yBOeefaClJachr L2dFm+LrwgqIevBJzoFdN9wcXRdV3LHGAplOKlmOn0O6pE5Ot+D1l8FHPnBCNiAGUyKe vLBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80204-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80204-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id g10si5261419pfg.199.2017.06.08.14.15.01 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:15:02 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80204-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80204-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80204-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=Rp1oeYGgruvOxJ7VHMUVYonR/iJUjdr IvLNHO8Gajghiv32A2GbHpBrQ7Afw9FVF8YFRg/9FRpg46nFj+IJpxs9uNqvooxJ he5TK/UX1LNi4yWFR0I1ZBMzePTuyUL3IHjMXioaR14Pl0QKOzXobOCvl4Csui7g SUIePB+DZQ0E= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=ySGugbnxiQ1aiOjYCB/xKDFJ1uk=; b=Cmfbr eeQgDMjZE7mSvica2F9qPp10G923XNKvk2Yaz5tiquC7155jpqadNamjo0lkWkTx 5z5CxGqb97G+bWukdwArNoKWgQk81ofPvbN3bARY+aVvsKuvyjHt91uCXqRe5Pwd ICz+fN97mi0gM03EwpOpKa7l2gQL02jRSZJKn0= Received: (qmail 38721 invoked by alias); 8 Jun 2017 21:14:00 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 38551 invoked by uid 89); 8 Jun 2017 21:13:59 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=0755, sk:xxxxxxx X-HELO: mail-qt0-f181.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=Ysg0IQQywzCBk4SeZ+wl5AXQQDipqBHQGtjtxfxtVT8=; b=aHe4+voKTDTumYr1YXKkdT8pXivnHTKpzDz0I5OAvA3BMlZwsnUBPO4JnaZeG6uo9h /0fy+kh3kG0FLgvI9x0a9kPS1btuCRz7ye3z8+S8DwE4m/edF6fa3M7sQ07HshqH8Fhg D2l/v+L0dttm4Cil0+NWKEvx9Z49wsJbXJydYGdAO8H+FsyQzbMGNTVIfcMsVsCLiA+1 ek0qnGFdsCxLhCRBNf5wuDrVa/rzg7pDBiwmN8OFnkDnLW+g7XqwjDj70OmkN+s6k1cM xKQTSYVaV4r3jsv4XtGowP6JKSjM7NfkI5axoTuNPUVwG4/SOMuAkyhCCbMlOJ21GxfL RSOw== X-Gm-Message-State: AODbwcDk46W1ZyNMUoYvAfdaJQWIWXhUKl3ZLEALyErorPQvK0k1igsl gW0vSP5fX8uH3AeEmHfNTA== X-Received: by 10.237.50.71 with SMTP id y65mr30430022qtd.117.1496956432088; Thu, 08 Jun 2017 14:13:52 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 04/17] posix: Adjust glob tests to libsupport Date: Thu, 8 Jun 2017 18:13:18 -0300 Message-Id: <1496956411-25594-5-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch adjust glob tests to use libsupport. It also refactor some tests to move to a more meaningful file name and to gather similar tests in a common file: * move bug-glob3.c tests to tst-glob_basic.c. * move bug-glob2.c tests to tst-glob_memory.c * move common definitions to tst-glob_common.c. Checked on x86_64-linux-gnu. * posix/Makefile (tests): Remove bug-glob2 and bug-glob3. Add tst-glob_basic and tst-glob_memory. * posix/bug-glob3.c: Move to ... * posix/tst-glob_basic.c: ... here. * posix/bug-glob2.c: Move to ... * posix/tst-glob_memory.c: ... here. * posix/globtest.c: Use libsupport. * posix/tst-gnuglob.c: Likewise. * posix/tst-glob_common.c: New file. --- posix/Makefile | 12 +-- posix/bug-glob3.c | 45 --------- posix/globtest.c | 163 ++++++++++++++++++------------- posix/tst-glob_basic.c | 41 ++++++++ posix/tst-glob_common.c | 103 +++++++++++++++++++ posix/{bug-glob2.c => tst-glob_memory.c} | 100 +++---------------- posix/tst-gnuglob.c | 109 +++------------------ 7 files changed, 270 insertions(+), 303 deletions(-) delete mode 100644 posix/bug-glob3.c create mode 100644 posix/tst-glob_basic.c create mode 100644 posix/tst-glob_common.c rename posix/{bug-glob2.c => tst-glob_memory.c} (76%) -- 2.7.4 diff --git a/posix/Makefile b/posix/Makefile index 2c0c9f9..1001e54 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -79,7 +79,7 @@ tests := test-errno tstgetopt testfnm runtests runptests \ tst-nice tst-nanosleep tst-regex2 \ transbug tst-rxspencer tst-pcre tst-boost \ bug-ga1 tst-vfork1 tst-vfork2 tst-vfork3 tst-waitid \ - tst-getaddrinfo2 bug-glob2 bug-glob3 tst-sysconf \ + tst-getaddrinfo2 tst-glob_memory tst-glob_basic tst-sysconf \ tst-execvp1 tst-execvp2 tst-execlp1 tst-execlp2 \ tst-execv1 tst-execv2 tst-execl1 tst-execl2 \ tst-execve1 tst-execve2 tst-execle1 tst-execle2 \ @@ -121,7 +121,7 @@ generated += $(addprefix wordexp-test-result, 1 2 3 4 5 6 7 8 9 10) \ tst-getconf.out \ tst-pcre-mem.out tst-pcre.mtrace tst-boost-mem.out \ tst-boost.mtrace bug-ga2.mtrace bug-ga2-mem.out \ - bug-glob2.mtrace bug-glob2-mem.out tst-vfork3-mem.out \ + tst-glob_memory.mtrace tst-glob_memory-mem.out tst-vfork3-mem.out \ tst-vfork3.mtrace getconf.speclist tst-fnmatch-mem.out \ tst-fnmatch.mtrace bug-regex36.mtrace @@ -139,7 +139,7 @@ tests-special += $(objpfx)bug-regex2-mem.out $(objpfx)bug-regex14-mem.out \ $(objpfx)bug-regex21-mem.out $(objpfx)bug-regex31-mem.out \ $(objpfx)tst-rxspencer-no-utf8-mem.out $(objpfx)tst-pcre-mem.out \ $(objpfx)tst-boost-mem.out $(objpfx)tst-getconf.out \ - $(objpfx)bug-glob2-mem.out $(objpfx)tst-vfork3-mem.out \ + $(objpfx)tst-glob_memory-mem.out $(objpfx)tst-vfork3-mem.out \ $(objpfx)tst-fnmatch-mem.out $(objpfx)bug-regex36-mem.out xtests-special += $(objpfx)bug-ga2-mem.out endif @@ -342,10 +342,10 @@ $(objpfx)bug-ga2-mem.out: $(objpfx)bug-ga2.out bug-ga2-ENV = MALLOC_TRACE=$(objpfx)bug-ga2.mtrace -bug-glob2-ENV = MALLOC_TRACE=$(objpfx)bug-glob2.mtrace +tst-glob_memory-ENV = MALLOC_TRACE=$(objpfx)tst-glob_memory.mtrace -$(objpfx)bug-glob2-mem.out: $(objpfx)bug-glob2.out - $(common-objpfx)malloc/mtrace $(objpfx)bug-glob2.mtrace > $@; \ +$(objpfx)tst-glob_memory-mem.out: $(objpfx)tst-glob_memory.out + $(common-objpfx)malloc/mtrace $(objpfx)tst-glob_memory.mtrace > $@; \ $(evaluate-test) $(inst_libexecdir)/getconf: $(inst_bindir)/getconf \ diff --git a/posix/bug-glob3.c b/posix/bug-glob3.c deleted file mode 100644 index f2fbd70..0000000 --- a/posix/bug-glob3.c +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include -#include - -static int -do_test (void) -{ - int result = 0; - glob_t g; - g.gl_pathc = 0; - - int r = glob ("", 0, NULL, &g); - if (r != GLOB_NOMATCH) - { - puts ("glob (\"\", 0, NULL, &g) did not fail"); - result = 1; - } - else if (g.gl_pathc != 0) - { - puts ("gl_pathc after glob (\"\", 0, NULL, &g) not zero"); - result = 1; - } - - r = glob ("", GLOB_NOCHECK, NULL, &g); - if (r != 0) - { - puts ("glob (\"\", GLOB_NOCHECK, NULL, &g) did fail"); - result = 1; - } - else if (g.gl_pathc != 1) - { - puts ("gl_pathc after glob (\"\", GLOB_NOCHECK, NULL, &g) not 1"); - result = 1; - } - else if (strcmp (g.gl_pathv[0], "") != 0) - { - puts ("gl_pathv[0] after glob (\"\", GLOB_NOCHECK, NULL, &g) not \"\""); - result = 1; - } - - return result; -} - -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" diff --git a/posix/globtest.c b/posix/globtest.c index 7ffcb91..e5b5891 100644 --- a/posix/globtest.c +++ b/posix/globtest.c @@ -1,4 +1,5 @@ -/* Copyright (C) 1997-2017 Free Software Foundation, Inc. +/* Basic glob tests. It uses an extenal driver script (tst-glob.sh). + Copyright (C) 1997-2017 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -21,77 +22,97 @@ #include #include -int -main (int argc, char *argv[]) +#include + +#define OPT_BRACE 'b' +#define OPT_NOCHECK 'c' +#define OPT_ONLYDIR 'd' +#define OPT_NOESCAPE 'e' +#define OPT_ERR 'E' +#define OPT_NOMAGIC 'g' +#define OPT_MARK 'm' +#define OPT_DOOFFS 'o' +#define OPT_PERIOD 'p' +#define OPT_QUOTES 'q' +#define OPT_NOSORT 's' +#define OPT_TILDE 't' +#define OPT_TILDE_CHECK 'T' + +#define CMDLINE_OPTSTRING "bcdeEgmopqstT" + +static int glob_flags = 0; +static int quotes = 1; + +static void +cmdline_process_function (int c) { - int i, j; - int glob_flags = 0; - glob_t g; - int quotes = 1; + switch (c) + { + case OPT_BRACE: + glob_flags |= GLOB_BRACE; + break; + case OPT_NOCHECK: + glob_flags |= GLOB_NOCHECK; + break; + case OPT_ONLYDIR: + glob_flags |= GLOB_ONLYDIR; + break; + case OPT_NOESCAPE: + glob_flags |= GLOB_NOESCAPE; + break; + case OPT_ERR: + glob_flags |= GLOB_ERR; + break; + case OPT_NOMAGIC: + glob_flags |= GLOB_NOMAGIC; + break; + case OPT_MARK: + glob_flags |= GLOB_MARK; + break; + case OPT_DOOFFS: + glob_flags |= GLOB_DOOFFS; + break; + case OPT_PERIOD: + glob_flags |= GLOB_PERIOD; + break; + case OPT_QUOTES: + quotes = 0; + break; + case OPT_NOSORT: + glob_flags |= GLOB_NOSORT; + break; + case OPT_TILDE: + glob_flags |= GLOB_TILDE; + break; + case OPT_TILDE_CHECK: + glob_flags |= GLOB_TILDE_CHECK; + break; + } +} - g.gl_offs = 0; +#define CMDLINE_PROCESS cmdline_process_function - while ((i = getopt (argc, argv, "bcdeEgmopqstT")) != -1) - switch(i) - { - case 'b': - glob_flags |= GLOB_BRACE; - break; - case 'c': - glob_flags |= GLOB_NOCHECK; - break; - case 'd': - glob_flags |= GLOB_ONLYDIR; - break; - case 'e': - glob_flags |= GLOB_NOESCAPE; - break; - case 'E': - glob_flags |= GLOB_ERR; - break; - case 'g': - glob_flags |= GLOB_NOMAGIC; - break; - case 'm': - glob_flags |= GLOB_MARK; - break; - case 'o': - glob_flags |= GLOB_DOOFFS; - g.gl_offs = 1; - break; - case 'p': - glob_flags |= GLOB_PERIOD; - break; - case 'q': - quotes = 0; - break; - case 's': - glob_flags |= GLOB_NOSORT; - break; - case 't': - glob_flags |= GLOB_TILDE; - break; - case 'T': - glob_flags |= GLOB_TILDE_CHECK; - break; - default: - exit (-1); - } +static int +do_test_argv (int argc, char *argv[]) +{ + int i, j; + glob_t g; - if (optind >= argc || chdir (argv[optind])) - exit(1); + g.gl_offs = glob_flags & GLOB_DOOFFS ? 1 : 0; - j = optind + 1; - if (optind + 1 >= argc) - exit (1); + if (argc < 2) + FAIL_EXIT1 ("invalid arguments (expecting path for glob)"); + if (chdir (argv[1]) != 0) + FAIL_EXIT1 ("chmod (%s): %m", argv[1]); - /* Do a glob on each remaining argument. */ - for (j = optind + 1; j < argc; j++) { - i = glob (argv[j], glob_flags, NULL, &g); - if (i != 0) - break; - glob_flags |= GLOB_APPEND; - } + /* Do a glob on each argument. */ + for (j = 2; j < argc; j++) + { + i = glob (argv[j], glob_flags, NULL, &g); + if (i != 0) + break; + glob_flags |= GLOB_APPEND; + } /* Was there an error? */ if (i == GLOB_NOSPACE) @@ -106,16 +127,18 @@ main (int argc, char *argv[]) if ((glob_flags & GLOB_DOOFFS) && g.gl_pathv[0] == NULL) g.gl_pathv[0] = (char *) "abc"; - /* Print out the names. Unless otherwise specified, qoute them. */ + /* Print out the names. Unless otherwise specified, quote them. */ if (g.gl_pathv) { for (i = 0; i < g.gl_offs + g.gl_pathc; ++i) - printf ("%s%s%s\n", quotes ? "`" : "", - g.gl_pathv[i] ? g.gl_pathv[i] : "(null)", - quotes ? "'" : ""); + printf ("%s%s%s\n", quotes ? "`" : "", + g.gl_pathv[i] ? g.gl_pathv[i] : "(null)", quotes ? "'" : ""); } globfree (&g); return 0; } + +#define TEST_FUNCTION_ARGV do_test_argv +#include diff --git a/posix/tst-glob_basic.c b/posix/tst-glob_basic.c new file mode 100644 index 0000000..64b95a8 --- /dev/null +++ b/posix/tst-glob_basic.c @@ -0,0 +1,41 @@ +/* Basic glob tests. + Copyright (C) 2001-2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +#include + +static int +do_test (void) +{ + glob_t g; + g.gl_pathc = 0; + + TEST_VERIFY_EXIT (glob ("", 0, NULL, &g) == GLOB_NOMATCH); + TEST_VERIFY_EXIT (g.gl_pathc == 0); + + TEST_VERIFY_EXIT (glob ("", GLOB_NOCHECK, NULL, &g) == 0); + TEST_VERIFY_EXIT (g.gl_pathc == 1); + TEST_VERIFY_EXIT (strcmp (g.gl_pathv[0], "") == 0); + + return 0; +} + +#include diff --git a/posix/tst-glob_common.c b/posix/tst-glob_common.c new file mode 100644 index 0000000..b7a66c7 --- /dev/null +++ b/posix/tst-glob_common.c @@ -0,0 +1,103 @@ +/* Common glob test definition. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +// #define DEBUG +#ifdef DEBUG +# define PRINTF(fmt, args...) \ + do \ + { \ + int save_errno = errno; \ + printf (fmt, ##args); \ + errno = save_errno; \ + } while (0) +#else +# define PRINTF(fmt, args...) +#endif + +struct filesystem_t +{ + const char *name; + int level; + int type; + mode_t mode; +}; + +static long int +find_file (const char *s, const struct filesystem_t *filesystem, + size_t nfiles) +{ + int level = 1; + long int idx = 0; + + while (s[0] == '/') + { + if (s[1] == '\0') + { + s = "."; + break; + } + ++s; + } + + if (strcmp (s, ".") == 0) + return 0; + + if (s[0] == '.' && s[1] == '/') + s += 2; + + while (*s != '\0') + { + char *endp = strchrnul (s, '/'); + + PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level); + + while (idx < nfiles && filesystem[idx].level >= level) + { + if (filesystem[idx].level == level + && memcmp (s, filesystem[idx].name, endp - s) == 0 + && filesystem[idx].name[endp - s] == '\0') + break; + ++idx; + } + + if (idx == nfiles || filesystem[idx].level < level) + { + errno = ENOENT; + return -1; + } + + if (*endp == '\0') + return idx + 1; + + if (filesystem[idx].type != DT_DIR + && (idx + 1 >= nfiles + || filesystem[idx].level >= filesystem[idx + 1].level)) + { + errno = ENOTDIR; + return -1; + } + + ++idx; + + s = endp + 1; + ++level; + } + + errno = ENOENT; + return -1; +} diff --git a/posix/bug-glob2.c b/posix/tst-glob_memory.c similarity index 76% rename from posix/bug-glob2.c rename to posix/tst-glob_memory.c index 592d957..33961e8 100644 --- a/posix/bug-glob2.c +++ b/posix/tst-glob_memory.c @@ -27,18 +27,10 @@ #include #include -// #define DEBUG -#ifdef DEBUG -# define PRINTF(fmt, args...) \ - do \ - { \ - int save_errno = errno; \ - printf (fmt, ##args); \ - errno = save_errno; \ - } while (0) -#else -# define PRINTF(fmt, args...) -#endif +#include + +#include "tst-glob_common.c" + #define LONG_NAME \ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \ @@ -52,13 +44,7 @@ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -static struct -{ - const char *name; - int level; - int type; - mode_t mode; -} filesystem[] = +struct filesystem_t filesystem[] = { { ".", 1, DT_DIR, 0755 }, { "..", 1, DT_DIR, 0755 }, @@ -79,7 +65,7 @@ static struct { "..", 3, DT_DIR, 0755 }, { "a", 3, DT_REG, 0644 } }; -#define nfiles (sizeof (filesystem) / sizeof (filesystem[0])) +static const size_t nfiles = sizeof (filesystem) / sizeof (filesystem[0]); typedef struct @@ -91,65 +77,10 @@ typedef struct } my_DIR; -static long int -find_file (const char *s) -{ - int level = 1; - long int idx = 0; - - if (strcmp (s, ".") == 0) - return 0; - - if (s[0] == '.' && s[1] == '/') - s += 2; - - while (*s != '\0') - { - char *endp = strchrnul (s, '/'); - - PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level); - - while (idx < nfiles && filesystem[idx].level >= level) - { - if (filesystem[idx].level == level - && memcmp (s, filesystem[idx].name, endp - s) == 0 - && filesystem[idx].name[endp - s] == '\0') - break; - ++idx; - } - - if (idx == nfiles || filesystem[idx].level < level) - { - errno = ENOENT; - return -1; - } - - if (*endp == '\0') - return idx + 1; - - if (filesystem[idx].type != DT_DIR - && (idx + 1 >= nfiles - || filesystem[idx].level >= filesystem[idx + 1].level)) - { - errno = ENOTDIR; - return -1; - } - - ++idx; - - s = endp + 1; - ++level; - } - - errno = ENOENT; - return -1; -} - - static void * my_opendir (const char *s) { - long int idx = find_file (s); + long int idx = find_file (s, filesystem, nfiles); my_DIR *dir; if (idx == -1) @@ -241,7 +172,7 @@ my_closedir (void *dir) static int my_stat (const char *name, struct stat *st) { - long int idx = find_file (name); + long int idx = find_file (name, filesystem, nfiles); if (idx == -1) { @@ -275,7 +206,7 @@ init_glob_altdirfuncs (glob_t *pglob) } -int +static int do_test (void) { mtrace (); @@ -286,10 +217,7 @@ do_test (void) if (glob ("dir/*able/*", GLOB_ERR | GLOB_ALTDIRFUNC, NULL, &gl) != GLOB_ABORTED) - { - puts ("glob did not fail with GLOB_ABORTED"); - exit (EXIT_FAILURE); - } + FAIL_EXIT1 ("glob did not fail with GLOB_ABORTED"); globfree (&gl); @@ -298,10 +226,7 @@ do_test (void) gl.gl_offs = 3; if (glob ("dir2/*", GLOB_DOOFFS, NULL, &gl) != GLOB_NOMATCH) - { - puts ("glob did not fail with GLOB_NOMATCH"); - exit (EXIT_FAILURE); - } + FAIL_EXIT1 ("glot dit not fail with GLOB_NOMATCH"); globfree (&gl); @@ -310,5 +235,4 @@ do_test (void) return 0; } -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" +#include diff --git a/posix/tst-gnuglob.c b/posix/tst-gnuglob.c index d753674..f86eef7 100644 --- a/posix/tst-gnuglob.c +++ b/posix/tst-gnuglob.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -28,21 +27,11 @@ #include #include +#include -// #define DEBUG -#ifdef DEBUG -# define PRINTF(fmt, args...) printf (fmt, ##args) -#else -# define PRINTF(fmt, args...) -#endif - +#include "tst-glob_common.c" -static struct -{ - const char *name; - int level; - int type; -} filesystem[] = +struct filesystem_t filesystem[] = { { ".", 1, DT_DIR }, { "..", 1, DT_DIR }, @@ -84,7 +73,7 @@ static struct { "..", 4, DT_DIR }, { "hidden", 4, DT_REG } }; -#define nfiles (sizeof (filesystem) / sizeof (filesystem[0])) +static const size_t nfiles = sizeof (filesystem) / sizeof (filesystem[0]); typedef struct @@ -96,75 +85,10 @@ typedef struct } my_DIR; -static long int -find_file (const char *s) -{ - int level = 1; - long int idx = 0; - - while (s[0] == '/') - { - if (s[1] == '\0') - { - s = "."; - break; - } - ++s; - } - - if (strcmp (s, ".") == 0) - return 0; - - if (s[0] == '.' && s[1] == '/') - s += 2; - - while (*s != '\0') - { - char *endp = strchrnul (s, '/'); - - PRINTF ("looking for %.*s, level %d\n", (int) (endp - s), s, level); - - while (idx < nfiles && filesystem[idx].level >= level) - { - if (filesystem[idx].level == level - && memcmp (s, filesystem[idx].name, endp - s) == 0 - && filesystem[idx].name[endp - s] == '\0') - break; - ++idx; - } - - if (idx == nfiles || filesystem[idx].level < level) - { - errno = ENOENT; - return -1; - } - - if (*endp == '\0') - return idx + 1; - - if (filesystem[idx].type != DT_DIR - && (idx + 1 >= nfiles - || filesystem[idx].level >= filesystem[idx + 1].level)) - { - errno = ENOTDIR; - return -1; - } - - ++idx; - - s = endp + 1; - ++level; - } - - errno = ENOENT; - return -1; -} - - static void * my_opendir (const char *s) { - long int idx = find_file (s); + long int idx = find_file (s, filesystem, nfiles); my_DIR *dir; @@ -176,7 +100,7 @@ my_opendir (const char *s) dir = (my_DIR *) malloc (sizeof (my_DIR)); if (dir == NULL) - error (EXIT_FAILURE, errno, "cannot allocate directory handle"); + FAIL_EXIT1 ("cannot allocate directory handle"); dir->level = filesystem[idx].level; dir->idx = idx; @@ -247,13 +171,11 @@ my_closedir (void *dir) static int my_stat (const char *name, struct stat *st) { - long int idx = find_file (name); + long int idx = find_file (name, filesystem, nfiles); if (idx == -1) - { - PRINTF ("my_stat (\"%s\", ...) = -1 (%s)\n", name, strerror (errno)); - return -1; - } + FAIL_EXIT1 ("%s (\"%s\", ...) == -1 (%s)", __func__, name, + strerror (errno)); memset (st, '\0', sizeof (*st)); @@ -276,7 +198,8 @@ static const char *glob_errstring[] = [GLOB_ABORTED] = "read error", [GLOB_NOMATCH] = "no matches found" }; -#define nglob_errstring (sizeof (glob_errstring) / sizeof (glob_errstring[0])) +static const size_t nglob_errstring = (sizeof (glob_errstring) + / sizeof (glob_errstring[0])); static const char * @@ -289,7 +212,7 @@ flagstr (int flags) "GLOB_ALTDIRFUNC", "GLOB_BRACE", "GLOB_NOMAGIC", "GLOB_TILDE", "GLOB_ONLYDIR", "GLOB_TILDECHECK" }; -#define nstrs (sizeof (strs) / sizeof (strs[0])) + static const size_t nstrs = (sizeof (strs) / sizeof (strs[0])); static char buf[100]; char *cp = buf; int cnt; @@ -311,7 +234,6 @@ flagstr (int flags) } return buf; -#undef nstrs } @@ -378,7 +300,6 @@ test_result (const char *fmt, int flags, glob_t *gl, const char *str[]) return result; } - static int do_test (void) { @@ -416,7 +337,8 @@ do_test (void) result |= test_result (fmt, flags, &gl, (const char *[]) { c, NULL }); \ else \ printf ("result for glob (\"%s\", %s) = %s\n\n", fmt, flagstr (flags), \ - errstr (errval)) + errstr (errval)); \ + globfree (&gl) test ("*/*/*", 0, 0, "dir1lev1/dir2lev2/dir1lev3", @@ -498,5 +420,4 @@ do_test (void) return result; } -#define TEST_FUNCTION do_test () -#include "../test-skeleton.c" +#include From patchwork Thu Jun 8 21:13:19 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103411 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632566qgd; Thu, 8 Jun 2017 14:15:13 -0700 (PDT) X-Received: by 10.98.16.220 with SMTP id 89mr22109070pfq.210.1496956512952; Thu, 08 Jun 2017 14:15:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956512; cv=none; d=google.com; s=arc-20160816; b=GGqdMHVOFsxe2xrJKEBXVlew1+kdr3cXHJwaV/mq8ONe7hSJc15QiAb8/MslbWTlV7 QnDuVpons/41s1HfdwKbujVgrn0fxRhI6YyR0s/BPLySRLuR1EiVNbkLL4DKQ9Xi5Ads HjLytauldIbmy6kyW18wcaKSOMgB1vQlfNGPNOe5ekJtf28W7ZVSvtNXV+2SDzet9I30 Zux5m9ug1jkgJrVHTN9Dv7d2ig0xwg6qZjyJmbhwkCzrVPqnac6sA9RDU+CAMZql+zzT jObZVMxfC4zhkOQ5614UXLivdL0uOuJsqnhgi1/ScfbFkDE5WEFwMNzNZgEHGkthS1VC DHjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=t8wtAE4RtkCAhnN0dQBEByKMBknWZ7ebw2knGykLcYU=; b=j7JXQ7RKcl411qT47kMhbu5BKxTUm5z/2EE4Tw99mn8l43phqpDaI1cTqn7/P3Aqnn F/zCSrraEFS8KjkI/1HO26+2E7TvYTdreYyhRcqqTVfbqHnnBtpfjvnBQ66cMA7LAS33 SwkALpYOMg+5ZUyHIjpcM0aMySCsxUpwnuNqj6wj1lHX1LnPdXZkVKdu25/yNc5qTXw4 7CM+PUSVq8bIwx6PU040MmePW1AjHwy8QHPT8jmR4biXYa78Gf3PCEwAON0SZiWJ8pLC deVxMR+l4j3z6bYslWaZS+ZEJT4RwB7jg4LDQ5O2FNcAFnEpbTBLcgc0YW44RDk19K+R jWlg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80205-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80205-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 76si5201888pfp.320.2017.06.08.14.15.12 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:15:12 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80205-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80205-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80205-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=HPOW7f/uMKnX2KNUuBn+uzpoicCoq6J 1TLHb2XmYpMDMXINtsFKOOTtFjczcwixqL0DTUwTdokgQQE9TgoEH79FocUHcCkV Lm95H87Q7fxGqwIf6jNmbwEPkf47ZIWQI+7tf67YISPG2SmngfneVuSKHqWn79tS qGtGtDfPlebA= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=tGH4DkU9SGP28rfvXxMmRHjtjDQ=; b=CLQsW rfBubJf6Uq6karq6aq0IhvkcHNITh1YRLpb8vMkTeg+ZBxaZIfhWPdrWNSff0qzX POHJBFV19EJotQjT2/rwrLnS+sVMNQW5lNlEiVqxKU+ifXPy4aFGnrycJ8QdkQAS 37QOsupxCP31xxlpOfQn/hc9ek4U6Y+gvXn2fo= Received: (qmail 39099 invoked by alias); 8 Jun 2017 21:14:04 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 38973 invoked by uid 89); 8 Jun 2017 21:14:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f171.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=t8wtAE4RtkCAhnN0dQBEByKMBknWZ7ebw2knGykLcYU=; b=pgW/tJmDMWMRBFslIwzfsELUbc6fTefKdX0DksBdoaw0MbUzUZdrKA4TcrkghdnbgN 6mGCw+UWG4J8bE+QbYXsfwK99RTQ3yMm6uETOG1W2KHAfjb6SxgybYRIYHX5OMJq9W/P J4cl0x6BmH/j8JQ8vwPxMCbVzLj4bTky2DQCH10ssGMKYPJkGR5rvH8emceWRT1gvl83 RcTxyt7WD7VtcRfeddNlYngrb1aBX/CNVU0lWrVACpHMEK11pu3SsdCDZCR+k88DBDVs PCEyGdwwkWJ98GhnaMvIKTtRlkVNsTtJ+phtWKZrgVSBqormKk/igOO8a0pU4BIoUJV2 q5xg== X-Gm-Message-State: AKS2vOzs6pYpnWkAxfQbBJvisr53d9yznaK4DGnRN9hq9Yxvbp2XsFbl 9zNMSF/F5XH+rd9CAdDJFA== X-Received: by 10.200.34.230 with SMTP id g35mr3015538qta.65.1496956434866; Thu, 08 Jun 2017 14:13:54 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 05/17] posix: Consolidate glob implementation Date: Thu, 8 Jun 2017 18:13:19 -0300 Message-Id: <1496956411-25594-6-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch consolidates the glob implementation. The main changes are: * Remove specific defines required for multiple compilation in same unit (GLOB_ONLY_P, NO_GLOB_PATTERN_P and GLOB_COMPAT_BUILD). To allow using the same code to build compat version on Linux, extra units are used instead (oldglob.c). * Both globfree and GNU extension glob_pattern_p are now on their files. This simplifies the creation of compat symbol when required. * Also similar to glob/glob64, a new globfree64 is file is added with an empty implementatio as default. * On Linux all implementation now uses a default one with the exception of alpha (which requires a specific versioning) and s390-32 (which different than other 32 bits with support for v2.1 symbol does not add a compat symbol). * Move i386 olddirent.h header to Linux default directory, since it is the only header with this name and it is shared among different architectures (and used on compat glob symbol). Checked on x86_64-linux-gnu and i686-linux-gnu. * posix/Makefile (routines): Add globfree, globfree64, and glob_pattern_p. * posix/glob.c: Remove GLOB_ONLY_P, GLOB_COMPAT_BUILD, and NO_GLOB_PATTERN_P define usage. (globfree): Move to its own file. (__glob_pattern_type): Likewise. (__glob_pattern_p): Likewise. * posix/glob_internal.h: New file. * posix/glob_pattern_p.c: Likewise. * posix/globfree.c: Likewise. * posix/globfree64.c: Likewise. * sysdeps/gnu/glob64.c: Remove file. * sysdeps/unix/sysv/linux/Makefile (sysdep_routines): Add oldglob. * sysdeps/unix/sysv/linux/alpha/Makefile [$(subdir) = posix] (sysdep_routines): Remove rule. * sysdeps/unix/sysv/linux/alpha/glob.c: Remove file. * sysdeps/unix/sysv/linux/arm/glob64.c: Likewise. * sysdeps/wordsize-64/glob.c: Likewise. * sysdeps/unix/sysv/linux/m68k/glob64.c: Likewise. * sysdeps/unix/sysv/linux/mips/mips64/n64/glob64.c: Likewise. * sysdeps/unix/sysv/linux/powerpc/powerpc32/glob64.c: Likewise. * sysdeps/unix/sysv/linux/sparc/sparc32/glob64.c: Likewise. * sysdeps/unix/sysv/linux/wordsize-64/glob64.c: Likewise. * sysdeps/unix/sysv/linux/x86_64/x32/glob.c: Likewise. * sysdeps/wordsize-64/glob64.c: Likewise. * sysdeps/unix/sysv/linux/alpha/glob64.c: New file. * sysdeps/unix/sysv/linux/alpha/globfree.c: Likewise. * sysdeps/unix/sysv/linux/glob.c: Likewise. * sysdeps/unix/sysv/linux/glob64.c: Likewise. * sysdeps/unix/sysv/linux/globfree.c: Likewise. * sysdeps/unix/sysv/linux/globfree64.c: Likewise. * sysdeps/unix/sysv/linux/i386/alphasort64.c: include olddirent.h using relative path instead of absolute one. * sysdeps/unix/sysv/linux/i386/getdents64.c: Likewise. * sysdeps/unix/sysv/linux/i386/readdir64.c: Likewise. * sysdeps/unix/sysv/linux/i386/readdir64_r.c: Likewise. * sysdeps/unix/sysv/linux/i386/versionsort64.c: Likewise. * sysdeps/unix/sysv/linux/i386/olddirent.h: Move to ... * sysdeps/unix/sysv/linux/olddirent.h: ... here. * sysdeps/unix/sysv/linux/i386/glob64.c: Move to ... * sysdeps/unix/sysv/linux/oldglob.c: ... here. * sysdeps/unix/sysv/linux/i386/glob64.c: Remove file. * sysdeps/unix/sysv/linux/oldglob.c: New file. * sysdeps/unix/sysv/linux/s390/s390-32/glob64.c: New file. * sysdeps/unix/sysv/linux/s390/s390-32/oldglob.c: Likewise. --- posix/Makefile | 2 +- posix/glob.c | 82 +--------------------- posix/glob_internal.h | 57 +++++++++++++++ posix/glob_pattern_p.c | 29 ++++++++ posix/globfree.c | 37 ++++++++++ posix/globfree64.c | 27 +++++++ sysdeps/gnu/glob64.c | 25 ------- sysdeps/unix/sysv/linux/Makefile | 2 +- sysdeps/unix/sysv/linux/alpha/Makefile | 4 -- sysdeps/unix/sysv/linux/alpha/{glob.c => glob64.c} | 11 --- sysdeps/unix/sysv/linux/alpha/globfree.c | 36 ++++++++++ sysdeps/unix/sysv/linux/arm/glob64.c | 1 - sysdeps/unix/sysv/linux/glob.c | 23 ++++++ sysdeps/unix/sysv/linux/glob64.c | 55 +++++++++++++++ sysdeps/unix/sysv/linux/globfree.c | 23 ++++++ sysdeps/unix/sysv/linux/globfree64.c | 0 sysdeps/unix/sysv/linux/i386/alphasort64.c | 2 +- sysdeps/unix/sysv/linux/i386/getdents64.c | 2 +- sysdeps/unix/sysv/linux/i386/readdir64.c | 2 +- sysdeps/unix/sysv/linux/i386/readdir64_r.c | 2 +- sysdeps/unix/sysv/linux/i386/versionsort64.c | 2 +- sysdeps/unix/sysv/linux/m68k/glob64.c | 1 - sysdeps/unix/sysv/linux/mips/mips64/n64/glob64.c | 1 - sysdeps/unix/sysv/linux/{i386 => }/olddirent.h | 0 .../unix/sysv/linux/{i386/glob64.c => oldglob.c} | 53 ++++---------- sysdeps/unix/sysv/linux/powerpc/powerpc32/glob64.c | 1 - sysdeps/unix/sysv/linux/s390/s390-32/glob64.c | 20 ++++++ sysdeps/unix/sysv/linux/s390/s390-32/oldglob.c | 2 + sysdeps/unix/sysv/linux/sparc/sparc32/glob64.c | 1 - sysdeps/unix/sysv/linux/wordsize-64/glob64.c | 2 - sysdeps/unix/sysv/linux/x86_64/x32/glob.c | 1 - sysdeps/wordsize-64/glob.c | 8 --- sysdeps/wordsize-64/glob64.c | 1 - 33 files changed, 330 insertions(+), 185 deletions(-) create mode 100644 posix/glob_internal.h create mode 100644 posix/glob_pattern_p.c create mode 100644 posix/globfree.c create mode 100644 posix/globfree64.c delete mode 100644 sysdeps/gnu/glob64.c rename sysdeps/unix/sysv/linux/alpha/{glob.c => glob64.c} (78%) create mode 100644 sysdeps/unix/sysv/linux/alpha/globfree.c delete mode 100644 sysdeps/unix/sysv/linux/arm/glob64.c create mode 100644 sysdeps/unix/sysv/linux/glob.c create mode 100644 sysdeps/unix/sysv/linux/glob64.c create mode 100644 sysdeps/unix/sysv/linux/globfree.c create mode 100644 sysdeps/unix/sysv/linux/globfree64.c delete mode 100644 sysdeps/unix/sysv/linux/m68k/glob64.c delete mode 100644 sysdeps/unix/sysv/linux/mips/mips64/n64/glob64.c rename sysdeps/unix/sysv/linux/{i386 => }/olddirent.h (100%) rename sysdeps/unix/sysv/linux/{i386/glob64.c => oldglob.c} (61%) delete mode 100644 sysdeps/unix/sysv/linux/powerpc/powerpc32/glob64.c create mode 100644 sysdeps/unix/sysv/linux/s390/s390-32/glob64.c create mode 100644 sysdeps/unix/sysv/linux/s390/s390-32/oldglob.c delete mode 100644 sysdeps/unix/sysv/linux/sparc/sparc32/glob64.c delete mode 100644 sysdeps/unix/sysv/linux/wordsize-64/glob64.c delete mode 100644 sysdeps/unix/sysv/linux/x86_64/x32/glob.c delete mode 100644 sysdeps/wordsize-64/glob.c delete mode 100644 sysdeps/wordsize-64/glob64.c -- 2.7.4 diff --git a/posix/Makefile b/posix/Makefile index 1001e54..38cbce9 100644 --- a/posix/Makefile +++ b/posix/Makefile @@ -45,7 +45,7 @@ routines := \ getpgid setpgid getpgrp bsd-getpgrp setpgrp getsid setsid \ getresuid getresgid setresuid setresgid \ pathconf sysconf fpathconf \ - glob glob64 fnmatch regex \ + glob glob64 globfree globfree64 glob_pattern_p fnmatch regex \ confstr \ getopt getopt1 \ sched_setp sched_getp sched_sets sched_gets sched_yield sched_primax \ diff --git a/posix/glob.c b/posix/glob.c index 250bff1..dc13e26 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -37,8 +37,6 @@ #include /* Needed on stupid SunOS for assert. */ -#ifndef GLOB_ONLY_P - #include #if !defined POSIX && defined _POSIX_VERSION # define POSIX @@ -90,6 +88,8 @@ #include +#include "glob_internal.h" + #ifdef _SC_GETPW_R_SIZE_MAX # define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX) #else @@ -168,8 +168,6 @@ readdir_result_might_be_dir (struct readdir_result d) D_INO_TO_RESULT (source) \ } -#endif /* !defined GLOB_ONLY_P */ - /* Call gl_readdir on STREAM. This macro can be overridden to reduce type safety if an old interface version needs to be supported. */ #ifndef GL_READDIR @@ -252,7 +250,6 @@ static bool size_add_wrapv (size_t a, size_t b, size_t *r); static bool glob_use_alloca (size_t alloca_used, size_t len); /* We must not compile this function twice. */ -#ifndef GLOB_COMPAT_BUILD static bool size_add_wrapv (size_t a, size_t b, size_t *r) { @@ -271,7 +268,6 @@ glob_use_alloca (size_t alloca_used, size_t len) return (!size_add_wrapv (alloca_used, len, &size) && __libc_use_alloca (size)); } -#endif static int glob_in_dir (const char *pattern, const char *directory, int flags, int (*errfunc) (const char *, int), @@ -279,7 +275,6 @@ static int glob_in_dir (const char *pattern, const char *directory, extern int __glob_pattern_type (const char *pattern, int quote) attribute_hidden; -#ifndef GLOB_ONLY_P static int prefix_array (const char *prefix, char **array, size_t n) __THROWNL; static int collated_compare (const void *, const void *) __THROWNL; @@ -308,7 +303,6 @@ next_brace_sub (const char *cp, int flags) return *cp != '\0' ? cp : NULL; } -#endif /* !defined GLOB_ONLY_P */ /* Do glob searching for PATTERN, placing results in PGLOB. The bits defined above may be set in FLAGS. @@ -1355,26 +1349,6 @@ libc_hidden_def (glob) #endif -#ifndef GLOB_ONLY_P - -/* Free storage allocated in PGLOB by a previous `glob' call. */ -void -globfree (glob_t *pglob) -{ - if (pglob->gl_pathv != NULL) - { - size_t i; - for (i = 0; i < pglob->gl_pathc; ++i) - free (pglob->gl_pathv[pglob->gl_offs + i]); - free (pglob->gl_pathv); - pglob->gl_pathv = NULL; - } -} -#if defined _LIBC && !defined globfree -libc_hidden_def (globfree) -#endif - - /* Do a collated comparison of A and B. */ static int collated_compare (const void *a, const void *b) @@ -1451,58 +1425,6 @@ prefix_array (const char *dirname, char **array, size_t n) } -/* We must not compile this function twice. */ -#ifndef NO_GLOB_PATTERN_P -int -__glob_pattern_type (const char *pattern, int quote) -{ - const char *p; - int ret = 0; - - for (p = pattern; *p != '\0'; ++p) - switch (*p) - { - case '?': - case '*': - return 1; - - case '\\': - if (quote) - { - if (p[1] != '\0') - ++p; - ret |= 2; - } - break; - - case '[': - ret |= 4; - break; - - case ']': - if (ret & 4) - return 1; - break; - } - - return ret; -} - -/* Return nonzero if PATTERN contains any metacharacters. - Metacharacters can be quoted with backslashes if QUOTE is nonzero. */ -int -__glob_pattern_p (const char *pattern, int quote) -{ - return __glob_pattern_type (pattern, quote) == 1; -} -# ifdef _LIBC -weak_alias (__glob_pattern_p, glob_pattern_p) -# endif -#endif - -#endif /* !defined GLOB_ONLY_P */ - - /* Like `glob', but PATTERN is a final pathname component, and matches are searched for in DIRECTORY. The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done. diff --git a/posix/glob_internal.h b/posix/glob_internal.h new file mode 100644 index 0000000..d989a98 --- /dev/null +++ b/posix/glob_internal.h @@ -0,0 +1,57 @@ +/* Shared definition for glob and glob_pattern_p. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef GLOB_INTERNAL_H +# define GLOB_INTERNAL_H + +static inline int +__glob_pattern_type (const char *pattern, int quote) +{ + const char *p; + int ret = 0; + + for (p = pattern; *p != '\0'; ++p) + switch (*p) + { + case '?': + case '*': + return 1; + + case '\\': + if (quote) + { + if (p[1] != '\0') + ++p; + ret |= 2; + } + break; + + case '[': + ret |= 4; + break; + + case ']': + if (ret & 4) + return 1; + break; + } + + return ret; +} + +#endif /* GLOB_INTERNAL_H */ diff --git a/posix/glob_pattern_p.c b/posix/glob_pattern_p.c new file mode 100644 index 0000000..6e451f2 --- /dev/null +++ b/posix/glob_pattern_p.c @@ -0,0 +1,29 @@ +/* Return nonzero if PATTERN contains any metacharacters. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include "glob_internal.h" + +/* Return nonzero if PATTERN contains any metacharacters. + Metacharacters can be quoted with backslashes if QUOTE is nonzero. */ +int +__glob_pattern_p (const char *pattern, int quote) +{ + return __glob_pattern_type (pattern, quote) == 1; +} +weak_alias (__glob_pattern_p, glob_pattern_p) diff --git a/posix/globfree.c b/posix/globfree.c new file mode 100644 index 0000000..4ebd142 --- /dev/null +++ b/posix/globfree.c @@ -0,0 +1,37 @@ +/* Frees the dynamically allocated storage from an earlier call to glob. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* Free storage allocated in PGLOB by a previous `glob' call. */ +void +globfree (glob_t *pglob) +{ + if (pglob->gl_pathv != NULL) + { + size_t i; + for (i = 0; i < pglob->gl_pathc; ++i) + free (pglob->gl_pathv[pglob->gl_offs + i]); + free (pglob->gl_pathv); + pglob->gl_pathv = NULL; + } +} +#ifndef globfree +libc_hidden_def (globfree) +#endif diff --git a/posix/globfree64.c b/posix/globfree64.c new file mode 100644 index 0000000..cce288e --- /dev/null +++ b/posix/globfree64.c @@ -0,0 +1,27 @@ +/* Frees the dynamically allocated storage from an earlier call to glob. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* Free storage allocated in PGLOB by a previous `glob' call. */ +void +globfree64 (glob64_t *pglob) +{ +} +libc_hidden_def (globfree64) diff --git a/sysdeps/gnu/glob64.c b/sysdeps/gnu/glob64.c deleted file mode 100644 index 250ff07..0000000 --- a/sysdeps/gnu/glob64.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include - -#define dirent dirent64 -#define __readdir(dirp) __readdir64 (dirp) - -#define glob_t glob64_t -#define glob(pattern, flags, errfunc, pglob) \ - glob64 (pattern, flags, errfunc, pglob) -#define globfree(pglob) globfree64 (pglob) - -#undef stat -#define stat stat64 -#undef __lstat -#define __lstat(file, buf) __lxstat64 (_STAT_VER, file, buf) - -#define NO_GLOB_PATTERN_P 1 - -#define COMPILE_GLOB64 1 - -#include - -libc_hidden_def (glob64) -libc_hidden_def (globfree64) diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 99b3f9d..68bfc17 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -147,7 +147,7 @@ endif ifeq ($(subdir),posix) sysdep_headers += bits/initspin.h -sysdep_routines += sched_getcpu +sysdep_routines += sched_getcpu oldglob tests += tst-affinity tst-affinity-pid diff --git a/sysdeps/unix/sysv/linux/alpha/Makefile b/sysdeps/unix/sysv/linux/alpha/Makefile index 47bd189..50f4fb1 100644 --- a/sysdeps/unix/sysv/linux/alpha/Makefile +++ b/sysdeps/unix/sysv/linux/alpha/Makefile @@ -1,7 +1,3 @@ -ifeq ($(subdir),posix) -sysdep_routines += oldglob -endif - ifeq ($(subdir),stdlib) gen-as-const-headers += ucontext-offsets.sym endif diff --git a/sysdeps/unix/sysv/linux/alpha/glob.c b/sysdeps/unix/sysv/linux/alpha/glob64.c similarity index 78% rename from sysdeps/unix/sysv/linux/alpha/glob.c rename to sysdeps/unix/sysv/linux/alpha/glob64.c index 2d7d287..dd86d15 100644 --- a/sysdeps/unix/sysv/linux/alpha/glob.c +++ b/sysdeps/unix/sysv/linux/alpha/glob64.c @@ -16,36 +16,25 @@ . */ #define glob64 __no_glob64_decl -#define globfree64 __no_globfree64_decl -#include #include #include /* For Linux/Alpha we have to make the glob symbols versioned. */ #define glob(pattern, flags, errfunc, pglob) \ __new_glob (pattern, flags, errfunc, pglob) -#define globfree(pglob) \ - __new_globfree (pglob) /* We need prototypes for these new names. */ extern int __new_glob (const char *__pattern, int __flags, int (*__errfunc) (const char *, int), glob_t *__pglob); -extern void __new_globfree (glob_t *__pglob); #include #undef glob -#undef globfree #undef glob64 -#undef globfree64 versioned_symbol (libc, __new_glob, glob, GLIBC_2_1); -versioned_symbol (libc, __new_globfree, globfree, GLIBC_2_1); libc_hidden_ver (__new_glob, glob) -libc_hidden_ver (__new_globfree, globfree) weak_alias (__new_glob, glob64) -weak_alias (__new_globfree, globfree64) -libc_hidden_ver (__new_globfree, globfree64) diff --git a/sysdeps/unix/sysv/linux/alpha/globfree.c b/sysdeps/unix/sysv/linux/alpha/globfree.c new file mode 100644 index 0000000..9d159f1 --- /dev/null +++ b/sysdeps/unix/sysv/linux/alpha/globfree.c @@ -0,0 +1,36 @@ +/* Frees the dynamically allocated storage from an earlier call to glob. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define globfree64 __no_globfree64_decl + +#include +#include + +#define globfree(pglob) __new_globfree (pglob) +extern void __new_globfree (glob_t *__pglob); + +#include + +#undef globfree +#undef globfree64 + +versioned_symbol (libc, __new_globfree, globfree, GLIBC_2_1); +libc_hidden_ver (__new_globfree, globfree) + +weak_alias (__new_globfree, globfree64) +libc_hidden_ver (__new_globfree, globfree64) diff --git a/sysdeps/unix/sysv/linux/arm/glob64.c b/sysdeps/unix/sysv/linux/arm/glob64.c deleted file mode 100644 index 82a9a29..0000000 --- a/sysdeps/unix/sysv/linux/arm/glob64.c +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/sysdeps/unix/sysv/linux/glob.c b/sysdeps/unix/sysv/linux/glob.c new file mode 100644 index 0000000..153ba8c --- /dev/null +++ b/sysdeps/unix/sysv/linux/glob.c @@ -0,0 +1,23 @@ +/* Find pathnames matching a pattern. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#ifndef __OFF_T_MATCHES_OFF64_T +# include +#endif diff --git a/sysdeps/unix/sysv/linux/glob64.c b/sysdeps/unix/sysv/linux/glob64.c new file mode 100644 index 0000000..6ff6611 --- /dev/null +++ b/sysdeps/unix/sysv/linux/glob64.c @@ -0,0 +1,55 @@ +/* Find pathnames matching a pattern. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#ifdef __OFF_T_MATCHES_OFF64_T +# define glob64 __no_glob64_decl +# include +# undef glob64 +weak_alias (glob, glob64) +#else +# include +# include +# include + +# define dirent dirent64 +# define __readdir(dirp) __readdir64 (dirp) + +# define glob_t glob64_t +# define glob(pattern, flags, errfunc, pglob) \ + __glob64 (pattern, flags, errfunc, pglob) +# define globfree(pglob) globfree64 (pglob) + +# undef stat +# define stat stat64 + +# define COMPILE_GLOB64 1 + +# include + +# include "shlib-compat.h" + +# ifdef GLOB_NO_OLD_VERSION +strong_alias (__glob64, glob64) +libc_hidden_def (glob64) +# else +versioned_symbol (libc, __glob64, glob64, GLIBC_2_2); +libc_hidden_ver (__glob64, glob64) +# endif +#endif diff --git a/sysdeps/unix/sysv/linux/globfree.c b/sysdeps/unix/sysv/linux/globfree.c new file mode 100644 index 0000000..d615510 --- /dev/null +++ b/sysdeps/unix/sysv/linux/globfree.c @@ -0,0 +1,23 @@ +/* Frees the dynamically allocated storage from an earlier call to glob. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define globfree64 __no_globfree64_decl +#include +#undef globfree64 +weak_alias (globfree, globfree64) +libc_hidden_ver (globfree, globfree64) diff --git a/sysdeps/unix/sysv/linux/globfree64.c b/sysdeps/unix/sysv/linux/globfree64.c new file mode 100644 index 0000000..e69de29 diff --git a/sysdeps/unix/sysv/linux/i386/alphasort64.c b/sysdeps/unix/sysv/linux/i386/alphasort64.c index d5fd47a..04b29b6 100644 --- a/sysdeps/unix/sysv/linux/i386/alphasort64.c +++ b/sysdeps/unix/sysv/linux/i386/alphasort64.c @@ -30,7 +30,7 @@ versioned_symbol (libc, __alphasort64, alphasort64, GLIBC_2_2); #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) -#include +#include int __old_alphasort64 (const struct __old_dirent64 **a, diff --git a/sysdeps/unix/sysv/linux/i386/getdents64.c b/sysdeps/unix/sysv/linux/i386/getdents64.c index e8b257f..2010bbf 100644 --- a/sysdeps/unix/sysv/linux/i386/getdents64.c +++ b/sysdeps/unix/sysv/linux/i386/getdents64.c @@ -28,7 +28,7 @@ #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) -#include +#include #define __GETDENTS __old_getdents64 #define DIRENT_TYPE struct __old_dirent64 diff --git a/sysdeps/unix/sysv/linux/i386/readdir64.c b/sysdeps/unix/sysv/linux/i386/readdir64.c index f80b6a7..bd2375f 100644 --- a/sysdeps/unix/sysv/linux/i386/readdir64.c +++ b/sysdeps/unix/sysv/linux/i386/readdir64.c @@ -31,7 +31,7 @@ versioned_symbol (libc, __readdir64, readdir64, GLIBC_2_2); #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) -#include +#include #define __READDIR attribute_compat_text_section __old_readdir64 #define __GETDENTS __old_getdents64 diff --git a/sysdeps/unix/sysv/linux/i386/readdir64_r.c b/sysdeps/unix/sysv/linux/i386/readdir64_r.c index 344fd53..8c0262d 100644 --- a/sysdeps/unix/sysv/linux/i386/readdir64_r.c +++ b/sysdeps/unix/sysv/linux/i386/readdir64_r.c @@ -31,7 +31,7 @@ versioned_symbol (libc, __readdir64_r, readdir64_r, GLIBC_2_2); #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) -#include +#include #define __READDIR_R attribute_compat_text_section __old_readdir64_r #define __GETDENTS __old_getdents64 diff --git a/sysdeps/unix/sysv/linux/i386/versionsort64.c b/sysdeps/unix/sysv/linux/i386/versionsort64.c index 3e1c6ea..87f2f95 100644 --- a/sysdeps/unix/sysv/linux/i386/versionsort64.c +++ b/sysdeps/unix/sysv/linux/i386/versionsort64.c @@ -30,7 +30,7 @@ versioned_symbol (libc, __versionsort64, versionsort64, GLIBC_2_2); #if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) -#include +#include int __old_versionsort64 (const struct __old_dirent64 **a, diff --git a/sysdeps/unix/sysv/linux/m68k/glob64.c b/sysdeps/unix/sysv/linux/m68k/glob64.c deleted file mode 100644 index 82a9a29..0000000 --- a/sysdeps/unix/sysv/linux/m68k/glob64.c +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/sysdeps/unix/sysv/linux/mips/mips64/n64/glob64.c b/sysdeps/unix/sysv/linux/mips/mips64/n64/glob64.c deleted file mode 100644 index 33918ea..0000000 --- a/sysdeps/unix/sysv/linux/mips/mips64/n64/glob64.c +++ /dev/null @@ -1 +0,0 @@ -/* glob64 is in glob.c */ diff --git a/sysdeps/unix/sysv/linux/i386/olddirent.h b/sysdeps/unix/sysv/linux/olddirent.h similarity index 100% rename from sysdeps/unix/sysv/linux/i386/olddirent.h rename to sysdeps/unix/sysv/linux/olddirent.h diff --git a/sysdeps/unix/sysv/linux/i386/glob64.c b/sysdeps/unix/sysv/linux/oldglob.c similarity index 61% rename from sysdeps/unix/sysv/linux/i386/glob64.c rename to sysdeps/unix/sysv/linux/oldglob.c index 2dcbe33..af934f9 100644 --- a/sysdeps/unix/sysv/linux/i386/glob64.c +++ b/sysdeps/unix/sysv/linux/oldglob.c @@ -1,5 +1,5 @@ -/* Two glob variants with 64-bit support, for dirent64 and __olddirent64. - Copyright (C) 1998-2017 Free Software Foundation, Inc. +/* Find pathnames matching a pattern. Compatibility version. + Copyright (C) 2017 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -16,61 +16,32 @@ License along with the GNU C Library; if not, see . */ -#include #include -#include - -#define dirent dirent64 -#define __readdir(dirp) __readdir64 (dirp) - -#define glob_t glob64_t -#define glob(pattern, flags, errfunc, pglob) \ - __glob64 (pattern, flags, errfunc, pglob) -#define globfree(pglob) globfree64 (pglob) - -#undef stat -#define stat stat64 -#undef __lstat -#define __lstat(file, buf) __lxstat64 (_STAT_VER, file, buf) - -#define NO_GLOB_PATTERN_P 1 - -#define COMPILE_GLOB64 1 - -#include - #include "shlib-compat.h" -libc_hidden_def (globfree64) - -versioned_symbol (libc, __glob64, glob64, GLIBC_2_2); -libc_hidden_ver (__glob64, glob64) - -#if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) +#if SHLIB_COMPAT(libc, GLIBC_2_1, GLIBC_2_2) \ + && !defined(GLOB_NO_OLD_VERSION) -#include +#include int __old_glob64 (const char *__pattern, int __flags, int (*__errfunc) (const char *, int), glob64_t *__pglob); -#undef dirent +#define glob_t glob64_t +#define globfree(pglob) globfree64 (pglob) +#undef stat +#define stat stat64 + #define dirent __old_dirent64 -#undef GL_READDIR -# define GL_READDIR(pglob, stream) \ +#define GL_READDIR(pglob, stream) \ ((struct __old_dirent64 *) (pglob)->gl_readdir (stream)) -#undef __readdir #define __readdir(dirp) __old_readdir64 (dirp) -#undef glob #define glob(pattern, flags, errfunc, pglob) \ __old_glob64 (pattern, flags, errfunc, pglob) #define convert_dirent __old_convert_dirent -#define glob_in_dir __old_glob_in_dir -#define GLOB_ATTRIBUTE attribute_compat_text_section -#define GLOB_ONLY_P 1 - -#define GLOB_COMPAT_BUILD 1 +#define GLOB_ATTRIBUTE attribute_compat_text_section #include diff --git a/sysdeps/unix/sysv/linux/powerpc/powerpc32/glob64.c b/sysdeps/unix/sysv/linux/powerpc/powerpc32/glob64.c deleted file mode 100644 index 82a9a29..0000000 --- a/sysdeps/unix/sysv/linux/powerpc/powerpc32/glob64.c +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/glob64.c b/sysdeps/unix/sysv/linux/s390/s390-32/glob64.c new file mode 100644 index 0000000..b00dc54 --- /dev/null +++ b/sysdeps/unix/sysv/linux/s390/s390-32/glob64.c @@ -0,0 +1,20 @@ +/* Find pathnames matching a pattern. S390-32 Linux version. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#define GLOB_NO_OLD_VERSION +#include diff --git a/sysdeps/unix/sysv/linux/s390/s390-32/oldglob.c b/sysdeps/unix/sysv/linux/s390/s390-32/oldglob.c new file mode 100644 index 0000000..56d7d12 --- /dev/null +++ b/sysdeps/unix/sysv/linux/s390/s390-32/oldglob.c @@ -0,0 +1,2 @@ +#define GLOB_NO_OLD_VERSION +#include diff --git a/sysdeps/unix/sysv/linux/sparc/sparc32/glob64.c b/sysdeps/unix/sysv/linux/sparc/sparc32/glob64.c deleted file mode 100644 index 82a9a29..0000000 --- a/sysdeps/unix/sysv/linux/sparc/sparc32/glob64.c +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/sysdeps/unix/sysv/linux/wordsize-64/glob64.c b/sysdeps/unix/sysv/linux/wordsize-64/glob64.c deleted file mode 100644 index eab7703..0000000 --- a/sysdeps/unix/sysv/linux/wordsize-64/glob64.c +++ /dev/null @@ -1,2 +0,0 @@ -/* This file is here so sysdeps/gnu/glob64.c doesn't take precedence. */ -#include diff --git a/sysdeps/unix/sysv/linux/x86_64/x32/glob.c b/sysdeps/unix/sysv/linux/x86_64/x32/glob.c deleted file mode 100644 index e542747..0000000 --- a/sysdeps/unix/sysv/linux/x86_64/x32/glob.c +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/sysdeps/wordsize-64/glob.c b/sysdeps/wordsize-64/glob.c deleted file mode 100644 index 082faf1..0000000 --- a/sysdeps/wordsize-64/glob.c +++ /dev/null @@ -1,8 +0,0 @@ -#define glob64 __no_glob64_decl -#define globfree64 __no_globfree64_decl -#include -#undef glob64 -#undef globfree64 -weak_alias (glob, glob64) -weak_alias (globfree, globfree64) -libc_hidden_ver (globfree, globfree64) diff --git a/sysdeps/wordsize-64/glob64.c b/sysdeps/wordsize-64/glob64.c deleted file mode 100644 index 33918ea..0000000 --- a/sysdeps/wordsize-64/glob64.c +++ /dev/null @@ -1 +0,0 @@ -/* glob64 is in glob.c */ From patchwork Thu Jun 8 21:13:20 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103409 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632432qgd; Thu, 8 Jun 2017 14:14:49 -0700 (PDT) X-Received: by 10.99.152.70 with SMTP id l6mr39093632pgo.136.1496956489177; Thu, 08 Jun 2017 14:14:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956489; cv=none; d=google.com; s=arc-20160816; b=BrwlRP4i+t+0ySNTiaFIXgOd4nqodfGzlNzmRIC1NM3EgCrMm75qgqllpVR9JIKrMw nDNWRJf7JIo9y8b7NwPQo63xnxSrY61wtx7yoAmdoFNfPPP2G3fXmrxnTW2fm6/kWI3U Fjjix6KdUV3UObk82vqo0iYVZL34sqyLkPB1ee4ryT1K8DlnHHEbc/NUAZTrLVSRjnDp ReFKWt4GwQ8S6fiFTUrMkXwJcIKnvYH4qPYTWBiAyL+R6YpV93JF4i3tsLTVmrEXKBh4 LvJ9JpDHVTqpJ8L7ea3KFStIYwde86+xLLHaj2BlSFS32cjVQ54GgKqzb1Xf9LMYz0rL 4LTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :delivered-to:sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=S9NkWFtoWCPwibyNfQLlxTWcXfbQzaOWTw6TyEwtquU=; b=nVvcx0oqrFKQgPyY2fCRfcyNn+HrRrKPCK+9CNkseyVMWne0CTCqQfUVPsY5kxn5Ua so/1+HMe2k9ZeG9QQgKpEa5jGQ851P0mz1d1G5HU2b+FA7XI2urBSk5U3Bnc+XnZOx3/ YebV/te7SoCMva1lDoJPJY5k6yIOJwVnHUHYsx4Ud83kZ6y1rjOJv9fMxENmGKzbRGzm NgKmGV0mVvlVLLFOtOLXHBrr70pfhyYWS+oPVooEK0lP1RHxOvnWIPGbDAJqeprXjhRJ ulpKP1sWXSrR1aVB1URx9jXiK3si1BHgY9+rRZIivZXhZo/Pij4AcDnbkfUbRvGP0aQo kvfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80203-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80203-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id j5si5168992pgk.297.2017.06.08.14.14.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:14:49 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80203-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80203-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80203-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=ACWUJBSf5sEmyMjRidULKKo8D1Wkbko azvfH3Tnj16rYwtsrlbW+OSHV3IYAsRPvn5p3ltCmktt8ARvjIKga/ay+1XehjoO 2I1AXNV4a/tBx8B7CvxyeMi6/Lrt6d4Vsko3sbyEQ/IHieVrIfFl4NSZGWxfenA2 fOFa7XY55RZs= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:cc:subject:date:message-id:in-reply-to :references; s=default; bh=GG4SGFs6n8pTKWIQDfqprmdGg1g=; b=UhKK2 JEkQmVNKZsE+daFuN1VEOYaDDZTQQO0421EW/66B15Gfc9/aC94HvXPpG7MxuuzF wZSfelXNQUF48G4CO7tvdHLtmWMwIcFhDGucqDGmqMtx5+1gc1R4eykmPc5ivgah SU77jb13AB5CJNAVtLp34UT6zEqUnn8LgFV7dc= Received: (qmail 38543 invoked by alias); 8 Jun 2017 21:13:58 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 38443 invoked by uid 89); 8 Jun 2017 21:13:57 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS, UNSUBSCRIBE_BODY autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f179.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=S9NkWFtoWCPwibyNfQLlxTWcXfbQzaOWTw6TyEwtquU=; b=ND1maLQ5TIRMbZn47ez1umUbR5EDxpkcbAMXEchzNdZdoJy3LnILlNvSx/S3A0xOrm AdA+5ThGGbDpKH09gXU4MjBbohUkyJ8dZsd6qTTNz6yTyaZI1ELl/4BkNuO1g6yj5GFE kRMQMtbTHLT83W90ypvL7As7xwSjx6osCqzudnnzeVKoF1SoEoa2RCBQYLVV935goQvv RHKPQNCiIY1y1lWBBD8wiYEPLF3gDsvK4Vrt1gbjKFpL54FI6DECtUlVFsWDDocbj6+u iAfGonxrK2xHaxMA9ScsxZ/YBwpfMODdn2jd79ARNP+XJ85gqlH37BOm9pk2R6JFB1pj EpQw== X-Gm-Message-State: AKS2vOztJregezTZpwfvnzsbONVdXMdUAv7qIkwycj2CW9b5zyFR5MNX i2ez9RFiJjw3ecSQALDBGw== X-Received: by 10.55.120.199 with SMTP id t190mr44400741qkc.235.1496956437601; Thu, 08 Jun 2017 14:13:57 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Florian Weimer Subject: [PATCH 06/17] posix: Rewrite to use struct scratch_buffer instead of extend_alloca Date: Thu, 8 Jun 2017 18:13:20 -0300 Message-Id: <1496956411-25594-7-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> From: Florian Weimer This patch removes a lot of boilerplate code to manager buffers for getpwnam_r. Checked on x86_64-linux-gnu. [BZ #18023] * posix/glob.c (glob): Use struct scratch_buffer instead of extend_alloca. --- posix/glob.c | 147 +++++++++++------------------------------------------------ 1 file changed, 26 insertions(+), 121 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index dc13e26..425d81b 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -88,13 +88,9 @@ #include +#include #include "glob_internal.h" -#ifdef _SC_GETPW_R_SIZE_MAX -# define GETPW_R_SIZE_MAX() sysconf (_SC_GETPW_R_SIZE_MAX) -#else -# define GETPW_R_SIZE_MAX() (-1) -#endif #ifdef _SC_LOGIN_NAME_MAX # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX) #else @@ -696,97 +692,43 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (success) { struct passwd *p; - char *malloc_pwtmpbuf = NULL; - char *pwtmpbuf; # if defined HAVE_GETPWNAM_R || defined _LIBC - long int pwbuflenmax = GETPW_R_SIZE_MAX (); - size_t pwbuflen = pwbuflenmax; struct passwd pwbuf; int save = errno; + struct scratch_buffer pwtmpbuf; + scratch_buffer_init (&pwtmpbuf); -# ifndef _LIBC - if (! (0 < pwbuflenmax && pwbuflenmax <= SIZE_MAX)) - /* `sysconf' does not support _SC_GETPW_R_SIZE_MAX. - Try a moderate value. */ - pwbuflen = 1024; -# endif - if (glob_use_alloca (alloca_used, pwbuflen)) - pwtmpbuf = alloca_account (pwbuflen, alloca_used); - else - { - pwtmpbuf = malloc (pwbuflen); - if (pwtmpbuf == NULL) - { - if (__glibc_unlikely (malloc_name)) - free (name); - retval = GLOB_NOSPACE; - goto out; - } - malloc_pwtmpbuf = pwtmpbuf; - } - - while (getpwnam_r (name, &pwbuf, pwtmpbuf, pwbuflen, &p) + while (getpwnam_r (name, &pwbuf, + pwtmpbuf.data, pwtmpbuf.length, &p) != 0) { - size_t newlen; - bool v; if (errno != ERANGE) { p = NULL; break; } - v = size_add_wrapv (pwbuflen, pwbuflen, &newlen); - if (!v && malloc_pwtmpbuf == NULL - && glob_use_alloca (alloca_used, newlen)) - pwtmpbuf = extend_alloca_account (pwtmpbuf, pwbuflen, - newlen, alloca_used); - else + if (!scratch_buffer_grow (&pwtmpbuf)) { - char *newp = (v ? NULL - : realloc (malloc_pwtmpbuf, newlen)); - if (newp == NULL) - { - free (malloc_pwtmpbuf); - if (__glibc_unlikely (malloc_name)) - free (name); - retval = GLOB_NOSPACE; - goto out; - } - malloc_pwtmpbuf = pwtmpbuf = newp; + retval = GLOB_NOSPACE; + goto out; } - pwbuflen = newlen; __set_errno (save); } # else - p = getpwnam (name); + p = getpwnam (pwtmpbuf.data); # endif - if (__glibc_unlikely (malloc_name)) - free (name); if (p != NULL) { - if (malloc_pwtmpbuf == NULL) - home_dir = p->pw_dir; - else + home_dir = strdup (p->pw_dir); + malloc_home_dir = 1; + if (home_dir == NULL) { - size_t home_dir_len = strlen (p->pw_dir) + 1; - if (glob_use_alloca (alloca_used, home_dir_len)) - home_dir = alloca_account (home_dir_len, - alloca_used); - else - { - home_dir = malloc (home_dir_len); - if (home_dir == NULL) - { - free (pwtmpbuf); - retval = GLOB_NOSPACE; - goto out; - } - malloc_home_dir = 1; - } - memcpy (home_dir, p->pw_dir, home_dir_len); + scratch_buffer_free (&pwtmpbuf); + retval = GLOB_NOSPACE; + goto out; } } - free (malloc_pwtmpbuf); + scratch_buffer_free (&pwtmpbuf); } else { @@ -924,59 +866,25 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* Look up specific user's home directory. */ { struct passwd *p; - char *malloc_pwtmpbuf = NULL; + struct scratch_buffer pwtmpbuf; + scratch_buffer_init (&pwtmpbuf); + # if defined HAVE_GETPWNAM_R || defined _LIBC - long int buflenmax = GETPW_R_SIZE_MAX (); - size_t buflen = buflenmax; - char *pwtmpbuf; struct passwd pwbuf; int save = errno; -# ifndef _LIBC - if (! (0 <= buflenmax && buflenmax <= SIZE_MAX)) - /* Perhaps 'sysconf' does not support _SC_GETPW_R_SIZE_MAX. Try a - moderate value. */ - buflen = 1024; -# endif - if (glob_use_alloca (alloca_used, buflen)) - pwtmpbuf = alloca_account (buflen, alloca_used); - else - { - pwtmpbuf = malloc (buflen); - if (pwtmpbuf == NULL) - { - nomem_getpw: - if (__glibc_unlikely (malloc_user_name)) - free (user_name); - retval = GLOB_NOSPACE; - goto out; - } - malloc_pwtmpbuf = pwtmpbuf; - } - - while (getpwnam_r (user_name, &pwbuf, pwtmpbuf, buflen, &p) != 0) + while (getpwnam_r (user_name, &pwbuf, + pwtmpbuf.data, pwtmpbuf.length, &p) != 0) { - size_t newlen; - bool v; if (errno != ERANGE) { p = NULL; break; } - v = size_add_wrapv (buflen, buflen, &newlen); - if (!v && malloc_pwtmpbuf == NULL - && glob_use_alloca (alloca_used, newlen)) - pwtmpbuf = extend_alloca_account (pwtmpbuf, buflen, - newlen, alloca_used); - else + if (!scratch_buffer_grow (&pwtmpbuf)) { - char *newp = v ? NULL : realloc (malloc_pwtmpbuf, newlen); - if (newp == NULL) - { - free (malloc_pwtmpbuf); - goto nomem_getpw; - } - malloc_pwtmpbuf = pwtmpbuf = newp; + retval = GLOB_NOSPACE; + goto out; } __set_errno (save); } @@ -1005,7 +913,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), dirname = malloc (home_len + rest_len + 1); if (dirname == NULL) { - free (malloc_pwtmpbuf); + scratch_buffer_free (&pwtmpbuf); retval = GLOB_NOSPACE; goto out; } @@ -1016,18 +924,15 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), dirlen = home_len + rest_len; dirname_modified = 1; - - free (malloc_pwtmpbuf); } else { - free (malloc_pwtmpbuf); - if (flags & GLOB_TILDE_CHECK) /* We have to regard it as an error if we cannot find the home directory. */ return GLOB_NOMATCH; } + scratch_buffer_free (&pwtmpbuf); } } # endif /* Not Amiga && not WINDOWS32. */ From patchwork Thu Jun 8 21:13:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103412 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632641qgd; Thu, 8 Jun 2017 14:15:23 -0700 (PDT) X-Received: by 10.84.253.23 with SMTP id z23mr37160411pll.11.1496956523729; Thu, 08 Jun 2017 14:15:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956523; cv=none; d=google.com; s=arc-20160816; b=nSY02A1OSNAVHZl4mX7FG9oAGbcVWkjsCQwQByTMgeR62oRnvyXg7uNAyc4bQEmhxQ HGks5IRNr7JkkmtSbjRFPw+gA2AHRU5fiDksMTn5aPPcl+f6Zdo6QNhC5aNyiIPQ0Hse 4F87cOPcxsf/xrJA9esUXrwzvxW493eobr7h0lu2jz+KhU8VuYtuuXHtGD+P5oYUSAmM DPb43LtDrL+cn1QSnaUhYUQuUSZ0fWjb2WI/hQuwQp3A+T3NHQRYfoKlhJkSgmau1YCT ORkkxkT568W4k2QTdKD3sxqekQF9fpNuvOk80JpKcHSd3SymZhKOMQuVpnFo0NPEAUXG SojQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=zvQtk2LxFfAa/A9xrzK/27Oe92vcRC2B+RfjFRVE4+Q=; b=ndWuzu4FV3dC2/2985kjz2w0WV+JAp1iZWN0EqewWh3arB5SEKBP9zgKvMvm4cc18z 2RyOPD1qVCaXx8O9SWonaPVeFRnkEx68vH/HfaG6LFqp5Zmb+EdE720mOkRJjKXc5yx+ h3Dmp7hyG8z2BflLEAFVPLw3OjzS1Ca+ssbHI/XKkFbr8iCkctmTvRxHNAVq7q0jIA4n IolGEMt/7rS5p6iJM9tHinMCjqI+FXYZc3jnLnGREhqiV8P15L09zbSQ5PmO02B9Kz9X lYG6sxBmAs+iBxILTGPcBAu4oGANP+ltqaNwvvtNcUTMtRPm21iGyQdQIPgbh7QW4x55 b/rQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80206-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80206-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id j15si5219081pga.273.2017.06.08.14.15.23 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:15:23 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80206-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80206-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80206-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=MDFLTIRgRfi97LmmdjCEaFBwo7xga3x 4ftE2PY0e0MAhr9bmsRZlzi98LqjJu0i9pNFfUIhK6FYqRcWhRpbtfxKFNGttTHb m4LJzWtv721c3xseQrpy5k/aaJfyAIOJTlSb+8uBC34OVCi90VWvDWLzv7qSNUCH G4UqUQ4Ro3RI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=tAz8zKsS6JfIV2VbPHUjyvTWW1Q=; b=jmQz9 6wh6hbn2lJXGmVV4jSNBHBxClrQcmrWxc5xoWHVeafDg0fLlbksutfq2SLwqsnl7 vYZlYwwdve0R2zSIt1Wnl1aWG7MS69zzNaEzhZ3BSBDLTeTLOFk1vSo7+27alzD8 lG/e0isZ1pcBUSMALc6uBTb5EX73PwfHM5lD84= Received: (qmail 39150 invoked by alias); 8 Jun 2017 21:14:04 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 38967 invoked by uid 89); 8 Jun 2017 21:14:03 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=maintaining, 1014 X-HELO: mail-qt0-f176.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=zvQtk2LxFfAa/A9xrzK/27Oe92vcRC2B+RfjFRVE4+Q=; b=GML72pi3SPkQMYFtpb8FepYJQzrpaowYoRTzrfPNpXh8/aRbAm1LL6s3XDTbJzTT/J +l34Vl3nvETNwFXlWcj86YHYW9BnuXZIXBxYcAu2Saw+nnhxuqMINj7oebX/iPz+UasV f97ZznYi9OPGiiFotlfdioMrRvRsRsXwwBN1V0NcRjuFGsT4pbcmjTh3SArGYgoFk9iS hkJl5rJnOHh+GPXm5L6wddm2W4J/yjfJAZe2OjH0OV6eZl/H55uLa4TiRL0MpGs8vk+t VrRvl3jRKsVyGj7ds9+bKeU254Zog268gynKyQiDGn7RSusd2eB3Xg9oci5blftCu9cL lkjw== X-Gm-Message-State: AODbwcBSyMST7crFZQR9ePeQ0sSH48MHZobDKMrKCU4VjwakQ269Y8Sh Ez4CXDbRpl7kKRSgHZKW2Q== X-Received: by 10.237.62.8 with SMTP id l8mr46309110qtf.20.1496956439994; Thu, 08 Jun 2017 14:13:59 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 07/17] malloc: Add specialized dynarray for C strings Date: Thu, 8 Jun 2017 18:13:21 -0300 Message-Id: <1496956411-25594-8-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch adds an specialized dynarray to manage C strings using the dynarray internal implementation. It uses some private fields from dynarray and thus it provided specific files to access and manage the internal string buffer. For instance: struct char_array str; // str == "testing" char_array_init_str (&str, "testing"); // c == 's' char c = char_array_pos (&str, 2); // str = "testing2" char_array_set_str (&str, "testing2"); // str = "testi" char_array_erase (&str, 5); // str = "123testi" char_array_prepend_str (&str, "123"); // len = 8 size_t len = char_array_length (&str); // str = "123testi456" char_array_append_str (&str, "456"); // str = "123testi789" char_array_replace_str_pos (&str, 7, "789", 3); The provided function are not extensive and meant mainly to be use in subsequent glob implementation cleanup. For internal object consistency only the function provided by char_array.c should be used, including internal object manipulation. To check for possible overflows in internal size manipulation a new function, check_add_wrapv_size_t, is added on malloc-internal. It basically return whether the addition of two size_t overflows. Checked on x86_64-linux-gnu. * malloc/Makefile (test-internal): Add tst-char_array. * malloc/malloc-internal (check_add_wrapv_size_t): New function. * malloc/char_array.c: New file. * malloc/tst-char-array.c: Likewise. --- malloc/Makefile | 1 + malloc/char_array.c | 256 +++++++++++++++++++++++++++++++++++++++++++++++ malloc/malloc-internal.h | 14 +++ malloc/tst-char_array.c | 107 ++++++++++++++++++++ 4 files changed, 378 insertions(+) create mode 100644 malloc/char_array.c create mode 100644 malloc/tst-char_array.c -- 2.7.4 diff --git a/malloc/Makefile b/malloc/Makefile index af025cb..098e3c6 100644 --- a/malloc/Makefile +++ b/malloc/Makefile @@ -46,6 +46,7 @@ tests-internal += \ tst-dynarray \ tst-dynarray-fail \ tst-dynarray-at-fail \ + tst-char_array ifneq (no,$(have-tunables)) tests += tst-malloc-usable-tunables diff --git a/malloc/char_array.c b/malloc/char_array.c new file mode 100644 index 0000000..cce9360 --- /dev/null +++ b/malloc/char_array.c @@ -0,0 +1,256 @@ +/* Specialized dynarray for C strings. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* This file provides a dynamic C string with an initial stack allocated + buffer. Since it is based on dynarray, it provided dynamic size + expansion and heap usage for large strings. + + The following parameters are optional: + + CHAR_ARRAY_INITIAL_SIZE + The size of the statically allocated array (default is 256). It will + be used to define DYNARRAY_INITIAL_SIZE. + + The following functions are provided: + + bool char_array_init_empty (struct char_array *); + bool char_array_init_str (struct char_array *, const char *); + bool char_array_init_str_size (struct char_array *, const char *, size_t); + bool char_array_is_empty (struct char_array *); + const char *char_array_str (struct char_array *); + char char_array_pos (struct char_array *, size_t); + size_t char_array_length (struct char_array *); + bool char_array_set_str (struct char_array *, const char *); + bool char_array_set_str_size (struct char_array *, const char *, size_t); + void char_array_erase (struct char_array *, size_t); + bool char_array_resize_str (struct char_array *, size_t); + bool char_array_prepend_str (struct char_array *, const char *); + bool char_array_append_str (struct char_array *, const char *); + bool char_array_replace_str_pos (struct char_array *, size_t, const char *, + size_t); + + For instance: + + struct char_array str; + // str == "testing"; + char_array_init_str (&str, "testing"); + // c == 's' + char c = char_array_pos (&str, 2); + // str = "testing2"; + char_array_set_str (&str, "testing2"); + // str = "testi"; + char_array_erase (&str, 5); + // str = "123testi"; + char_array_prepend_str (&str, "123"); + // len = 8; + size_t len = char_array_length (&str); + // str = "123testi456"; + char_array_append_str (&str, "456"); + // str = "123testi789"; + char_array_replace_str_pos (&str, 7, "789", 3); + */ + +#define DYNARRAY_STRUCT char_array +#define DYNARRAY_ELEMENT char +#define DYNARRAY_PREFIX char_array_ +#define DYNARRAY_ELEMENT_INIT(__e) (*__e = '\0') +#ifndef CHAR_ARRAY_INITIAL_SIZE +# define CHAR_ARRAY_INITIAL_SIZE 256 +#endif +#define DYNARRAY_INITIAL_SIZE CHAR_ARRAY_INITIAL_SIZE +#include +#include + +/* Return a const char for the internal C string handled by 'array'. */ +static const char * __attribute_used__ +char_array_str (struct char_array *array) +{ + return char_array_at (array, 0); +} + +/* Return the character at position 'pos' from the char_array 'array'. */ +static char __attribute_used__ +char_array_pos (struct char_array *array, size_t pos) +{ + return *char_array_at (array, pos); +} + +static size_t __attribute_used__ +char_array_length (struct char_array *array) +{ + /* Exclude the final '\0'. */ + return array->dynarray_header.used - 1; +} + +/* Copy the contents of string 'str' to char_array 'array', including the + final '\0'. */ +static bool __attribute_used__ +char_array_set_str (struct char_array *array, const char *str) +{ + size_t size = strlen (str) + 1; + if (!char_array_resize (array, size)) + return false; + memcpy (array->dynarray_header.array, str, size); + array->dynarray_header.used = size; + return true; +} + +/* Copy up 'size' bytes from string 'str' to char_array 'array'. A final + '\0' is appended in the char_array. */ +static bool __attribute_used__ +char_array_set_str_size (struct char_array *array, const char *str, + size_t size) +{ + size_t newsize; + if (check_add_wrapv_size_t (size, 1, &newsize) + || !char_array_resize (array, newsize)) + return false; + *((char *) mempcpy (array->dynarray_header.array, str, size)) = '\0'; + array->dynarray_header.used = newsize; + return true; +} + +/* Initialize the char_array 'array' and sets it to an empty string (""). */ +static bool __attribute_used__ +char_array_init_empty (struct char_array *array) +{ + char_array_init (array); + return char_array_set_str (array, ""); +} + +/* Initialize the char_array 'array' and copy the content of string 'str'. */ +static bool __attribute_used__ +char_array_init_str (struct char_array *array, const char *str) +{ + char_array_init (array); + return char_array_set_str (array, str); +} + +/* Initialize the char_array 'array' and copy the content of string 'str' + up to 'size' characteres. */ +static bool __attribute_used__ +char_array_init_str_size (struct char_array *array, const char *str, + size_t size) +{ + char_array_init (array); + return char_array_set_str_size (array, str, size); +} + +static bool __attribute_used__ +char_array_is_empty (struct char_array *array) +{ + return *char_array_at (array, 0) == '\0'; +} + +/* Remove the byte at position 'pos' from char_array 'array'. The contents + are moved internally if the position is not at the end of the internal + buffer. */ +static bool __attribute_used__ +char_array_erase (struct char_array *array, size_t pos) +{ + if (pos >= array->dynarray_header.used - 1) + return false; + + char *ppos = char_array_at (array, pos); + char *lpos = array->dynarray_header.array + array->dynarray_header.used; + ptrdiff_t size = lpos - ppos; + memmove (ppos, ppos + 1, size); + array->dynarray_header.used--; + return true; +} + +/* Resize the char_array 'array' to size 'count' maintaining the ending + '\0' byte. */ +static bool __attribute_used__ +char_array_crop (struct char_array *array, size_t size) +{ + if (size >= (array->dynarray_header.used - 1) + || !char_array_resize (array, size + 1)) + return false; + + array->dynarray_header.array[array->dynarray_header.used] = '\0'; + return true; +} + +/* Prepend the contents of string 'str' to char_array 'array', including the + final '\0' byte. */ +static bool __attribute_used__ +char_array_prepend_str (struct char_array *array, const char *str) +{ + size_t size = strlen (str); + /* Resizing the array might change its used elements and we need below + to correct copy the elements. */ + size_t used = array->dynarray_header.used; + + size_t newsize; + if (check_add_wrapv_size_t (used, size, &newsize) + || !char_array_resize (array, newsize)) + return false; + + /* Make room for the string and copy it. */ + memmove (array->dynarray_header.array + size, array->dynarray_header.array, + used); + memcpy (array->dynarray_header.array, str, size); + array->dynarray_header.used = newsize; + return true; +} + +/* Append the contents of string 'str' to char_array 'array, including the + final '\0' byte. */ +static bool __attribute_used__ +char_array_append_str (struct char_array *array, const char *str) +{ + size_t size = strlen (str); + /* Resizing the array might change its used elements and it used it below + to correct copy the elements. */ + size_t used = array->dynarray_header.used - 1; + + /* array 'used' does account for final '\0', so there is no need to add + an extra element to calculate the final required size. */ + size_t newsize; + if (check_add_wrapv_size_t (used + 1, size, &newsize) + || !char_array_resize (array, newsize)) + return false; + + /* Start to append at '\0' up to string length and add a final '\0'. */ + *(char*) mempcpy (array->dynarray_header.array + used, str, size) = '\0'; + array->dynarray_header.used = newsize; + return true; +} + +/* Replace the contents starting of position 'pos' of char_array 'array' + with the contents of string 'str' up to 'len' bytes. A final '\0' + is appended in the string. */ +static bool __attribute_used__ +char_array_replace_str_pos (struct char_array *array, size_t pos, + const char *str, size_t len) +{ + if (pos > array->dynarray_header.used) + return false; + + size_t newsize; + if (check_add_wrapv_size_t (pos, len, &newsize) + || check_add_wrapv_size_t (newsize, 1, &newsize) + || !char_array_resize (array, newsize)) + return false; + + char *start = char_array_at (array, pos); + *(char *) mempcpy (start, str, len) = '\0'; + array->dynarray_header.used = newsize; + return true; +} diff --git a/malloc/malloc-internal.h b/malloc/malloc-internal.h index dbd801a..3066cd3 100644 --- a/malloc/malloc-internal.h +++ b/malloc/malloc-internal.h @@ -101,4 +101,18 @@ check_mul_overflow_size_t (size_t left, size_t right, size_t *result) #endif } +/* Set *R = A + B. Return true if the answer is mathematically incorrect due + to overflow; in this case, *R is the low order bits of the correct + answer. */ +static inline bool +check_add_wrapv_size_t (size_t a, size_t b, size_t *r) +{ +#if 5 <= __GNUC__ + return __builtin_add_overflow (a, b, r); +#else + *r = a + b; + return *r < a; +#endif +} + #endif /* _MALLOC_INTERNAL_H */ diff --git a/malloc/tst-char_array.c b/malloc/tst-char_array.c new file mode 100644 index 0000000..53f9482 --- /dev/null +++ b/malloc/tst-char_array.c @@ -0,0 +1,107 @@ +/* Test for char_array. + Copyright (C) 2017 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +#include + +#include +#include +#include +#include +#include + +static int +do_test (void) +{ + mtrace (); + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_empty (&str) == true); + TEST_VERIFY_EXIT (char_array_length (&str) == 0); + TEST_VERIFY_EXIT (char_array_is_empty (&str) == true); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str (&str, "testing")); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("testing")); + TEST_VERIFY_EXIT (char_array_pos (&str, 2) == 's'); + TEST_VERIFY_EXIT (char_array_is_empty (&str) == false); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "testing") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str_size (&str, "testing", 4)); + TEST_VERIFY_EXIT (char_array_length (&str) == 4); + TEST_VERIFY_EXIT (char_array_pos (&str, 2) == 's'); + TEST_VERIFY_EXIT (char_array_is_empty (&str) == false); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "test") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str (&str, "testing")); + TEST_VERIFY_EXIT (char_array_set_str (&str, "abcdef")); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "abcdef") == 0); + TEST_VERIFY_EXIT (char_array_set_str_size (&str, "abcdef", 4)); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "abcd") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str (&str, "testing")); + TEST_VERIFY_EXIT (char_array_erase (&str, 4) == true); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("testing") - 1); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "testng") == 0); + TEST_VERIFY_EXIT (char_array_erase (&str, char_array_length (&str)) + == false); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("testing") - 1); + TEST_VERIFY_EXIT (char_array_erase (&str, char_array_length (&str) - 1) + == true); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("testing") - 2); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "testn") == 0); + char_array_free (&str); + } + + { + struct char_array str; + TEST_VERIFY_EXIT (char_array_init_str (&str, "test")); + TEST_VERIFY_EXIT (char_array_prepend_str (&str, "123")); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "123test") == 0); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("123test")); + TEST_VERIFY_EXIT (char_array_append_str (&str, "456")); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "123test456") == 0); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("123test456")); + TEST_VERIFY_EXIT (char_array_replace_str_pos (&str, 7, "789", 3)); + TEST_VERIFY_EXIT (strcmp (char_array_str (&str), "123test789") == 0); + TEST_VERIFY_EXIT (char_array_length (&str) == strlen ("123test789")); + char_array_free (&str); + } + + return 0; +} + +#include From patchwork Thu Jun 8 21:13:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103415 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632836qgd; Thu, 8 Jun 2017 14:16:00 -0700 (PDT) X-Received: by 10.84.225.145 with SMTP id u17mr37395096plj.241.1496956560640; Thu, 08 Jun 2017 14:16:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956560; cv=none; d=google.com; s=arc-20160816; b=IgM+Fg2MwXoMsxdaHKTFq2iTieg7bpHY8p/vxGoRDYVU4KsdlLzl1xBUX7WSjJwQ4b Ssvy7VFspJgwJL6awsQrP3SjduplAvZ1WUwwUHZHoe1bwtThtSNt0ayFWj7MgH8Rk+g5 rJEl5akvzuFHjR2/i51reHAC5esBDlcTIQvyzSQFGPhxyLkPqpw7lG3yh6nR56Xynx5r etnN9HQ1bQWHW5lwOqa9zqvQaKdFQoT9AF80a2ZDvDpq/d7IbGqeoGXTFQGtWC4xCCrs jQX+LZiPHebWwLjo74UkGAQ6MxxK0wu92A78yLSuI9RuQ+SAiw8tbOvF0OfnCtJVefNO 74Yg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=vcNQLYIzpwbguBXgtbHC/rIsr/sBykKa3HDPby/5zJM=; b=Gw9tzfMz9MtTP3dIczsBaihlTx/rkwiYi7wja3zOsK9NO8wcvWWTNghHn0zDcysdvF Us/aH/UXAHH+czqBZ7JRHBpBmVPyGhw5PlPNLo5HdCwZA7St2p89L/A7QOCshoZlWgAW /bSySvD33WlUs0OVObgB2vByMWI4BXdk/BfqDNyrQCXta4k9dZ/VXCn1ApcPDna+qBlO FtJ1GzW6jXTa0pFz2olgyf3+D6LyTBWJkXsALhECbWsYejqUDt7tyJPUbVSFQmLaNMzT hBxzNIscYOtvas8tGS80RRkj0pqyClZmX/cnF7IeWCjj6BtYJP+WcT2GqvuuU1D/Ms4k gO9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80209-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80209-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id c2si5355798plk.369.2017.06.08.14.16.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:00 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80209-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80209-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80209-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=Hn3XulLzdChivDAEtqEPLeJ0PgZozaR O1IpxhLorwOKfQuvxAkJW3mwi6VoDyExt2TZtDr/DogYqPHQI66+Ugz15xxAMZcb u/EuCSQIdDwbm9PG/eIepLMKA3AftQw23NglQLpuRrGwPM6ZP/p/Ii5g+3kK2Yr+ R1LW6BvdZ6ec= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=ixqm5IS/d7e8nJT1UJc2V70FqSw=; b=WtZMO OErGYvOIg5igoGui6QNFxgeUaLb2Lq5kPiJys/3R68ACgmmImPu5t+g2Q4hhOmHz ZbpF4epR2F5s2vXY/5RZeqSSM7YtdKPJvZDQk5qELFNJ3kUcWP2UwbN/IXzkRdSh Mx6lOEi2yd4f7lpzjIVv3uFl3bRHfssthvEnsk= Received: (qmail 39774 invoked by alias); 8 Jun 2017 21:14:10 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 39580 invoked by uid 89); 8 Jun 2017 21:14:08 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=8347 X-HELO: mail-qt0-f175.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=vcNQLYIzpwbguBXgtbHC/rIsr/sBykKa3HDPby/5zJM=; b=t3Eg65z9uldiRgKvTugBxQQXgZT30zTMF6ov6RjPHM/ud6H1qet6vrE67OUtXokN3C NXe9scW34Saj9SMvxcCFZHbAL4y8iPsk7rPmbJ0/7Sb7JRpw9yESmB1Em0h9NC5LqjDC 0It7ZLzrdiUkY6QkBgNfzHsr4+RU9xqWR9dJZ/O/n+gL0KiPMqtxr/nzq6ys+al9ndmd wyfs9coDT1DalF0f+LGcRZwgNoO4UfxqtmgV8pHwLeTGK0qIXmKaltl2wZPgHwKQkxUL 3eeidF/ESvCU19wwgbWUifdkyfkErMs9LmgCQNxR8eEyvHrMHybjoqe+k5yIUpnKCI/A 6mrA== X-Gm-Message-State: AODbwcDGfRrEHvLHIo3JerRSBiHpStBuvgCKWLYbZhCTSkwcLzVuY4ad OotSz2Od6oZZ2+8DLM2/Gg== X-Received: by 10.200.35.122 with SMTP id b55mr17185969qtb.24.1496956442831; Thu, 08 Jun 2017 14:14:02 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 08/17] posix: Use char_array for internal glob dirname Date: Thu, 8 Jun 2017 18:13:22 -0300 Message-Id: <1496956411-25594-9-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This is the first patch of the set to remove alloca usage on glob implementation. Internal path to search for file might expand to a non static directory derived from pattern for some difference cases (GLOB_NOESCAPE, GNU GLOB_TILDE) and to allow a non-static dirname path glob uses a lot of boilerplate code to manage the buffer (which is either allocated using alloca or malloc depending both to size requested and the total alloca_used). The patch changes to use the char_array struct with the default size (256 bytes). It simplifies all the allocation code by using char_array one and every internal buffer access is done using char_array provided functions. No functional changes are expected. Checked on x86_64-linux-gnu. * posix/globc.c (glob): Use char_array for dirname. --- posix/glob.c | 294 ++++++++++++++++++++++++++--------------------------------- 1 file changed, 131 insertions(+), 163 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 425d81b..fbb7aa7 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -90,6 +90,7 @@ #include #include "glob_internal.h" +#include #ifdef _SC_LOGIN_NAME_MAX # define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX) @@ -316,16 +317,15 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), glob_t *pglob) { const char *filename; - char *dirname = NULL; size_t dirlen; int status; size_t oldcount; int meta; - int dirname_modified; - int malloc_dirname = 0; + bool dirname_modified; glob_t dirs; int retval = 0; size_t alloca_used = 0; + struct char_array dirname; if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0) { @@ -333,6 +333,9 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), return -1; } + if (!char_array_init_empty (&dirname)) + return GLOB_NOSPACE; + /* POSIX requires all slashes to be matched. This means that with a trailing slash we must match only directories. */ if (pattern[0] && pattern[strlen (pattern) - 1] == '/') @@ -353,12 +356,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), size_t i; if (pglob->gl_offs >= ~((size_t) 0) / sizeof (char *)) - return GLOB_NOSPACE; + goto err_nospace; pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1) * sizeof (char *)); if (pglob->gl_pathv == NULL) - return GLOB_NOSPACE; + goto err_nospace; for (i = 0; i <= pglob->gl_offs; ++i) pglob->gl_pathv[i] = NULL; @@ -416,7 +419,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), pglob->gl_pathc = 0; pglob->gl_pathv = NULL; } - return GLOB_NOSPACE; + goto err_nospace; } } @@ -432,6 +435,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), illegal_brace: if (__glibc_unlikely (!alloca_onealt)) free (onealt); + char_array_free (&dirname); return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob); } @@ -477,7 +481,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), globfree (pglob); pglob->gl_pathc = 0; } - return result; + retval = result; + goto out; } if (*next == '}') @@ -494,9 +499,10 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (pglob->gl_pathc != firstc) /* We found some entries. */ - return 0; + retval = 0; else if (!(flags & (GLOB_NOCHECK|GLOB_NOMAGIC))) - return GLOB_NOMATCH; + retval = GLOB_NOMATCH; + goto out; } } @@ -512,14 +518,15 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (filename == NULL) filename = strchr (pattern, ':'); #endif /* __MSDOS__ || WINDOWS32 */ - dirname_modified = 0; + dirname_modified = false; if (filename == NULL) { /* This can mean two things: a simple name or "~name". The latter case is nothing but a notation for a directory. */ if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && pattern[0] == '~') { - dirname = (char *) pattern; + if (!char_array_set_str (&dirname, pattern)) + goto err_nospace; dirlen = strlen (pattern); /* Set FILENAME to NULL as a special flag. This is ugly but @@ -537,10 +544,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), filename = pattern; #ifdef _AMIGA - dirname = (char *) ""; +# define CURRENT_FILENAME "" #else - dirname = (char *) "."; +# define CURRENT_FILENAME "." #endif + if (!char_array_set_str (&dirname, CURRENT_FILENAME)) + goto err_nospace; dirlen = 0; } } @@ -549,13 +558,13 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), && (flags & GLOB_NOESCAPE) == 0)) { /* "/pattern" or "\\/pattern". */ - dirname = (char *) "/"; + if (!char_array_set_str (&dirname, "/")) + goto err_nospace; dirlen = 1; ++filename; } else { - char *newp; dirlen = filename - pattern; #if defined __MSDOS__ || defined WINDOWS32 if (*filename == ':' @@ -569,50 +578,48 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* For now, disallow wildcards in the drive spec, to prevent infinite recursion in glob. */ if (__glob_pattern_p (drive_spec, !(flags & GLOB_NOESCAPE))) - return GLOB_NOMATCH; + { + retval = GLOB_NOMATCH; + goto out; + } /* If this is "d:pattern", we need to copy ':' to DIRNAME as well. If it's "d:/pattern", don't remove the slash from "d:/", since "d:" and "d:/" are not the same.*/ } #endif - if (glob_use_alloca (alloca_used, dirlen + 1)) - newp = alloca_account (dirlen + 1, alloca_used); - else - { - newp = malloc (dirlen + 1); - if (newp == NULL) - return GLOB_NOSPACE; - malloc_dirname = 1; - } - *((char *) mempcpy (newp, pattern, dirlen)) = '\0'; - dirname = newp; + if (!char_array_set_str_size (&dirname, pattern, dirlen)) + goto err_nospace; ++filename; if (filename[0] == '\0' #if defined __MSDOS__ || defined WINDOWS32 - && dirname[dirlen - 1] != ':' - && (dirlen < 3 || dirname[dirlen - 2] != ':' - || dirname[dirlen - 1] != '/') + && char_array_pos (&dirname, dirlen - 1) != ':' + && (dirlen < 3 || char_array_pos (&dirname, dirlen - 2) != ':' + || char_array_pos (&dirname, dirlen - 1) != '/') #endif && dirlen > 1) /* "pattern/". Expand "pattern", appending slashes. */ { int orig_flags = flags; int val; - if (!(flags & GLOB_NOESCAPE) && dirname[dirlen - 1] == '\\') + if (!(flags & GLOB_NOESCAPE) + && char_array_pos (&dirname, dirlen - 1) == '\\') { /* "pattern\\/". Remove the final backslash if it hasn't been quoted. */ - char *p = (char *) &dirname[dirlen - 1]; - - while (p > dirname && p[-1] == '\\') --p; - if ((&dirname[dirlen] - p) & 1) + size_t p = dirlen - 1; + while (p > 0 && char_array_pos (&dirname, p - 1) == '\\') --p; + if ((dirlen - p) & 1) { - *(char *) &dirname[--dirlen] = '\0'; + /* Since we are shrinking the array, there is no need to + check the function return. */ + dirlen -= 2; + char_array_crop (&dirname, dirlen); flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC); } } - val = glob (dirname, flags | GLOB_MARK, errfunc, pglob); + val = glob (char_array_str (&dirname), flags | GLOB_MARK, errfunc, + pglob); if (val == 0) pglob->gl_flags = ((pglob->gl_flags & ~GLOB_MARK) | (flags & GLOB_MARK)); @@ -629,11 +636,14 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } } - if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) && dirname[0] == '~') + if ((flags & (GLOB_TILDE|GLOB_TILDE_CHECK)) + && char_array_pos (&dirname, 0) == '~') { - if (dirname[1] == '\0' || dirname[1] == '/' - || (!(flags & GLOB_NOESCAPE) && dirname[1] == '\\' - && (dirname[2] == '\0' || dirname[2] == '/'))) + if (char_array_pos (&dirname, 1) == '\0' + || char_array_pos (&dirname, 1) == '/' + || (!(flags & GLOB_NOESCAPE) && char_array_pos (&dirname, 1) == '\\' + && (char_array_pos (&dirname, 2) == '\0' + || char_array_pos (&dirname, 2) == '/'))) { /* Look up home directory. */ char *home_dir = getenv ("HOME"); @@ -681,10 +691,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { name = malloc (buflen); if (name == NULL) - { - retval = GLOB_NOSPACE; - goto out; - } + goto err_nospace; malloc_name = 1; } @@ -708,10 +715,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), break; } if (!scratch_buffer_grow (&pwtmpbuf)) - { - retval = GLOB_NOSPACE; - goto out; - } + goto err_nospace; __set_errno (save); } # else @@ -724,8 +728,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (home_dir == NULL) { scratch_buffer_free (&pwtmpbuf); - retval = GLOB_NOSPACE; - goto out; + goto err_nospace; } } scratch_buffer_free (&pwtmpbuf); @@ -754,53 +757,26 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), # endif /* WINDOWS32 */ # endif /* Now construct the full directory. */ - if (dirname[1] == '\0') + if (char_array_pos (&dirname, 1) == '\0') { - if (__glibc_unlikely (malloc_dirname)) - free (dirname); - - dirname = home_dir; - dirlen = strlen (dirname); - malloc_dirname = malloc_home_dir; + if (!char_array_set_str (&dirname, home_dir)) + goto err_nospace; + dirlen = char_array_size (&dirname) - 1; } else { - char *newp; - size_t home_len = strlen (home_dir); - int use_alloca = glob_use_alloca (alloca_used, home_len + dirlen); - if (use_alloca) - newp = alloca_account (home_len + dirlen, alloca_used); - else - { - newp = malloc (home_len + dirlen); - if (newp == NULL) - { - if (__glibc_unlikely (malloc_home_dir)) - free (home_dir); - retval = GLOB_NOSPACE; - goto out; - } - } - - mempcpy (mempcpy (newp, home_dir, home_len), - &dirname[1], dirlen); - - if (__glibc_unlikely (malloc_dirname)) - free (dirname); - - dirname = newp; - dirlen += home_len - 1; - malloc_dirname = !use_alloca; - - if (__glibc_unlikely (malloc_home_dir)) - free (home_dir); + /* Replaces '~' by the obtained HOME dir. */ + char_array_erase (&dirname, 0); + if (!char_array_prepend_str (&dirname, home_dir)) + goto err_nospace; } - dirname_modified = 1; + dirname_modified = true; } # if !defined _AMIGA && !defined WINDOWS32 else { - char *end_name = strchr (dirname, '/'); + char *dirnamestr = char_array_at (&dirname, 0); + char *end_name = strchr (dirnamestr, '/'); char *user_name; int malloc_user_name = 0; char *unescape = NULL; @@ -809,23 +785,23 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { if (end_name == NULL) { - unescape = strchr (dirname, '\\'); + unescape = strchr (dirnamestr, '\\'); if (unescape) end_name = strchr (unescape, '\0'); } else - unescape = memchr (dirname, '\\', end_name - dirname); + unescape = memchr (dirnamestr, '\\', end_name - dirnamestr); } if (end_name == NULL) - user_name = dirname + 1; + user_name = dirnamestr + 1; else { char *newp; - if (glob_use_alloca (alloca_used, end_name - dirname)) - newp = alloca_account (end_name - dirname, alloca_used); + if (glob_use_alloca (alloca_used, end_name - dirnamestr)) + newp = alloca_account (end_name - dirnamestr, alloca_used); else { - newp = malloc (end_name - dirname); + newp = malloc (end_name - dirnamestr); if (newp == NULL) { retval = GLOB_NOSPACE; @@ -835,8 +811,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } if (unescape != NULL) { - char *p = mempcpy (newp, dirname + 1, - unescape - dirname - 1); + char *p = mempcpy (newp, dirnamestr + 1, + unescape - dirnamestr - 1); char *q = unescape; while (*q != '\0') { @@ -858,7 +834,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), *p = '\0'; } else - *((char *) mempcpy (newp, dirname + 1, end_name - dirname)) + *((char *) mempcpy (newp, dirnamestr + 1, end_name - dirnamestr)) = '\0'; user_name = newp; } @@ -898,39 +874,25 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* If we found a home directory use this. */ if (p != NULL) { - size_t home_len = strlen (p->pw_dir); - size_t rest_len = end_name == NULL ? 0 : strlen (end_name); - - if (__glibc_unlikely (malloc_dirname)) - free (dirname); - malloc_dirname = 0; - - if (glob_use_alloca (alloca_used, home_len + rest_len + 1)) - dirname = alloca_account (home_len + rest_len + 1, - alloca_used); - else + if (!char_array_set_str (&dirname, p->pw_dir)) { - dirname = malloc (home_len + rest_len + 1); - if (dirname == NULL) - { - scratch_buffer_free (&pwtmpbuf); - retval = GLOB_NOSPACE; - goto out; - } - malloc_dirname = 1; + scratch_buffer_free (&pwtmpbuf); + retval = GLOB_NOSPACE; + goto out; } - *((char *) mempcpy (mempcpy (dirname, p->pw_dir, home_len), - end_name, rest_len)) = '\0'; - dirlen = home_len + rest_len; - dirname_modified = 1; + dirlen = strlen (p->pw_dir); + dirname_modified = true; } else { if (flags & GLOB_TILDE_CHECK) /* We have to regard it as an error if we cannot find the home directory. */ - return GLOB_NOMATCH; + { + retval = GLOB_NOMATCH; + goto out; + } } scratch_buffer_free (&pwtmpbuf); } @@ -948,9 +910,10 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* Return the directory if we don't check for error or if it exists. */ if ((flags & GLOB_NOCHECK) || (((__builtin_expect (flags & GLOB_ALTDIRFUNC, 0)) - ? ((*pglob->gl_lstat) (dirname, &st) == 0 + ? ((*pglob->gl_lstat) (char_array_str (&dirname), &st) == 0 && S_ISDIR (st.st_mode)) - : (__lstat64 (dirname, &st64) == 0 && S_ISDIR (st64.st_mode))))) + : (__lstat64 (char_array_str (&dirname), &st64) == 0 + && S_ISDIR (st64.st_mode))))) { size_t newcount = pglob->gl_pathc + pglob->gl_offs; char **new_gl_pathv; @@ -958,12 +921,10 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (newcount > SIZE_MAX / sizeof (char *) - 2) { nospace: - if (__glibc_unlikely (malloc_dirname)) - free (dirname); free (pglob->gl_pathv); pglob->gl_pathv = NULL; pglob->gl_pathc = 0; - return GLOB_NOSPACE; + goto err_nospace; } new_gl_pathv = realloc (pglob->gl_pathv, @@ -978,33 +939,32 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), pglob->gl_pathv[newcount] = malloc (dirlen + 2); if (pglob->gl_pathv[newcount] == NULL) goto nospace; - p = mempcpy (pglob->gl_pathv[newcount], dirname, dirlen); + p = mempcpy (pglob->gl_pathv[newcount], + char_array_str (&dirname), dirlen); p[0] = '/'; p[1] = '\0'; } else { - if (__glibc_unlikely (malloc_dirname)) - pglob->gl_pathv[newcount] = dirname; - else - { - pglob->gl_pathv[newcount] = strdup (dirname); - if (pglob->gl_pathv[newcount] == NULL) - goto nospace; - } + pglob->gl_pathv[newcount] = strdup (char_array_str (&dirname)); + if (pglob->gl_pathv[newcount] == NULL) + goto nospace; } pglob->gl_pathv[++newcount] = NULL; ++pglob->gl_pathc; pglob->gl_flags = flags; - return 0; + retval = 0; + goto out; } /* Not found. */ - return GLOB_NOMATCH; + retval = GLOB_NOMATCH; + goto out; } - meta = __glob_pattern_type (dirname, !(flags & GLOB_NOESCAPE)); + meta = __glob_pattern_type (char_array_str (&dirname), + !(flags & GLOB_NOESCAPE)); /* meta is 1 if correct glob pattern containing metacharacters. If meta has bit (1 << 2) set, it means there was an unterminated [ which we handle the same, using fnmatch. Broken unterminated @@ -1017,15 +977,15 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), the pattern in each directory found. */ size_t i; - if (!(flags & GLOB_NOESCAPE) && dirlen > 0 && dirname[dirlen - 1] == '\\') + if (!(flags & GLOB_NOESCAPE) && dirlen > 0 + && char_array_pos (&dirname, dirlen - 1) == '\\') { /* "foo\\/bar". Remove the final backslash from dirname if it has not been quoted. */ - char *p = (char *) &dirname[dirlen - 1]; - - while (p > dirname && p[-1] == '\\') --p; - if ((&dirname[dirlen] - p) & 1) - *(char *) &dirname[--dirlen] = '\0'; + size_t p = dirlen - 1; + while (p > 0 && char_array_pos (&dirname, p - 1) == '\\') --p; + if ((dirlen - p) & 1) + char_array_crop (&dirname, --dirlen); } if (__glibc_unlikely ((flags & GLOB_ALTDIRFUNC) != 0)) @@ -1039,7 +999,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), dirs.gl_lstat = pglob->gl_lstat; } - status = glob (dirname, + status = glob (char_array_str (&dirname), ((flags & (GLOB_ERR | GLOB_NOESCAPE | GLOB_ALTDIRFUNC)) | GLOB_NOSORT | GLOB_ONLYDIR), @@ -1047,7 +1007,10 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (status != 0) { if ((flags & GLOB_NOCHECK) == 0 || status != GLOB_NOMATCH) - return status; + { + retval = status; + goto out; + } goto no_matches; } @@ -1085,7 +1048,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), globfree (&dirs); globfree (pglob); pglob->gl_pathc = 0; - return status; + retval = status; + goto out; } /* Stick the directory on the front of each name. */ @@ -1096,7 +1060,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), globfree (&dirs); globfree (pglob); pglob->gl_pathc = 0; - return GLOB_NOSPACE; + goto err_nospace; } } @@ -1118,7 +1082,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { nospace2: globfree (&dirs); - return GLOB_NOSPACE; + goto err_nospace; } new_gl_pathv = realloc (pglob->gl_pathv, @@ -1133,7 +1097,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), globfree (&dirs); globfree (pglob); pglob->gl_pathc = 0; - return GLOB_NOSPACE; + goto err_nospace; } ++pglob->gl_pathc; @@ -1145,7 +1109,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), else { globfree (&dirs); - return GLOB_NOMATCH; + retval = GLOB_NOMATCH; + goto out; } } @@ -1158,7 +1123,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (meta & 2) { - char *p = strchr (dirname, '\\'), *q; + char *p = strchr (char_array_str (&dirname), '\\'), *q; /* We need to unescape the dirname string. It is certainly allocated by alloca, as otherwise filename would be NULL or dirname wouldn't contain backslashes. */ @@ -1175,12 +1140,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), ++q; } while (*p++ != '\0'); - dirname_modified = 1; + dirname_modified = true; } if (dirname_modified) flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC); - status = glob_in_dir (filename, dirname, flags, errfunc, pglob, - alloca_used); + status = glob_in_dir (filename, char_array_str (&dirname), flags, + errfunc, pglob, alloca_used); if (status != 0) { if (status == GLOB_NOMATCH && flags != orig_flags @@ -1191,19 +1156,20 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), flags = orig_flags; goto no_matches; } - return status; + retval = status; + goto out; } if (dirlen > 0) { /* Stick the directory on the front of each name. */ - if (prefix_array (dirname, + if (prefix_array (char_array_str (&dirname), &pglob->gl_pathv[old_pathc + pglob->gl_offs], pglob->gl_pathc - old_pathc)) { globfree (pglob); pglob->gl_pathc = 0; - return GLOB_NOSPACE; + goto err_nospace; } } } @@ -1228,7 +1194,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { globfree (pglob); pglob->gl_pathc = 0; - return GLOB_NOSPACE; + goto err_nospace; } strcpy (&new[len - 2], "/"); pglob->gl_pathv[i] = new; @@ -1244,10 +1210,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } out: - if (__glibc_unlikely (malloc_dirname)) - free (dirname); - + char_array_free (&dirname); return retval; + + err_nospace: + char_array_free (&dirname); + return GLOB_NOSPACE; } #if defined _LIBC && !defined glob libc_hidden_def (glob) From patchwork Thu Jun 8 21:13:23 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103413 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632717qgd; Thu, 8 Jun 2017 14:15:36 -0700 (PDT) X-Received: by 10.99.103.7 with SMTP id b7mr40191001pgc.2.1496956536368; Thu, 08 Jun 2017 14:15:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956536; cv=none; d=google.com; s=arc-20160816; b=D5EN9RMAUTz7f+ooUFRsAnOYi67iEbay1cp1l+9EH5pmr/Hy6dLE6Pp457eX6sQtO6 wL+uyrqJ/25+c3O/9yllkgVvFkge8VoWiVN0jAK1cFCJk2fRNZlRPleZJ7HhGRtrrcnl 5WUrfArwHGKQdekVA7vTDKV87uIVMdcEAoubD68loZRWPQV8/Y6s0GrSe3B21AdC1NVj IBSANJycTgGmDYtMB0PSfnZVi+XlgCxYXG8QaLcCG9Zi/K+MkuNLczo/N0EfQbO3mMIF 4BijDlZe0Mt9KLYtWCMBZCxy7xxvCyA6I4OjCJlXI7OiMwhlnQ3T5uWodWoWBLSIyN2y Jymw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=WAaG+BIGQi8QXi6C1pzSbHUrOHLs5nok0Qy6oeJhEMQ=; b=fpuGoXBYEbLCBLswJniyUZQxV3J1qOM+ui7HJBeRFWwoiINabzmqmMpTp+mhALxzXD Wy6hEcKnoHSmlwt7WA2yx/H91S8RDO9cfW1riYhGcBOHce0H6pa17gfxGRqhUxmrAWXA I+PLoq2FNZGG4N1vjzMuDDmY+HMrN6iHlmJQXmQpM/9rcQX4G4Su790EldaeLlNGaLIJ fLNFSt9E/2h5EYZpeTRiJVJg1rHu6TQ54OhXxDqYrL3C3bibNkOlDFUvmI/IFZlB/a8z Tw9UoIKeKh+rYgSyU4ogxuRC/hiRmsnZDmsiCCAF0/WmmMT/zOO6ocUkevwiIAFvkQ8C V3dQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80207-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80207-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id y81si5343583pfj.43.2017.06.08.14.15.36 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:15:36 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80207-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80207-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80207-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=S8qQYxfz16NNkzgMPO8wsNNvopu5MB3 QcpevozYGnMUlxuC3S+nF4rZMEYlosK9l8/CcSfeZV1e6WssVJ9/B+8QZYTb2V0I +4rNIWO3FE2AGN3PiZLvnuLlezQ27ec2vgcjRxDgSOf6cB5n3zfjER9w68RoS+JZ 6JkRqV2QyIlg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=zpkJDErB/l9GARzKjAz7d45akGE=; b=cX3jp GELh9Uihoeta1rwJZxngKSh1/90BbM4FcM+QdNLkK5lmVT9vQJqkg6C575cJFIRn pV346HiIxSmYY+H2wnk61fIcLwaXvP5bGjQjd6WdnP4dW6o9//F/VIZE4r/05FH0 RkIoL/qCGOcWgTo7ElgUsM8vyTvFvZ3oyTZRUY= Received: (qmail 39323 invoked by alias); 8 Jun 2017 21:14:05 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 39146 invoked by uid 89); 8 Jun 2017 21:14:04 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=accomplish X-HELO: mail-qt0-f181.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=WAaG+BIGQi8QXi6C1pzSbHUrOHLs5nok0Qy6oeJhEMQ=; b=cTTxCnpe094IoM6VNo3GEk8P3EGvWyDJc1En/Ciy4JnHbIc0NNTRK6UaCTnVZBuw8Q U/rcg2Qmo6z0F7PMxQwlR3zYLLnLnPeuUY9+q3QzNjTwELGqr0fjjW+DRpFXkbjAhKji p/YZxqNUkyRsCccArVfQDefJ7iLwhk629JHR5VJ1PIFM9c4IX2BNGkog7mII3l8O6oeb vmkTyr8jZr1qGfGnkBiPk09n3kimMB4dl6G+DYRgHE7GVIWFKYqjODk2pO2SBDKpkA7h 43GqANdmXYT4rjfTbX8aXijPdwMzg41ib3hzBJgHlXRHVg+ykLvSwCO83Y9IWNWeZ/31 7IGQ== X-Gm-Message-State: AKS2vOx9oOBgyzZxY5Z3wTWlCG/hj/TcBG8Niuw9ibScBuzVCXjZblvv 61HbGQuyeu14fiItuDUiaw== X-Received: by 10.55.95.5 with SMTP id t5mr46885253qkb.64.1496956445155; Thu, 08 Jun 2017 14:14:05 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 09/17] posix: Remove glob GET_LOGIN_NAME_MAX usage Date: Thu, 8 Jun 2017 18:13:23 -0300 Message-Id: <1496956411-25594-10-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> Current glob implementation allows non limited user name for home directory construction on GLOB_TILDE case. To accomplish it glob either construct a name on stack if size are small enough (based on current alloca_used) value in heap otherwise. There is no actual login to resize the buffer in case of the resizing the buffer in case of ERANGE, so a static buffer using glibc default LOGIN_NAME_MAX is suffice. Checked on x86_64-linux-gnu. * posix/glob.c (LOGIN_NAME_MAX): Define if not defined. (glob): Use static buffer for user_name on getlogin_r. --- posix/glob.c | 33 +++++---------------------------- 1 file changed, 5 insertions(+), 28 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index fbb7aa7..14a502c 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -92,10 +92,8 @@ #include "glob_internal.h" #include -#ifdef _SC_LOGIN_NAME_MAX -# define GET_LOGIN_NAME_MAX() sysconf (_SC_LOGIN_NAME_MAX) -#else -# define GET_LOGIN_NAME_MAX() (-1) +#ifndef LOGIN_NAME_MAX +# define LOGIN_NAME_MAX 256 #endif static const char *next_brace_sub (const char *begin, int flags) __THROWNL; @@ -677,25 +675,9 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (home_dir == NULL || home_dir[0] == '\0') { int success; - char *name; - int malloc_name = 0; - size_t buflen = GET_LOGIN_NAME_MAX () + 1; - - if (buflen == 0) - /* `sysconf' does not support _SC_LOGIN_NAME_MAX. Try - a moderate value. */ - buflen = 20; - if (glob_use_alloca (alloca_used, buflen)) - name = alloca_account (buflen, alloca_used); - else - { - name = malloc (buflen); - if (name == NULL) - goto err_nospace; - malloc_name = 1; - } + char user_name[LOGIN_NAME_MAX]; - success = __getlogin_r (name, buflen) == 0; + success = __getlogin_r (user_name, sizeof (user_name)) == 0; if (success) { struct passwd *p; @@ -705,7 +687,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), struct scratch_buffer pwtmpbuf; scratch_buffer_init (&pwtmpbuf); - while (getpwnam_r (name, &pwbuf, + while (getpwnam_r (user_name, &pwbuf, pwtmpbuf.data, pwtmpbuf.length, &p) != 0) { @@ -733,11 +715,6 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } scratch_buffer_free (&pwtmpbuf); } - else - { - if (__glibc_unlikely (malloc_name)) - free (name); - } } if (home_dir == NULL || home_dir[0] == '\0') { From patchwork Thu Jun 8 21:13:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103414 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632766qgd; Thu, 8 Jun 2017 14:15:47 -0700 (PDT) X-Received: by 10.99.119.130 with SMTP id s124mr28787141pgc.33.1496956547728; Thu, 08 Jun 2017 14:15:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956547; cv=none; d=google.com; s=arc-20160816; b=zS/+Rp542fxeDMc7mBDNGMRAHZ1YTaD10SnOKGpIqUzK/pIINGW07y/mHr2Cu0sWHb WzxnqEWzoxTKWZmOW5dSsuTXOWNmxdePPlQsWANGkSxirGDapFXavnuzpNycedewMbYQ 4t6YjdO8z/LCdOATvIZ/XCVpReVt8d1thsL4fX3HEi3B2DmxUjcOWIWqYovDQrRanrWU xDCnmkaFsGpVFCh6OuBMuV5DuYSf+WnxIokJArHJfu75DZsLuZNOrrsu2S/NB846onMd 0hf9TtPZCBcRKJwuPQ7nu+qQpDeKyqnkAdbO3PZQvJmZViRCuTkkNOSC99rtM/wGPab1 43Ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=hWLPhNGA1OphlblzJZTf7Ncaug5wYh6ew27rVHeNLWc=; b=rTYdZYNAT0KlYqer2ihLnV+Udvm9WhGPQTVmOGrmXafgpZ46W6Gr/zwwf8lnAm7tfg 71c/CDRc7DvbDP+ZPb2itqbonz9zWsSSxqdF4S9tBCwDX4+H/YYJnORBovs3uzmGUxyS np+t1PUENWG9NzASTxMsyy/bQjH0CVUVOgZYVePLe6/QynOJzR0rxW9ty97g/QTo5knd vEKZQKQJ+TvY74FHBufDpQLn5Du7lESUwXw0Xsy3TldkXAJrR0rAnE6vmkp5FEychATV 28ds+mbf3UQLBFWTupD0Nbl2Zg0TFB+4JVatqlew5O5ILZqd9RcKq9DPCjO2caTBLlmB 4q1A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80208-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80208-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id 31si5168333ply.191.2017.06.08.14.15.47 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:15:47 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80208-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80208-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80208-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=u+dZ7Sl724SyNaUSWh4CbT43WN3rNTb SQq3MABymyISaqRXbT/BjTJJYKz4rNIOUzTCrpJtt0S52Z2/3yQK9dRQI2Ow+s8v s0SeLa38F69WUGQe5i4MzcrrqOoOb9sMZZbnRLtwtUCilwmKpVQ7xJuVsvP4J1QT f+3DkpOPuARE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=My+C6GlA4pGmdiMqIaicPu6K6jo=; b=E71gD aW8qooyTH3ETQ/zsCr7/k5FJpQFidYGrL4tMOJ5UjQ56RYSRi8yGoJhwDZGJ2nAa UqLiGAW7nJYUreGcGNOTEXGYhr1Z8qgFirVRddFaJv95KTzWTnwpj2AyNLnjIj7e na8zbEgUt7HfHWPHFpnwXsFvoaXoq1WhUQhkLI= Received: (qmail 39712 invoked by alias); 8 Jun 2017 21:14:09 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 39522 invoked by uid 89); 8 Jun 2017 21:14:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f173.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=hWLPhNGA1OphlblzJZTf7Ncaug5wYh6ew27rVHeNLWc=; b=k5S/K/KHYtowqqnMQGqzMedm5OG+KcS4UPTObN3/mnXSkW0tZAFMGooh3JzEmLiPB/ udffOdAAxaGxyL/cAjuSX5+fAGd+M16iI1+wptxrpCkap8ynscbGux4rOLTusp81KNwV ok/Js580Sq7gPD9Dud3g+ZHO+rqo2UWPglYqTZ+L9eHMu+IwwQzK0xzn9IvAzDQ+YG/8 kL4JhUwP5db64tlU9PyhZ9klcjAX6q8yYFbnlSDuLkSnj9HlIATVWKZjajRVZFgkv21C hH+g4UDbDdEBcFuaEMKoLyHOy4JR0+8TiUc0bZXSThBCdueVuabDL7A7xXWGH6rlkl5Q j/iQ== X-Gm-Message-State: AODbwcCaqvrG0m3y6rfbkO/0AFvonvhCefUbMz5/0n/JIPqjoV78OUZy 3DU1lvZpnFayPAM7iEpuKw== X-Received: by 10.237.37.74 with SMTP id w10mr42161408qtc.14.1496956447525; Thu, 08 Jun 2017 14:14:07 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 10/17] posix: User LOGIN_NAME_MAX for all user name in glob Date: Thu, 8 Jun 2017 18:13:24 -0300 Message-Id: <1496956411-25594-11-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch limits all user name obtained for GLOB_TILDE to max of LOGIN_NAME_MAX (256 on glibc) and remove all stack/malloc buffer handling boilerplate. Checked on x86_64-linux-gnu. * posix/glob.c (glob): Remove alloca usage on user_name for GLOB_TILDE. --- posix/glob.c | 35 +++++++++++------------------------ 1 file changed, 11 insertions(+), 24 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 14a502c..9e3050a 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -754,8 +754,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { char *dirnamestr = char_array_at (&dirname, 0); char *end_name = strchr (dirnamestr, '/'); - char *user_name; - int malloc_user_name = 0; + char user_name[LOGIN_NAME_MAX]; char *unescape = NULL; if (!(flags & GLOB_NOESCAPE)) @@ -770,26 +769,14 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), unescape = memchr (dirnamestr, '\\', end_name - dirnamestr); } if (end_name == NULL) - user_name = dirnamestr + 1; + strncpy (user_name, dirnamestr + 1, LOGIN_NAME_MAX - 1); else { - char *newp; - if (glob_use_alloca (alloca_used, end_name - dirnamestr)) - newp = alloca_account (end_name - dirnamestr, alloca_used); - else - { - newp = malloc (end_name - dirnamestr); - if (newp == NULL) - { - retval = GLOB_NOSPACE; - goto out; - } - malloc_user_name = 1; - } if (unescape != NULL) { - char *p = mempcpy (newp, dirnamestr + 1, - unescape - dirnamestr - 1); + ptrdiff_t name_len = unescape - dirnamestr - 1; + name_len = MIN (name_len, LOGIN_NAME_MAX - 1); + char *p = mempcpy (user_name, dirnamestr + 1, name_len); char *q = unescape; while (*q != '\0') { @@ -811,9 +798,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), *p = '\0'; } else - *((char *) mempcpy (newp, dirnamestr + 1, end_name - dirnamestr)) - = '\0'; - user_name = newp; + { + ptrdiff_t name_len = end_name - dirnamestr; + name_len = MIN (name_len, LOGIN_NAME_MAX - 1); + *((char *) mempcpy (user_name, dirnamestr + 1, name_len)) + = '\0'; + } } /* Look up specific user's home directory. */ @@ -845,9 +835,6 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), p = getpwnam (user_name); # endif - if (__glibc_unlikely (malloc_user_name)) - free (user_name); - /* If we found a home directory use this. */ if (p != NULL) { From patchwork Thu Jun 8 21:13:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103416 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632908qgd; Thu, 8 Jun 2017 14:16:09 -0700 (PDT) X-Received: by 10.98.63.10 with SMTP id m10mr28301220pfa.19.1496956569202; Thu, 08 Jun 2017 14:16:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956569; cv=none; d=google.com; s=arc-20160816; b=cQ4/zU8l908i4angP5ZGIDSBAmS6O/Df3cO/EQiJ5ajYBVCKLTh7sTlA5Jeqb5djtG Txzvxs53Eh1VtnqzxLAXZU7d8A/SuggEVfUhFGgCSTwWL//J2gozrkB/8oRuiRBOeg8k HOscz1DWRl1ncCYh02vMZx6Ao79Ox4zJ+GQ8vZGr6QZYvG1yxvcC0z2sOHRdVGrdfdXa YBo/yXndt/HNbdWTd/jQt/zUHSRKZFQ8j4XXW9+HNSZwXd5PkoNTg8FTFkeRovin9L/C u5LKtOKGFRRZBdeXP3M3l4bFGWDRxSLDxPwxHvCWcNQoXYG3YzHeAKYzR3qJDKqDIBc5 CVog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=9PUnokyi+MFN4VfrfpWTAGJSbghDlfuO3gqqLmOhmAA=; b=oTbzcvyszgZYDa+Z57MA75G6XckGR2rWC21qHP9RKAoBE9ud3gFgZBPGExC46L0I7H Z4WfQ39MtEv6f0zv558K1jp/0vEbtwaqHtB17iN1L/rN8tcEdO3H5SKb8dg33QqVCIm/ SxfIkSSMSIG+4GYuh9iC+zN3wbt7NdMuRGGUuujbL80YNC1kV7mOUfoS2f7qZFPLF36S Nkij27p6Ml3+zm3mNcCW1+aAlxGDRf2DUJg5VBAw8pORlqyik/PSBELUvUBLh9toEodc YhxEZfTXTrgYCxvQlp0adx+Gb99Gb+U4KkFVeJoKRQ0Qfgy4mEDTlixxBgIygbymk2zG iRXg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80210-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80210-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id l1si5131592plb.129.2017.06.08.14.16.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:09 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80210-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80210-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80210-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=fAcuv396v092LpyexZxeAQaLIc/F+G0 ZN2gpnQ0nrtk/RzrxpPd4cCaiQTD1Z65MpKRGVOspweBBmnMaJrusoACt2RIR9Dx u/LQlcQh6LG334e7nkG7tRtbLq4b6LCyBTe+WdBHsRBjI/9yHiNsFslSaSOOCQyr smew+KezRWj0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=IKYxUdg5wAA/EnLRnpu5Hda712c=; b=YeLvG gVOrOwhNUMLcnVYA8XyGSXUb3LrVFRu1yW2q/PXcoZSGmaScIMG5/Qj/M4KqEpQ4 4vTKYv/oHmk4OGOr652i3IHfyEy9xm9VjFZ8O+pOA8CI9VVQi1rt3OcvYWhSxgMV Y/QNoBgw7ZmfxLzl9eZYrQ+1Is3PQEgHbKwnGE= Received: (qmail 39886 invoked by alias); 8 Jun 2017 21:14:10 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 39722 invoked by uid 89); 8 Jun 2017 21:14:09 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f181.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=9PUnokyi+MFN4VfrfpWTAGJSbghDlfuO3gqqLmOhmAA=; b=Xsi+ApOpCUIXbpekIHvUGgzSLlUBB4zIo7TfBpi9uEieiuNFw/jGTfNVzWxhAh8SL9 kjegCOjjlye0KBULeoHa1sz6mwEDcoqtXO5xbI1NH/8NoniCgroaiVjFWvJs57sJWefP SccgK71I4aSPnLBpGhXVBJlCYheaadIUisfUkAaglDj8RwyPDed7IrakE3BX1fkAVGNr eE2VmEL8+WJ9UGFR13LCRAiLcEVMYnkQgWfGeIYeibWViP8wsGQMkWD5k5tZPCj9V5v4 hyllQCN/0HMZLyHlQQZA9K8mLs5zE8jA/Pk4EJbJnYPqTpOVSb8SZt2v8VdAb6Tji7qB GjAQ== X-Gm-Message-State: AODbwcDnrPbU1HndTx4mLwM4VvYb5MhLP4jtXJKhqvzceItW4hvmkuA9 N7dG7HqsMKFxiQeN6Wp9kw== X-Received: by 10.237.44.7 with SMTP id f7mr21278321qtd.142.1496956449835; Thu, 08 Jun 2017 14:14:09 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 11/17] posix: Remove alloca usage for GLOB_BRACE on glob Date: Thu, 8 Jun 2017 18:13:25 -0300 Message-Id: <1496956411-25594-12-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> GNU GLOB_BRACE internal implementation constructs a new expression and calls glob recursively. It then requires a possible large temporary buffer place the new pattern. This patch removes the alloca/malloc usage and replaces it with char_array. Checked on x86_64-linux-gnu. * posix/glob.c (glob): Remove alloca usage for onealt. --- posix/glob.c | 63 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 31 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 9e3050a..c83feb4 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -397,44 +397,32 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* Allocate working buffer large enough for our work. Note that we have at least an opening and closing brace. */ size_t firstc; - char *alt_start; const char *p; const char *next; const char *rest; size_t rest_len; - char *onealt; - size_t pattern_len = strlen (pattern) - 1; - int alloca_onealt = glob_use_alloca (alloca_used, pattern_len); - if (alloca_onealt) - onealt = alloca_account (pattern_len, alloca_used); - else + struct char_array onealt; + + /* We know the prefix for all sub-patterns. */ + ptrdiff_t onealtlen = begin - pattern; + if (!char_array_init_str_size (&onealt, pattern, onealtlen)) { - onealt = malloc (pattern_len); - if (onealt == NULL) + if (!(flags & GLOB_APPEND)) { - if (!(flags & GLOB_APPEND)) - { - pglob->gl_pathc = 0; - pglob->gl_pathv = NULL; - } - goto err_nospace; + pglob->gl_pathc = 0; + pglob->gl_pathv = NULL; } + goto err_nospace; } - /* We know the prefix for all sub-patterns. */ - alt_start = mempcpy (onealt, pattern, begin - pattern); - /* Find the first sub-pattern and at the same time find the rest after the closing brace. */ next = next_brace_sub (begin + 1, flags); if (next == NULL) { /* It is an invalid expression. */ - illegal_brace: - if (__glibc_unlikely (!alloca_onealt)) - free (onealt); - char_array_free (&dirname); - return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob); + char_array_free (&onealt); + goto illegal_brace; } /* Now find the end of the whole brace expression. */ @@ -443,8 +431,11 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { rest = next_brace_sub (rest + 1, flags); if (rest == NULL) - /* It is an illegal expression. */ - goto illegal_brace; + { + /* It is an illegal expression. */ + char_array_free (&onealt); + goto illegal_brace; + } } /* Please note that we now can be sure the brace expression is well-formed. */ @@ -463,17 +454,24 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), int result; /* Construct the new glob expression. */ - mempcpy (mempcpy (alt_start, p, next - p), rest, rest_len); + ptrdiff_t nextlen = next - p; + if (!char_array_replace_str_pos (&onealt, onealtlen, p, nextlen) + || !char_array_replace_str_pos (&onealt, onealtlen + nextlen, + rest, rest_len)) + { + char_array_free (&onealt); + retval = GLOB_NOSPACE; + goto out; + } - result = glob (onealt, + result = glob (char_array_str (&onealt), ((flags & ~(GLOB_NOCHECK | GLOB_NOMAGIC)) | GLOB_APPEND), errfunc, pglob); /* If we got an error, return it. */ if (result && result != GLOB_NOMATCH) { - if (__glibc_unlikely (!alloca_onealt)) - free (onealt); + char_array_free (&onealt); if (!(flags & GLOB_APPEND)) { globfree (pglob); @@ -492,8 +490,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), assert (next != NULL); } - if (__glibc_unlikely (!alloca_onealt)) - free (onealt); + char_array_free (&onealt); if (pglob->gl_pathc != firstc) /* We found some entries. */ @@ -1180,6 +1177,10 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), err_nospace: char_array_free (&dirname); return GLOB_NOSPACE; + + illegal_brace: + char_array_free (&dirname); + return glob (pattern, flags & ~GLOB_BRACE, errfunc, pglob); } #if defined _LIBC && !defined glob libc_hidden_def (glob) From patchwork Thu Jun 8 21:13:26 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103417 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2632962qgd; Thu, 8 Jun 2017 14:16:18 -0700 (PDT) X-Received: by 10.99.185.67 with SMTP id v3mr9690143pgo.189.1496956577954; Thu, 08 Jun 2017 14:16:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956577; cv=none; d=google.com; s=arc-20160816; b=cGE4N39bLcL/GWNSVahnv/0Z8PhrdBXyrmR6lKOpLWQ/5G3NfEM9zWmmmNCEqJ/SBS WDokp+fITsmttyyp+zv9oY4lQwnUamNHA3nI1/srh57ZbEKpUiyPuwLrIQoruEwVoqmX MvWPdAwIpcr6CscaYDGLP816Nr/w/G5E4yPi7GOgpEUxBirkepZPurTst+NC0Ng9N5GM tXqe3gMophXqMzzCtzoqoAg0HUZ83Cfk/EjATk71FY1X4NV05iMT+FMtuls5BZTFuHOb hevJ4kZ1ImOnf266xkLjxskF+VmxTw4lnbFZIwU4otLx58M9vJgX8jWhCdgJlh/X1GkT ll6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=Sy7eyYDM8fz4qIIIXSj9U7+Ulqz3q0JJCIhQmgt1ZBU=; b=riukcjxha/qeFft7ZH7PdNJhlnIMrjtWX7U4BGJFyLfBToMWQZ6jlTeTHYmfgp5zTX buIWNR9Pv6H+h7r5hIENSch3f9jvNQCmNSecy0AbdFdYi9fLSR1SH1/OMWtif/KGZfsi hSypqvlDjkI9KVKtDKGle/3khKBhC/Q096Xi2zxzgCO28hO6LXS+Gvlhwm70/La7uhIj NvJnQiw06BNJp4kWsbASSTJK+OxS0RhiZgcv5O+jddScKDKcBzu6pCYYuEkpRnMlYPi2 69T/F+wDfDReDCCeFoytEE2TTG7GYzHmSldAMz0bRaDmqkgFfK6PZuodCJ7ZmyVpTZcx vdjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80211-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80211-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id x4si5389735pfd.40.2017.06.08.14.16.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:17 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80211-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80211-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80211-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=bOnR4AtQFQbsCVW2lqm3zW5M69MetS0 EabEaaUe7q6yJJnwMwH6tUO1xDUNSkyZdC0RwZ/lx/MExTWC9fnam7ngJF3HhQTC SmE2DkgL/TqV+c24Lb8dh3Bjugy5bxsUz+SXf8scIz+Oh+GZ49gbyJ/84jwdl57U +mg2LqC6zP+A= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=ZEtDywxSm8AWlXGr8qhHc1oWsl4=; b=OoGuV xf0cW8+98fCvxek13YdBsXfsksTHYwRqzFOs2D8Sum+41yeZXBvXhV4KWqE1Likw 0SOWmAhnrUn6+3b3++4S42qjYJquahv/76gpXYP1TlxAnExtFIJI0Y0e2ejCqT01 VquAO42vhJ+ryc8El2FNNyAZGA+lCE28JdV44I= Received: (qmail 40133 invoked by alias); 8 Jun 2017 21:14:12 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 40022 invoked by uid 89); 8 Jun 2017 21:14:11 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f181.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=Sy7eyYDM8fz4qIIIXSj9U7+Ulqz3q0JJCIhQmgt1ZBU=; b=iau0Ixhe56uFTivuh8SrIg7oy26JI2C7f9/W3qTs858rtZLJBO5TyigRRoGNaHWUaa Dz4aRiuqieRn7Du6q3+1r3aCNRnU6wugmUrgJpg9vRUTiwyTgqG8khf9hDlvjtlkxlM2 z7jDgy6J6uUSbOKA5cxNhMGuTsSkQDbKip4tNB26RoWAgPl41GaUErsInBgWSszcXbBL CJAB44XSXayBgQceo/CoSbzABtU4K9l3aQxEmO4IFgSsUzLlZzyeTy3/+qX0us6WuRiw shx3XlIVvdXK+62Spz6RxlAu2mBZltb5wfYzweyXsBPRoV06WoTZmxnpaQGTUGTPkAxo AFtw== X-Gm-Message-State: AODbwcBkLxsyOeeYkaQmUPv0P3myUd1e0eXMSbQ9yA8T+suvhSZW6yte PcKcP6BLv8H+r+sF/O6kMg== X-Received: by 10.55.40.66 with SMTP id o63mr41957488qkh.133.1496956452155; Thu, 08 Jun 2017 14:14:12 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 12/17] posix: Remove alloca usage on glob dirname Date: Thu, 8 Jun 2017 18:13:26 -0300 Message-Id: <1496956411-25594-13-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> 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. --- posix/glob.c | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index c83feb4..beeb639 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -1272,7 +1272,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 { @@ -1312,33 +1311,24 @@ 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_lstat) (fullname, &ust.st) - : __lstat64 (fullname, &ust.st64)) == 0) + ? (*pglob->gl_lstat) (char_array_str (&fullname), &ust.st) + : __lstat64 (char_array_str (&fullname), &ust.st64)) == 0) /* 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 { From patchwork Thu Jun 8 21:13:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103418 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2633012qgd; Thu, 8 Jun 2017 14:16:28 -0700 (PDT) X-Received: by 10.84.205.70 with SMTP id o6mr23139998plh.32.1496956588587; Thu, 08 Jun 2017 14:16:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956588; cv=none; d=google.com; s=arc-20160816; b=zgQNAvtqtfOe5+hOvuRSzNFZFfnrJalcXnWUgkJt/1gziPIrwgmpSZdCapQAVKwZSK bFgUGt1PC7hQAd8NNpIgeV9bTa6Rg92Ne4a7nOu0LRaXKRW1ap/OIt0Ym1f7kk8IzWki +iLXgTWGQVXEztBh2FTL84XO8cbsOceo1cpiZ4a+fS+0BAXvbRvLm9sdTfVXQE4rDWqy PC4GiGUFw/Jd4KAvgkhY2xyrRLuq+QGXZPsBhgubfdYZzdLVOzYsJrWw9YgICUgiLco+ JXmo9/xgGPfTiJoyIAJUOw5OqWotz9CX9n2LFf5pi4GqhNrSOYMPIQSUjN2TKrEAsjMO umZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=2Lo4DSyvvsyY+OwpETP3t8B45pvImUsuolI/b5oYDlM=; b=p0Egs3pYyqbHjuZNrlwlUtAsgvEv6d0fMYmTa3Wz5jTK36xZnt+TbBNcQ2sqdvtXcX MqS/6sTx92qTpY4qcr/JcIXHSJnDmpcC0Xct16UYdDr8bgo0LfOCrrNfrx+/jKm3kge0 iqR/olCwd87tYKX2KNO/3qbQAyj9ZawFcJxWULvyAMQePnF5g3iY+Q4N6X54WXsd7r5/ 0iRu7W96D8fvCC4He7yi626npcxMryxBspYXhjgrKKOsN0GkGYMUI6lI8qlaxA9JKUyy FYVDszZ66clH5m9AK4Yi7YIxiWHAmWpnWbWiITNIc7ndDcfB1yNoxVBNPSN7BG+mNt3+ t8Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80212-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80212-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id e186si5153396pgc.163.2017.06.08.14.16.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:28 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80212-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80212-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80212-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=P7ujHQQXdUJLe/gYiOMXY6IaxOYNFxi PY5tRlnw7FVf36csr9dWhLwWAnhImkRR7em3yP0AE3AWpB25Oz4OAKSij92OEPHi e6c27yCEr/alQ6cwOrLZ035wb+ImbHUaBC/n0Qmuo0m6rEJESPBA5MQbKIoplWqr rOCjGNbp6iL8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=WdD3TZ/gke9t20uBCxA5PmicZvk=; b=bg/wE 2HM7kBtHaC5zEJhLLjNWi0SVzl6EG4MTp7WKbZiawvNy+HKyjiVD1F8p44qdb+pp VKZjS5YqeS6GX/xifjvxaezTAE3/3cJKFeaRGXTSah00xeSiebG/jyoRrchmEjh/ vP51CVybYJOMw61wuejZiEqAPp5SsiF6esb5DA= Received: (qmail 40420 invoked by alias); 8 Jun 2017 21:14:15 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 40355 invoked by uid 89); 8 Jun 2017 21:14:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f181.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=2Lo4DSyvvsyY+OwpETP3t8B45pvImUsuolI/b5oYDlM=; b=Q5ifOJB4k1Xt8Fa35Etqei2wwa7GlidlL9rFi9dy06+YC8F7EgemCjotFANB9birc6 Ctg2seczw2HwgeAbFs2yLqWD2d/Ez8PuYqOd1KDYj4U06O7jZOUpIKAvvaIUFg6qC3cJ ggb2vqnEO9t8yn8l2+/NK73R0LmFZYZEEFSPrOJbapylsTPWoKtFe6tSnnQFazHUMHP7 u9sw+eFng9nUyHTLP1vVoiNkUpF85+zFQFujk2tAcabLIEaG7t1CdOP6sHI4EDT57Xhg GH90YjEvF0z0l9zmQCrJWJT3dUpitgWTMmK0cPJOGldUF2blgI2TGk4oRtQ7f6OkvNDs gzuQ== X-Gm-Message-State: AODbwcB8/TC4qXr6UERH9HH39tU9u51ALxthkGQHS9vc03EOifR7q9jO y1tCumVs4CnIdq/oTZXxcg== X-Received: by 10.200.45.83 with SMTP id o19mr39644424qta.43.1496956454478; Thu, 08 Jun 2017 14:14:14 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 13/17] posix: Use dynarray for globname in glob Date: Thu, 8 Jun 2017 18:13:27 -0300 Message-Id: <1496956411-25594-14-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch uses dynarray at glob internal glob_in_dir function to manage the various matched patterns. It simplify and removes all the boilerplate buffer managements required. It also removes the glob_use_alloca, since it is not used anymore. Checked on x86_64-linux-gnu. * posix/glob.c (glob_use_alloca): Remove. (glob_in_dir): Use dynarray for globnames. --- posix/glob.c | 155 +++++++++++++++-------------------------------------------- 1 file changed, 38 insertions(+), 117 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index beeb639..62be373 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -237,33 +237,6 @@ convert_dirent64 (const struct dirent64 *source) ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0) #endif -/* Set *R = A + B. Return true if the answer is mathematically - incorrect due to overflow; in this case, *R is the low order - bits of the correct answer.. */ - -static bool size_add_wrapv (size_t a, size_t b, size_t *r); -static bool glob_use_alloca (size_t alloca_used, size_t len); - -/* We must not compile this function twice. */ -static bool -size_add_wrapv (size_t a, size_t b, size_t *r) -{ -#if 5 <= __GNUC__ - return __builtin_add_overflow (a, b, r); -#else - *r = a + b; - return *r < a; -#endif -} - -static bool -glob_use_alloca (size_t alloca_used, size_t len) -{ - size_t size; - return (!size_add_wrapv (alloca_used, len, &size) - && __libc_use_alloca (size)); -} - static int glob_in_dir (const char *pattern, const char *directory, int flags, int (*errfunc) (const char *, int), glob_t *pglob, size_t alloca_used); @@ -1262,6 +1235,20 @@ prefix_array (const char *dirname, char **array, size_t n) return 0; } +struct globnames_result +{ + char **names; + size_t length; +}; + +/* Create a dynamic array for C string representing the glob name found. */ +#define DYNARRAY_STRUCT globnames_array +#define DYNARRAY_ELEMENT_FREE(ptr) free (*ptr) +#define DYNARRAY_ELEMENT char * +#define DYNARRAY_PREFIX globnames_array_ +#define DYNARRAY_FINAL_TYPE struct globnames_result +#define DYNARRAY_INITIAL_SIZE 64 +#include /* Like `glob', but PATTERN is a final pathname component, and matches are searched for in DIRECTORY. @@ -1273,26 +1260,13 @@ glob_in_dir (const char *pattern, const char *directory, int flags, glob_t *pglob, size_t alloca_used) { void *stream = NULL; - struct globnames - { - struct globnames *next; - size_t count; - char *name[64]; - }; -#define INITIAL_COUNT sizeof (init_names.name) / sizeof (init_names.name[0]) - struct globnames init_names; - struct globnames *names = &init_names; - struct globnames *names_alloca = &init_names; + struct globnames_array globnames; size_t nfound = 0; - size_t cur = 0; int meta; int save; int result; - alloca_used += sizeof (init_names); - - init_names.next = NULL; - init_names.count = INITIAL_COUNT; + globnames_array_init (&globnames); meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE)); if (meta == 0 && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC))) @@ -1379,30 +1353,10 @@ glob_in_dir (const char *pattern, const char *directory, int flags, if (fnmatch (pattern, d.name, fnm_flags) == 0) { - if (cur == names->count) - { - struct globnames *newnames; - size_t count = names->count * 2; - size_t size = (sizeof (struct globnames) - + ((count - INITIAL_COUNT) - * sizeof (char *))); - if (glob_use_alloca (alloca_used, size)) - newnames = names_alloca - = alloca_account (size, alloca_used); - else if ((newnames = malloc (size)) == NULL) - goto memory_error; - newnames->count = count; - newnames->next = names; - names = newnames; - cur = 0; - } - names->name[cur] = strdup (d.name); - if (names->name[cur] == NULL) - goto memory_error; - ++cur; - ++nfound; - if (SIZE_MAX - pglob->gl_offs <= nfound) - goto memory_error; + globnames_array_add (&globnames, strdup (d.name)); + if (globnames_array_has_failed (&globnames)) + goto memory_error; + nfound++; } } } @@ -1412,10 +1366,13 @@ glob_in_dir (const char *pattern, const char *directory, int flags, { size_t len = strlen (pattern); nfound = 1; - names->name[cur] = malloc (len + 1); - if (names->name[cur] == NULL) + char *newp = malloc (len + 1); + if (newp == NULL) + goto memory_error; + *((char *) mempcpy (newp, pattern, len)) = '\0'; + globnames_array_add (&globnames, newp); + if (globnames_array_has_failed (&globnames)) goto memory_error; - *((char *) mempcpy (names->name[cur++], pattern, len)) = '\0'; } result = GLOB_NOMATCH; @@ -1436,61 +1393,25 @@ glob_in_dir (const char *pattern, const char *directory, int flags, if (new_gl_pathv == NULL) { memory_error: - while (1) - { - struct globnames *old = names; - size_t i; - for (i = 0; i < cur; ++i) - free (names->name[i]); - names = names->next; - /* NB: we will not leak memory here if we exit without - freeing the current block assigned to OLD. At least - the very first block is always allocated on the stack - and this is the block assigned to OLD here. */ - if (names == NULL) - { - assert (old == &init_names); - break; - } - cur = names->count; - if (old == names_alloca) - names_alloca = names; - else - free (old); - } + globnames_array_free (&globnames); result = GLOB_NOSPACE; } else { - while (1) + struct globnames_result ret = { .names = 0, .length = -1 }; + if (!globnames_array_finalize (&globnames, &ret)) + result = GLOB_NOSPACE; + else { - struct globnames *old = names; - size_t i; - for (i = 0; i < cur; ++i) + for (size_t i = 0; i < ret.length; ++i) new_gl_pathv[pglob->gl_offs + pglob->gl_pathc++] - = names->name[i]; - names = names->next; - /* NB: we will not leak memory here if we exit without - freeing the current block assigned to OLD. At least - the very first block is always allocated on the stack - and this is the block assigned to OLD here. */ - if (names == NULL) - { - assert (old == &init_names); - break; - } - cur = names->count; - if (old == names_alloca) - names_alloca = names; - else - free (old); - } - - pglob->gl_pathv = new_gl_pathv; + = ret.names[i]; - pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; - - pglob->gl_flags = flags; + pglob->gl_pathv = new_gl_pathv; + pglob->gl_pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; + pglob->gl_flags = flags; + } + free (ret.names); } } From patchwork Thu Jun 8 21:13:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103419 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2633071qgd; Thu, 8 Jun 2017 14:16:38 -0700 (PDT) X-Received: by 10.98.27.215 with SMTP id b206mr5092466pfb.123.1496956598431; Thu, 08 Jun 2017 14:16:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956598; cv=none; d=google.com; s=arc-20160816; b=qKmfg4+JVPf+qVL4pPzO3IXk4qhnukCRs8q8zM/huPA9kGYOeKVl438+QZDGL+MOHv Ls5wmczph84M73fyAHSc7tEHKWn7OZM04raqeUciJ/LF3ex0IUDRFMU/vldgziYZdS3/ yHqeIU5+VfZXPOblMuzOt2tCZV9k0jH0/YHxlMt1Mi1Mia5hrAHJoq3EXtMzKhx9AzEp rjPhgQ34dTsTrhn0wQ946LQN2HHD6uOOr2oDntuQb+4XwjEneZwrJ6NxDzoWvfMvRvcW 8l7mCA5qUmiA/b2YgtbJu1Thgma2l7cyAp2s7Xc1pfb9c4I5uvk5+FEcmZbjnxO0ahwv NlYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=UeGAaHlQ3G8+ljnzbv9JSJ5wAho2dk7MQtmFARw9L4w=; b=Pso4HR39sts0LoXau2U5EHfILI4smm/KZvBZhRWfpjDcF9iGcJmPVY1Y93OqNK9RjK 8hEITHaRUcAvRzOElhVMQNth3YzNxNnuyhzsCvZ96gBTX0tLC/I0QIta8bN9NS2K8AYJ bN5gOu/k5rVs6zoJBVxJ1aV1u9vBBlSA4iEM1NSjWIpvlT+NCk7zZXeabkLCzMKUMW7A a+UiT0rVy0KHS3Gj+eAspj3YnfIuUy9lklNLWUn4gEWXNFoNYxFw9GT626Arl4tyobpY D1S/fU0BV9VM4xTFv6rtax7lPOOXbnM/mQ+I4KfBhtWGsL8WK1Cl4uR2cyZKV91xM9mS naQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80213-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80213-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id m9si5158188pln.442.2017.06.08.14.16.38 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:38 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80213-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80213-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80213-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=SJ0IP92+1caGZQmOsAxAatup33JRpcx SrzCnjEC4kNmabAriYskDthX2yO9/HC6el3R4EWggXou+NoX1bhmUvq2ojt/JNwE 8ofRAg2S0kz0ty9g/IOp9lhzM8Gh4NP87StdpWi5xeuvVWXK81TNVZ780gJhGL/0 sOHOUfyj08l8= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=cccF/v4VnWmFLbkFNagd8d6u300=; b=QsbMm B/nPg2vLsBNrNCdLhgiv0VrMoMFV5fyqdpfq6/Zr+0Bf/+YsHTUSULfOqbUrFg+X 8oV8NHAwGth63bkqGdS0C5O4OqU0d/6i4mxFYHrsmFMgvd42+dDsPUdDorTE2WhF s31uGgUaE0Ae3kXAP9iAJRIxuY5VkkWs3UGDFc= Received: (qmail 40757 invoked by alias); 8 Jun 2017 21:14:18 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 40536 invoked by uid 89); 8 Jun 2017 21:14:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f177.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=UeGAaHlQ3G8+ljnzbv9JSJ5wAho2dk7MQtmFARw9L4w=; b=oNXkNKP+N4ERUPZSV3EVyTpLgWkHUkHqzo5mLpjJJK27KOLjkRL6GSPhy+aGVxtEXp AS1CUp0xkP4d6wmMVBDhk6w7kMZ9f/jeRztmxqjZEi6xX4PKFMVizlWorrdAB6Xa+HgW mTeyQkiUWFSRKQy3QQdOCVMdNHxOA7oSLnzJFcEKwcfAAblidhuwdXYSOiHK9FyMEEsE e19vC+sSGNoROUve3yupi/LYf+JOqITlBCrrHhi+m7rqEWCJvOaYkQMYLSqwTqVqkU77 JAeAcYs8Wgl4xAJBM9y24jyo9Wir7fjiFtj8/hoVs9I70gz1kPmnA1f+qn3cIF7JnfXR pRTQ== X-Gm-Message-State: AKS2vOyDp2gmCm1QKK0ejN1VmsBHjN0EDEXglQKX2NBy4uBFCELAT0zi 1I9MESs5D4jlWz97VcQ9ig== X-Received: by 10.55.92.68 with SMTP id q65mr30392566qkb.205.1496956456790; Thu, 08 Jun 2017 14:14:16 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 14/17] posix: Remove all alloca usage in glob Date: Thu, 8 Jun 2017 18:13:28 -0300 Message-Id: <1496956411-25594-15-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> With alloca usage removal from glob this patch wraps it up by removing all the alloca defines and macros usage. Checked on x86_64-linux-gnu. posix/glob.c (glob_in_dir): Remove alloca_used argument. (glob): Remove alloca_used. --- posix/glob.c | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 62be373..652ae9e 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -58,7 +58,6 @@ #include #include #include -#include #ifdef _LIBC # undef strdup @@ -229,17 +228,11 @@ convert_dirent64 (const struct dirent64 *source) # ifdef GNULIB_defined_closedir # undef closedir # endif - -/* Just use malloc. */ -# define __libc_use_alloca(n) false -# define alloca_account(len, avar) ((void) (len), (void) (avar), (void *) 0) -# define extend_alloca_account(buf, len, newlen, avar) \ - ((void) (buf), (void) (len), (void) (newlen), (void) (avar), (void *) 0) #endif static int glob_in_dir (const char *pattern, const char *directory, int flags, int (*errfunc) (const char *, int), - glob_t *pglob, size_t alloca_used); + glob_t *pglob); extern int __glob_pattern_type (const char *pattern, int quote) attribute_hidden; @@ -295,7 +288,6 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), bool dirname_modified; glob_t dirs; int retval = 0; - size_t alloca_used = 0; struct char_array dirname; if (pattern == NULL || pglob == NULL || (flags & ~__GLOB_FLAGS) != 0) @@ -972,7 +964,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), status = glob_in_dir (filename, dirs.gl_pathv[i], ((flags | GLOB_APPEND) & ~(GLOB_NOCHECK | GLOB_NOMAGIC)), - errfunc, pglob, alloca_used); + errfunc, pglob); if (status == GLOB_NOMATCH) /* No matches in this directory. Try the next. */ continue; @@ -1079,7 +1071,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (dirname_modified) flags &= ~(GLOB_NOCHECK | GLOB_NOMAGIC); status = glob_in_dir (filename, char_array_str (&dirname), flags, - errfunc, pglob, alloca_used); + errfunc, pglob); if (status != 0) { if (status == GLOB_NOMATCH && flags != orig_flags @@ -1256,8 +1248,7 @@ struct globnames_result The GLOB_APPEND flag is assumed to be set (always appends). */ static int glob_in_dir (const char *pattern, const char *directory, int flags, - int (*errfunc) (const char *, int), - glob_t *pglob, size_t alloca_used) + int (*errfunc) (const char *, int), glob_t *pglob) { void *stream = NULL; struct globnames_array globnames; From patchwork Thu Jun 8 21:13:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103420 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2633123qgd; Thu, 8 Jun 2017 14:16:48 -0700 (PDT) X-Received: by 10.84.208.102 with SMTP id f35mr34041811plh.53.1496956608588; Thu, 08 Jun 2017 14:16:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956608; cv=none; d=google.com; s=arc-20160816; b=ar79FJbBskm3zlFfTavc3HRyKmoW7bUYlmTdpXNYTXLPVmNJzUd5X9LGUud6mE+rp0 1+pkTt/i+gJr+yuz15HFFgxlIKxDKCSnRnkf0u/9zm7pT/3+ApYlBbNj0+n0gsEQHRWq CrVhOG5wWSpttNnu4HqXCkXTHh3riPQBooEPDZXZid8LSRnC/xXNqXrHt5jDPCXHGJ/O Voqlpx7Ky1U9+bY33v1FzkPyFsws2ae5Mb+zhngFc9KvkuDt4EZyaCg+F7uH7c/PDd1B wFZpBeX2NE113PTwXbWj+bz18yBmz5vZhtoH/qd+4O2iF4AbTu140QcXig7/Foh4vt4J AQPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=mftcGRoC+RFXeTMXrZFfQPjseGZICjE9/eH/xdSJQtc=; b=n4kB/mpU0WRvEkV82j3dqY6RN3l+H/Y4AsTtt1OTDTOrTSzDgrb87oiEzhUMqwpSfh HElZ9QmPKY6aAIuDNC3kQ4xBYZtkZfSJck/ky13ZNaTowI/FqIZYq5lSZOZqtmy/Csu2 GjyGUNGt6ujScHD/hbCSGQhM3Ls95/dYcXsieoKGVCYpqESQW0rmw8zbiP0gTWxvI/xH HD5xLPlXdOGHD2UlUuMYP+extYwxtmkM3vwi/uhrJ4846Ga3rsLyqQ6frUKLa5JOuo6k HvLJQcHS/fwQoo0zwchC4K5kQrlJ7kLUdOSVweFBKFN70dYmoSvTL0IRp3Je7RZtVb2j 8sTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80214-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80214-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id w9si5274301pge.413.2017.06.08.14.16.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:48 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80214-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80214-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80214-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=QK2VCwYcuxXwt6H4GUZiq4PTbcndbbS ejL3gjrXvPJqI36vrgHz02SXwTW7WFJPpHyf+QLRqANT/ntGMeuUxl7XUjIkdrKo YqaKzzSQNMq0iLsUPXEJcDQ5oTVF97T2xZHDZ+vtf3FPBVSUQN3o/SL9Rf2pYNHD 6NgLV9S2cW/U= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=yyHwrTzvTev7fgTMiRj+gIDvEAc=; b=iONOD MdAAi3KmSlVaSAWpTZ81WmbbBYA0ARdIz4oJlK5vH6P8IjmSxuRnHzTcei7mvv5T IpdO/FJ0lch0fu3A/lo7ZlzDm/hDUfoLn7RdamGTGS7BTQg7jn2ye6ol1QUC9iIs rnYZ1D5TWQdsE1P/orJM4cw+k/liY9aiFBYRtY= Received: (qmail 41715 invoked by alias); 8 Jun 2017 21:14:28 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 40831 invoked by uid 89); 8 Jun 2017 21:14:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-27.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 spammy=6898 X-HELO: mail-qt0-f182.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=mftcGRoC+RFXeTMXrZFfQPjseGZICjE9/eH/xdSJQtc=; b=WYJSsdzJw+lHlWuD5jisId+BCk2IOJZhKIw/xMjq01krBaFvkNwee37/Dzms0JCpRR FCtAczZxPHRdIkZwKMAjddRT1fC7LBbt7BR3zC5HtrJ/nbWGjnM8klgm/fuyGzi7P12j Vq9qXQlQfNQdPWeYOfksQkmVXLd/v5pn/3fUyWC5LuYndkxSalLRFxOIg0HkzftaTZ5w 9pa7DpL0/EDAgziYbrIJ1Xim7BRMBCsRunQiH5VvFNI+pi0V+J+zVA3kkhn5pCf7d9FK pt1hsEVEWVNKnAgsNU5nX7R7eB22c4LltI0cQQeEkihai225RywRkwT36ZprURpLUaqg YtkQ== X-Gm-Message-State: AODbwcAv0/b+qABMWBVZPQFT9X1LJrsLAYbIpnWzWQi4NeXFj2+laoKL beycIqhgRQVjcw6tivQ0Bw== X-Received: by 10.237.47.69 with SMTP id l63mr14253692qtd.196.1496956459181; Thu, 08 Jun 2017 14:14:19 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 15/17] posix: Use char_array for home_dir in glob Date: Thu, 8 Jun 2017 18:13:29 -0300 Message-Id: <1496956411-25594-16-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch uses char_array for home directory discovery. It simplifies the buffer management. Checked on x86_64-linux-gnu. * posix/glob.c (glob): Use char_array for home directory. --- posix/glob.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 652ae9e..4cefdca 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -606,8 +606,15 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), || char_array_pos (&dirname, 2) == '/'))) { /* Look up home directory. */ - char *home_dir = getenv ("HOME"); - int malloc_home_dir = 0; + struct char_array home_dir; + + const char *home_env = getenv ("HOME"); + home_env = home_env == NULL ? "" : home_env; + if (!char_array_init_str (&home_dir, home_env)) + { + retval = GLOB_NOSPACE; + goto out; + } # ifdef _AMIGA if (home_dir == NULL || home_dir[0] == '\0') home_dir = "SYS:"; @@ -634,7 +641,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), home_dir = "c:/users/default"; /* poor default */ } # else - if (home_dir == NULL || home_dir[0] == '\0') + if (char_array_is_empty (&home_dir)) { int success; char user_name[LOGIN_NAME_MAX]; @@ -667,9 +674,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), # endif if (p != NULL) { - home_dir = strdup (p->pw_dir); - malloc_home_dir = 1; - if (home_dir == NULL) + if (!char_array_set_str (&home_dir, p->pw_dir)) { scratch_buffer_free (&pwtmpbuf); goto err_nospace; @@ -678,10 +683,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), scratch_buffer_free (&pwtmpbuf); } } - if (home_dir == NULL || home_dir[0] == '\0') + if (char_array_is_empty (&home_dir)) { - if (__glibc_unlikely (malloc_home_dir)) - free (home_dir); if (flags & GLOB_TILDE_CHECK) { retval = GLOB_NOMATCH; @@ -689,8 +692,11 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } else { - home_dir = (char *) "~"; /* No luck. */ - malloc_home_dir = 0; + if (!char_array_set_str (&home_dir, "~")) + { + retval = GLOB_NOSPACE; + goto out; + } } } # endif /* WINDOWS32 */ @@ -698,7 +704,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* Now construct the full directory. */ if (char_array_pos (&dirname, 1) == '\0') { - if (!char_array_set_str (&dirname, home_dir)) + if (!char_array_set_str (&dirname, char_array_str (&home_dir))) goto err_nospace; dirlen = char_array_size (&dirname) - 1; } @@ -706,9 +712,11 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { /* Replaces '~' by the obtained HOME dir. */ char_array_erase (&dirname, 0); - if (!char_array_prepend_str (&dirname, home_dir)) + if (!char_array_prepend_str (&dirname, + char_array_str (&home_dir))) goto err_nospace; } + char_array_free (&home_dir); dirname_modified = true; } # if !defined _AMIGA && !defined WINDOWS32 From patchwork Thu Jun 8 21:13:30 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103421 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2633164qgd; Thu, 8 Jun 2017 14:16:58 -0700 (PDT) X-Received: by 10.98.76.140 with SMTP id e12mr38083815pfj.78.1496956618787; Thu, 08 Jun 2017 14:16:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956618; cv=none; d=google.com; s=arc-20160816; b=ho2A2W4fRLWXSgrTVRaOIvru5Dl2O7O6r5RRB6RbaVy21YuVCqw2eFTscqNQd5fY7W VEsHhVYT5pGTo7aazXbXze4SbJL8IsJQluOmBZe43MF+gjeDElr9TXOS+h8eJnnaAMWj Mz8TjF9tLKxEP+2RzsH/X208bp1XSNgnx0z592yQDIcJKGBkRsMP0THjYqz2zf6dip7z hpemVokP1LEvuotdHudGcGQB/S4pNexgwT65Oc1YB+xiYks/PhyZfzrSFCEICU3U8YVx EtRQuh3FCPMGNYu2A2+jfdYVYaQ5+gpi/84HzYDR0epRpxD2ItPVStNKM46ChIT10ais bL8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=EWZEnxDhCmAcQZ+KkeTp1qsH0In9i8igrDNbCu97hsc=; b=gbRagYD/SwYMmfMU7mTomviHee4nWDEyEswP/sy02owxbNPJXxoOEEUYixMYAOjLKW lCWRk0MjbhjsB9soAKV6+rAf7yYyJU2j3By/frhTquoa3JjqtOFMhWMEUzifqWXl/j8b WKY727tSiL1nYRfBjsegoBQxV/yzIRsmS5XH3BRxyHqVoYpaeHnHHYPmMjJxLmBet7Fr s5fa4p8rzfhXXURMsn+Mr+f6ftG/OBXlTZnEwUqokZoRIkaiVrnjx6OG58k6tFblmAmG zuRu/UY7iksAeKUUen6QnzoKQoGVFT4srkk3zQp6R9azTuMB2NO/jpnjyjj7Tg6J/elS h+NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80215-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80215-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id r90si2909961pfk.14.2017.06.08.14.16.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:16:58 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80215-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80215-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80215-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=cuxJ6jt3ggnlymBLxBLRrcWQVxaYKmw A6eDV4fToHG6vI66XH+o/A8mChqiLjdsrD2H5r+0B7FjzcElpuSWRycEn+UoHpC8 6XcFPUFpRmAnahvbkSTIp9mhgbNrdl43pueLHBM1YYIt6pIGKUDhZakOF815HVTH fUwHVsLiB3JY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=4kgH+LovoMsbwCZVSmNQrt1Q5+g=; b=HpG4b wokeK2GAcv+YD3uTl/pHdKe8UH0ie5rennOh1QijhDFgJ7A+4g5Z5tJvPKR7m7q8 2zCggbTDHlIa+BEptMlEkheY5V8yF/OSwrSG07aIA43nSdQBu/jlnLas7a28l44G t4rN9PdRW4YXnIl4ehSNg2dpNIV0Z0P3pokV4I= Received: (qmail 41825 invoked by alias); 8 Jun 2017 21:14:29 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 41119 invoked by uid 89); 8 Jun 2017 21:14:22 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f173.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=EWZEnxDhCmAcQZ+KkeTp1qsH0In9i8igrDNbCu97hsc=; b=aLk1GMSCYSQKDcDxEZpSndUr2RWD3/Kt4/MPho1haeVbCvl4XmjnHexJhYaCl5JaG1 XD76uMpx7D1m1XXVokAZyMrAgOwu+L5G96T/KVwHyki2wNfiSxxmhyrZ7WDit71THUwr 2uqTtsSHbU1kfjrSW+2Im9hZ9xTPQVDHNQPusWzideWzxK3HN15BWleUMisd5PKTtDuJ 5cUvkNFHBzRrjWoX0b1DpTxEoX8mt1cH4o8CGccMq1iV6YI92dh/oEINNMKIwlXAUX6V XgY6J1YB8GxQp5F4I4aycX6bsBEFewqdiPkuNkM/oMlra2j/eu4PDtznmembwyAyfFEU ZfMg== X-Gm-Message-State: AODbwcBEPKUeDdSckmyIVkF2ZM8Y31D68iXvGPACj+qgYzhxwdOHHczD yaaN8LFgM2E6Rz6oLX0VGA== X-Received: by 10.237.44.7 with SMTP id f7mr21279305qtd.142.1496956461641; Thu, 08 Jun 2017 14:14:21 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 16/17] posix: Add common function to get home directory Date: Thu, 8 Jun 2017 18:13:30 -0300 Message-Id: <1496956411-25594-17-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch adds a common function to get the full home directory from a user. No functional changes expected. Checked on x86_64-linux-gnu. * posix/glob.c (get_home_directory): New function. (glob): Use get_home_directory. --- posix/glob.c | 145 +++++++++++++++++++++++------------------------------------ 1 file changed, 56 insertions(+), 89 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 4cefdca..2b11e45 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -264,6 +264,44 @@ next_brace_sub (const char *cp, int flags) return *cp != '\0' ? cp : NULL; } +/* Obtain the full home directory path from user 'user_name' and writes it + on char_array 'home_dir'. */ +static bool +get_home_directory (const char *user_name, struct char_array *home_dir) +{ + struct passwd *p; +#if defined HAVE_GETPWNAM_R || defined _LIBC + struct passwd pwbuf; + int save = errno; + struct scratch_buffer pwtmpbuf; + scratch_buffer_init (&pwtmpbuf); + + while (getpwnam_r (user_name, &pwbuf, pwtmpbuf.data, pwtmpbuf.length, &p) + != 0) + { + if (errno != ERANGE) + { + p = NULL; + break; + } + if (!scratch_buffer_grow (&pwtmpbuf)) + return false; + __set_errno (save); + } +#else + p = getpwnam (pwtmpbuf.data); +#endif + + bool retval = false; + if (p != NULL) + { + if (char_array_set_str (home_dir, p->pw_dir)) + retval = true; + } + scratch_buffer_free (&pwtmpbuf); + return retval; +} + /* Do glob searching for PATTERN, placing results in PGLOB. The bits defined above may be set in FLAGS. @@ -649,38 +687,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), success = __getlogin_r (user_name, sizeof (user_name)) == 0; if (success) { - struct passwd *p; -# if defined HAVE_GETPWNAM_R || defined _LIBC - struct passwd pwbuf; - int save = errno; - struct scratch_buffer pwtmpbuf; - scratch_buffer_init (&pwtmpbuf); - - while (getpwnam_r (user_name, &pwbuf, - pwtmpbuf.data, pwtmpbuf.length, &p) - != 0) - { - if (errno != ERANGE) - { - p = NULL; - break; - } - if (!scratch_buffer_grow (&pwtmpbuf)) - goto err_nospace; - __set_errno (save); - } -# else - p = getpwnam (pwtmpbuf.data); -# endif - if (p != NULL) - { - if (!char_array_set_str (&home_dir, p->pw_dir)) - { - scratch_buffer_free (&pwtmpbuf); - goto err_nospace; - } - } - scratch_buffer_free (&pwtmpbuf); + if (!get_home_directory (user_name, &home_dir)) + goto err_nospace; } } if (char_array_is_empty (&home_dir)) @@ -693,10 +701,7 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), else { if (!char_array_set_str (&home_dir, "~")) - { - retval = GLOB_NOSPACE; - goto out; - } + goto err_nospace; } } # endif /* WINDOWS32 */ @@ -777,59 +782,21 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), } /* Look up specific user's home directory. */ - { - struct passwd *p; - struct scratch_buffer pwtmpbuf; - scratch_buffer_init (&pwtmpbuf); - -# if defined HAVE_GETPWNAM_R || defined _LIBC - struct passwd pwbuf; - int save = errno; - - while (getpwnam_r (user_name, &pwbuf, - pwtmpbuf.data, pwtmpbuf.length, &p) != 0) - { - if (errno != ERANGE) - { - p = NULL; - break; - } - if (!scratch_buffer_grow (&pwtmpbuf)) - { - retval = GLOB_NOSPACE; - goto out; - } - __set_errno (save); - } -# else - p = getpwnam (user_name); -# endif - - /* If we found a home directory use this. */ - if (p != NULL) - { - if (!char_array_set_str (&dirname, p->pw_dir)) - { - scratch_buffer_free (&pwtmpbuf); - retval = GLOB_NOSPACE; - goto out; - } - - dirlen = strlen (p->pw_dir); - dirname_modified = true; - } - else - { - if (flags & GLOB_TILDE_CHECK) - /* We have to regard it as an error if we cannot find the - home directory. */ - { - retval = GLOB_NOMATCH; - goto out; - } - } - scratch_buffer_free (&pwtmpbuf); - } + if (get_home_directory (user_name, &dirname)) + { + dirlen = char_array_size (&dirname) - 1; + dirname_modified = true; + } + else + { + if (flags & GLOB_TILDE_CHECK) + /* We have to regard it as an error if we cannot find the + home directory. */ + { + retval = GLOB_NOMATCH; + goto out; + } + } } # endif /* Not Amiga && not WINDOWS32. */ } From patchwork Thu Jun 8 21:13:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 103422 Delivered-To: patch@linaro.org Received: by 10.140.91.77 with SMTP id y71csp2633217qgd; Thu, 8 Jun 2017 14:17:07 -0700 (PDT) X-Received: by 10.98.147.142 with SMTP id r14mr11486080pfk.168.1496956627862; Thu, 08 Jun 2017 14:17:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1496956627; cv=none; d=google.com; s=arc-20160816; b=Onl1sKhV+kTAnu5S82r5rcEKVcpNu36V1YoLcvfdQvlamWcc20vUC230afYMcMEuxb d3WUE4zlNt8bV+vS9gd53BwothbAFyTB8JSSshLYE73c3y1kCnyth/MVX8sYSugUaPrF yr43C5r7z9YdwdN7I/q6ujWl40yl7a9DWVkALTEBgwTW8Akd9Rc/Nxss0WTrs8YtBkkS CfIsay1vWbN89Kc1M5Po7PQoKAaVJPdAg+ypTiqP5Ujm4h0FxvtSxERzntafusrPyjNN FSE81EODuR/fQ8lHwFozy0RRjkP9hy/jrEUYOMGpQEsIlDsVQfqyIVpYfZdc4zM48Xsj nqRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:to:from:delivered-to :sender:list-help:list-post:list-archive:list-subscribe :list-unsubscribe:list-id:precedence:mailing-list:dkim-signature :domainkey-signature:arc-authentication-results; bh=vKm4bLoEFjGS2Wp1IdI4wsvyJA895uL8gXks41B1DHM=; b=w5jDFC5UzCsdBYNR7zbJQnxUBank9G534XjK4sU6MdjhBdgQ+EFWK28mamJShe74pt 4kPjmpBdySS60d2C3U6HWcbwydRqdhKm20cL2eJfxXCMexMpju9QBIZaB7Eaq/J0ZoHJ WiosFzNOup1TzxSWlpsZVHPJm4LTwqkvEn/4eK6TmGHQFdrvrSukRAI94Ke20DJ5NHVD bXGcbYhQat52yGNgiKfY2kzKb/+qD+PIrGvBMsXM4s0XLPrne/HKNeHnIOs1VGx1kKj6 fhEIEBIAGAd6YEx5fd811geMwjPXND2eF7Hhu+T7imyvJnZE2XuLLdAWcj8qCGOJMt8r f3Ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80216-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80216-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id q2si5276316plk.300.2017.06.08.14.17.07 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Jun 2017 14:17:07 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-80216-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org; spf=pass (google.com: domain of libc-alpha-return-80216-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-80216-patch=linaro.org@sourceware.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; q=dns; s=default; b=G5tD2Ff54k7mayhL6rHBkXUai7O4fMi lFDCx4kUh9RUcQAvn9yos3y/I+pVgpsozvbEjDAQiVGbtEnq8fXhzS4ZiDzxqp79 ef2UGtBtNUydqrHelQ512O6dpoveDtYM5PfYZBVs/oAoOsdxswB2AVtizTGjPBMV 5N8ofLvCbTlE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:in-reply-to :references; s=default; bh=kVKv2XxiPQMN8iKHz67Nf11CJZo=; b=dpKnP xOhu1gDoupciWnzxdEXfwQs/6wysGS1YsXpup6WViUgicXDG+/P+vemSUCcrRirl upOOL5Yul0nGcFRanZdo6zTkTAgKyXxk/nbebLnqH3f52b4+lAoITz7xy0zDYaLf dF8CskxvAnvGyrACyVDU0HZl6pO2CY8nfOVSr0= Received: (qmail 41890 invoked by alias); 8 Jun 2017 21:14:30 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 41302 invoked by uid 89); 8 Jun 2017 21:14:24 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f177.google.com X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=vKm4bLoEFjGS2Wp1IdI4wsvyJA895uL8gXks41B1DHM=; b=DOD34K9r/8MhigwnGOjZqwPpLMSMIU4W8bgsYpIkNKGMB9RshjD3JvR2zwolr13y1G vmYbZEUz4IRM/3U+BX68Xyu4m+Geq81CiyagTdJpXefvcuMlwnDeSPMY1fc8t6V0ueEV jZpvE5lqazyd+GhM8fvIYGxWddi+ypwL+RjyEUOZ91KDWtuLW2Iii+XWfKSEPXmATced KM0lxWXYMhwB154Kuclji/4VeawxDO9qo6W1uh7mKWMtFx7t1XW72hfBJie29F5NydUS OKoMEwvjvkQ5q/duUsv8XdILgg/nHG/AdN+Et+rJJW3dtX5xRKRXFGKFdWpTxhCCpHzY Or5A== X-Gm-Message-State: AKS2vOx9HYPcUZUY1HBCTy9Lo0EUNR1wwRGFsfr9kEiL4GAdA7Fzcrfh Jl+cmEgH7GnqJ+Ptn7O27w== X-Received: by 10.55.182.135 with SMTP id g129mr20153212qkf.111.1496956464010; Thu, 08 Jun 2017 14:14:24 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 17/17] posix: More check for overflow allocation in glob Date: Thu, 8 Jun 2017 18:13:31 -0300 Message-Id: <1496956411-25594-18-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> References: <1496956411-25594-1-git-send-email-adhemerval.zanella@linaro.org> This patch adds and replace the allocation overflow based using malloc internal functions check_add_wrapv_size_t and __libc_reallocarray. Checked on x86_64-linux-gnu. * posix/glob.c (glob_malloc_incr): New function. (glob_malloc_incr2): Likewise. (glob_realloc_incr): Likewise. (glob): Use glob_{realloc,malloc}_incr{2}. --- posix/glob.c | 92 +++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 63 insertions(+), 29 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 2b11e45..0de8233 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -302,6 +302,39 @@ get_home_directory (const char *user_name, struct char_array *home_dir) return retval; } +/* Allocate '(size + incr) * typesize' bytes while for overflow on the + arithmetic operations. */ +static void * +glob_malloc_incr (size_t size, size_t incr, size_t typesize) +{ + size_t newsize; + if (check_add_wrapv_size_t (size, incr, &newsize)) + return NULL; + return __libc_reallocarray (NULL, newsize, typesize); +} + +/* Allocate '(size + incr1 + incr2) * typesize' bytes while for overflow on + the arithmetic operations. */ +static void * +glob_malloc_incr2 (size_t size, size_t incr1, size_t incr2, size_t typesize) +{ + size_t newsize; + if (check_add_wrapv_size_t (size, incr1, &newsize) + || check_add_wrapv_size_t (newsize, incr2, &newsize)) + return NULL; + return __libc_reallocarray (NULL, newsize, typesize); +} + +/* Reallocate '(size + incr1) * typesize' bytes while for overflow on the + arithmetic operations. */ +static void * +glob_realloc_incr (void *old, size_t size, size_t incr, size_t typesize) +{ + size_t newsize; + if (check_add_wrapv_size_t (size, incr, &newsize)) + return NULL; + return __libc_reallocarray (old, newsize, typesize); +} /* Do glob searching for PATTERN, placing results in PGLOB. The bits defined above may be set in FLAGS. @@ -356,11 +389,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), { size_t i; - if (pglob->gl_offs >= ~((size_t) 0) / sizeof (char *)) - goto err_nospace; - - pglob->gl_pathv = (char **) malloc ((pglob->gl_offs + 1) - * sizeof (char *)); + pglob->gl_pathv = glob_malloc_incr (pglob->gl_offs, 1, + sizeof (char *)); if (pglob->gl_pathv == NULL) goto err_nospace; @@ -816,10 +846,11 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), : (__lstat64 (char_array_str (&dirname), &st64) == 0 && S_ISDIR (st64.st_mode))))) { - size_t newcount = pglob->gl_pathc + pglob->gl_offs; char **new_gl_pathv; + size_t newcount; - if (newcount > SIZE_MAX / sizeof (char *) - 2) + if (check_add_wrapv_size_t (pglob->gl_pathc, pglob->gl_offs, + &newcount)) { nospace: free (pglob->gl_pathv); @@ -828,8 +859,8 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), goto err_nospace; } - new_gl_pathv = realloc (pglob->gl_pathv, - (newcount + 2) * sizeof (char *)); + new_gl_pathv = glob_realloc_incr (pglob->gl_pathv, newcount, 2, + sizeof (char *)); if (new_gl_pathv == NULL) goto nospace; pglob->gl_pathv = new_gl_pathv; @@ -837,9 +868,12 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), if (flags & GLOB_MARK) { char *p; - pglob->gl_pathv[newcount] = malloc (dirlen + 2); + + pglob->gl_pathv[newcount] = glob_malloc_incr (dirlen, 2, + sizeof (char)); if (pglob->gl_pathv[newcount] == NULL) goto nospace; + p = mempcpy (pglob->gl_pathv[newcount], char_array_str (&dirname), dirlen); p[0] = '/'; @@ -976,18 +1010,19 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), /* No matches. */ if (flags & GLOB_NOCHECK) { - size_t newcount = pglob->gl_pathc + pglob->gl_offs; + size_t newcount; char **new_gl_pathv; - if (newcount > SIZE_MAX / sizeof (char *) - 2) + if (check_add_wrapv_size_t (pglob->gl_pathc, pglob->gl_offs, + &newcount)) { nospace2: globfree (&dirs); goto err_nospace; } - new_gl_pathv = realloc (pglob->gl_pathv, - (newcount + 2) * sizeof (char *)); + new_gl_pathv = glob_realloc_incr (pglob->gl_pathv, newcount, 2, + sizeof (char *)); if (new_gl_pathv == NULL) goto nospace2; pglob->gl_pathv = new_gl_pathv; @@ -1089,15 +1124,16 @@ glob (const char *pattern, int flags, int (*errfunc) (const char *, int), : (__lstat64 (pglob->gl_pathv[i], &st64) == 0 && (S_ISDIR (st64.st_mode) || S_ISLNK (st64.st_mode))))) { - size_t len = strlen (pglob->gl_pathv[i]) + 2; - char *new = realloc (pglob->gl_pathv[i], len); + size_t len = strlen (pglob->gl_pathv[i]); + char *new = glob_realloc_incr (pglob->gl_pathv[i], len, 2, + sizeof (char)); if (new == NULL) { globfree (pglob); pglob->gl_pathc = 0; goto err_nospace; } - strcpy (&new[len - 2], "/"); + strcpy (&new[len], "/"); pglob->gl_pathv[i] = new; } } @@ -1182,7 +1218,7 @@ prefix_array (const char *dirname, char **array, size_t n) for (i = 0; i < n; ++i) { size_t eltlen = strlen (array[i]) + 1; - char *new = malloc (dirlen + 1 + eltlen); + char *new = glob_malloc_incr2 (dirlen, 1, eltlen, sizeof (char)); if (new == NULL) { while (i > 0) @@ -1190,11 +1226,10 @@ prefix_array (const char *dirname, char **array, size_t n) return 1; } - { - char *endp = mempcpy (new, dirname, dirlen); - *endp++ = DIRSEP_CHAR; - mempcpy (endp, array[i], eltlen); - } + char *endp = mempcpy (new, dirname, dirlen); + *endp++ = DIRSEP_CHAR; + mempcpy (endp, array[i], eltlen); + free (array[i]); array[i] = new; } @@ -1345,16 +1380,15 @@ glob_in_dir (const char *pattern, const char *directory, int flags, if (nfound != 0) { char **new_gl_pathv; + size_t newlen; result = 0; - if (SIZE_MAX / sizeof (char *) - pglob->gl_pathc - < pglob->gl_offs + nfound + 1) + if (check_add_wrapv_size_t (pglob->gl_pathc, pglob->gl_offs, &newlen) + || check_add_wrapv_size_t (newlen, nfound, &newlen) + || check_add_wrapv_size_t (newlen, 1, &newlen)) goto memory_error; - new_gl_pathv - = realloc (pglob->gl_pathv, - (pglob->gl_pathc + pglob->gl_offs + nfound + 1) - * sizeof (char *)); + new_gl_pathv = realloc (pglob->gl_pathv, newlen * sizeof (char *)); if (new_gl_pathv == NULL) {