From patchwork Fri Aug 11 14:50:38 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: 109892 Delivered-To: patch@linaro.org Received: by 10.182.109.195 with SMTP id hu3csp976697obb; Fri, 11 Aug 2017 07:53:09 -0700 (PDT) X-Received: by 10.98.62.130 with SMTP id y2mr16797424pfj.114.1502463188986; Fri, 11 Aug 2017 07:53:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1502463188; cv=none; d=google.com; s=arc-20160816; b=HnNh+aitHPrAYPzeosN1rVzQAQaPCE9slMW6Lfkz2wEjMis9hnz/usD0AY4YonbkX2 DF3Lc6p0nOgwaA0FVS4z+hW8wawDoHJYdYUJ/aVIBUlix+E6HvZ5Jkon7kdWBAI3pw2g ZF10kJnEwrHvyqwLITx14ye7AjKeCuYmOesLK4SmkhEmvwBOtxxhMFnKplytPpqrd8Hk iTKKyeA0RAKEK+pn72L1k1TGaYaHS+Bh45FktzS3XMwFl3ARcHBby/wsxgMYzjxuc+C0 7do2c/6tfFYbySiK90Wo4PJ5gFZ0EPn7QYcRmmkCyM4Fa6WVMR6HoWnK5p08akZhkdrK ZoTA== 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=vWM2DFVascfaP7KbzwD997aBLDbEL9FjQ0VvRxxYDJ0=; b=WJlCiGJV6qzBpQKJnJLfFEkZlsiYh4tYNbquUbMkU3GCEzAtjrZLlpUaRVzsLETAq5 MD26adFdb/qhgNgMIU5A6pTm9mZfWp724mZWHSq+S81fZkEyi+2lqe8kHZN44LKDI3IL 7H5ND6LZTEkeLDJxgaL3WWWdUQxOSRBwnMa4nBRP2x+FeyWYR3EwhiNRLzTeLtch6t1J 6GYpYqJD4zLpt1FwjKu3j42gyqQUlhxn6TJadUMDz3I+i4i0LhmflvTSy8hmjcLi+PIM meuMtpSOg6UXXIne0Y/gu6/j2aKGBGU3K80D19+/SmDP3g/dpOv+vW+EV+E2hGQ0PJJi tXtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=lPxNmNoJ; spf=pass (google.com: domain of libc-alpha-return-83034-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83034-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 v123si622742pfv.124.2017.08.11.07.53.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Aug 2017 07:53:08 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-return-83034-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 header.s=default header.b=lPxNmNoJ; spf=pass (google.com: domain of libc-alpha-return-83034-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-83034-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=ZTaJU0+9Ct2J6WcN0Drukk6pIEkxHm8 Nk/rBSxHFIRldUSW1NIwGLuoPbDMmwPwk4MEkKWad97B5PaKxgXtcdMjRF/Kxv3b z0cH1c8juGppfxocNV784rYPA57GS39+43ESwmGDXP8xHrT+5abOugTPpQw2QnQO N6BJx5dcuZVs= 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=ottvdh5W1Xz+s9JN6GhJPi9fDWc=; b=lPxNm NoJautVY9alLZVDwtixROQP5qgGjp6t+StVOw0IKCIJAef2yZI0zqbXoXSfuvPGd vQ0IyYwmjan+O6K5EpruF0UMgXxJX2jbYUr/OaaxWsH5OfZTsKtrjYQIvZTn3vPQ qJm5cch1oXbl0vtgU2DoqqNPnUaGCezUhMwmtQ= Received: (qmail 78872 invoked by alias); 11 Aug 2017 14:51: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 78203 invoked by uid 89); 11 Aug 2017 14:51:14 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=ham version=3.3.2 spammy=managements X-HELO: mail-qk0-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=vWM2DFVascfaP7KbzwD997aBLDbEL9FjQ0VvRxxYDJ0=; b=YPY691DGnWEvpBs9wtsccD0nJxYdBJy+iew+89Q0tEKMvuRQXpffLzsLW3zg2yRxi9 kJwxwckPd3bO2vlIyJOix18vod1db5TjSV3wyrzUFMC+bFjYslns/JTtc+Oh4pvbHanJ NCGKqmP72JAycjmJj1j3ASZTtIif7UzsL2S34wVFDTKiAWZwu/X22FNon24Y87BKg7L0 niq35Unek/Hn6zplLAKK8GE7NlXtkMn/Cq78A0dRzBzFrCgx2uc13YtLC9kZQ8PGktRf +RlKCYnGvdMcXJbR7sSXC3H/jWj9Lv6YUmha0eKXp+0CxpRPan9V+eO+XC8/667fLRnr zHvA== X-Gm-Message-State: AHYfb5gV0T4SWewOpk9/c3PF8ou5J2XdU8PxTev6wzU++5EAPbqRL2jg c0RCGSEPkCLqsw0suCX4+g== X-Received: by 10.55.23.204 with SMTP id 73mr20809098qkx.157.1502463066818; Fri, 11 Aug 2017 07:51:06 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 12/18] posix: Use dynarray for globname in glob Date: Fri, 11 Aug 2017 11:50:38 -0300 Message-Id: <1502463044-4042-13-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1502463044-4042-1-git-send-email-adhemerval.zanella@linaro.org> References: <1502463044-4042-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 c09b347..f40ee6d 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -238,33 +238,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); @@ -1264,6 +1237,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. @@ -1275,26 +1262,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))) @@ -1383,30 +1357,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++; } } } @@ -1416,10 +1370,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; @@ -1440,61 +1397,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); } }