From patchwork Mon Feb 5 13:27:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 126880 Delivered-To: patch@linaro.org Received: by 10.46.124.24 with SMTP id x24csp1971657ljc; Mon, 5 Feb 2018 05:28:44 -0800 (PST) X-Google-Smtp-Source: AH8x227g5r8PjbMK6pky320szSdlP86Z1coAvgUsL6rp0FenE/USEkRUHMMYukwGLUsTjz77TvcR X-Received: by 2002:a17:902:48c8:: with SMTP id u8-v6mr42621783plh.272.1517837324732; Mon, 05 Feb 2018 05:28:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517837324; cv=none; d=google.com; s=arc-20160816; b=lh3CobHsFBI8ykBtqnPETT9NYfEw8OQT+mrlWFovChivwBWq1ic1vPhBNYQn+ICzsi 0xEL9CdRzBMs5zPU/oI0EHEaDnJ+T4pt7qzQ9q4oOADbpmGTELgE52FJrdp56UNGnfFP pF0go751rw/LM1UqAxGvUPS80+oMjEyDkx+h8jJATQHhuynYvrHEYhmRm/FfzVLKczTH LUws6cgzx05vzMD4ZMscRmTfbnSR+l6LaiowjHdzHfUmF8xmmKE0O0SER1D55ItwqY9H oO7rgsl+dIfrYoZsn3+0jQmNvXfs8kH8ZwhHgCB7pOC8tJnchTaEdZuDkfK3jy5UbQfY LHiA== 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=8wnpdPwrnzEzLoN4Ilk9UZN+3OjwSpuIlTmotSHiRWE=; b=JQiuS4WFp6XqeRhNhVteLFk9gpmakFR7t8ECLV2rVb+I06COZLGogiTubN8GvIqws5 nUF4WyGzqOZqu5Hzq2popefi8ft4iFyCE0tHKgq1C6PMM8hnspl+jFK5/8iwH232EffH AP8VTqOGk7xd5u6filsL4Q+qHKUlfHgB+OOzHQb47T2XEkjoPyPKi91X6rhJIxirA9zB G8dtMBJmsKiF2kGxRniuvBgQX4YmP6j6NaGCj+wk7UmUyVd4wQDTZO/Y9g0kv3Aoo6Fk QYnERBIT1Q48rayj37A6itq5ut/6Th/HIJBiiMUu2/qpMWDCdKyKhtrgJ9+EtM+7nhdU U+Mw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=UbnIRrHT; spf=pass (google.com: domain of libc-alpha-return-89969-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89969-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 a91-v6si6872165pld.125.2018.02.05.05.28.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Feb 2018 05:28:44 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-89969-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=UbnIRrHT; spf=pass (google.com: domain of libc-alpha-return-89969-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-89969-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=aybWqQ6/1Tq/6TgTQrYu7TSfDLlttq7 YQgG5ipM19ei7Ab3jU3B2Y4hmnX1jVuJqtaSO8BJ0G23KwvA3pWpAxEwWZwkZ7vn GUlOgXulIY3KBOfRQq1gU2JDQ4GxyvgOb9q/uNrymW2/hP1Py4XVY+f0VDOWh3tG sZKSaRS/CQ4s= 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=WhU2oJ6DwxKfOC6uQ/M7yB/GXIU=; b=UbnIR rHT+9b/NhZa+gPEGXauRulAhfAZrCcEOt9d5BzUwsQzIQ9g1ANjOBh2lx1+4nDsM i85buf2gEZTTxLXZN2x7u8EeOyOpo+G7KU6twrMAJEvdmAui88rt6r6AHL1mSBNJ tMrUfBNH6/C9V8RESKGs5gGdSBStboQ+e/tTuw= Received: (qmail 32985 invoked by alias); 5 Feb 2018 13:27:54 -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 32769 invoked by uid 89); 5 Feb 2018 13:27:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.7 required=5.0 tests=AWL, 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-f193.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=8wnpdPwrnzEzLoN4Ilk9UZN+3OjwSpuIlTmotSHiRWE=; b=kRpIlMZPbvaB3fDV8E3h5dbY4HQtO/OODS0KWnoL35Jj9yDF19kUypSFDvZknwFF0C 84c51mayN4DVCXd99CqtNWk+zXsdQGQNdoU2CqYJVUaHrR3fu/+d2QYCrWermj+QP0Sc +JD/vCaushfSlCfcDegMjIWU03psfXcNVTK/3Q5GPYIqYQFQxp/JbZ13zL453U837n8s dzBAOfDclyvE3m2wKgq8Zefvfs9QsHtHbVPvRzGLQAsImy2qjQsteBWARnkocqmVvFHD q8+BGbQJiZMsfJdi/Hn5YaPaf9PWXOPQ/5arqSl6dWAbhWGAl3YLmLkQtDjLIw6qsiDh XgsQ== X-Gm-Message-State: AKwxytdhHQAtroaHFo2htCpTQfWRbGmcnBECClTcBRPZzqo+Bi66+7/A NcbmNQwrY2bX4b6M5qyD06CqQ3WjV20= X-Received: by 10.237.41.163 with SMTP id o32mr80498571qtd.80.1517837268721; Mon, 05 Feb 2018 05:27:48 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH v2 05/12] posix: Use dynarray for globname in glob Date: Mon, 5 Feb 2018 11:27:27 -0200 Message-Id: <1517837254-19399-6-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1517837254-19399-1-git-send-email-adhemerval.zanella@linaro.org> References: <1517837254-19399-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_in_dir): Use dynarray for globnames. --- ChangeLog | 2 + posix/glob.c | 127 +++++++++++++++++------------------------------------------ 2 files changed, 39 insertions(+), 90 deletions(-) -- 2.7.4 diff --git a/posix/glob.c b/posix/glob.c index 26af331..2632e93 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -1189,6 +1189,21 @@ 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. The GLOB_NOSORT bit in FLAGS is ignored. No sorting is ever done. @@ -1199,25 +1214,13 @@ glob_in_dir (const char *pattern, const char *directory, int flags, glob_t *pglob, size_t alloca_used) { void *stream = NULL; -# define GLOBNAMES_MEMBERS(nnames) \ - struct globnames *next; size_t count; char *name[nnames]; - struct globnames { GLOBNAMES_MEMBERS (FLEXIBLE_ARRAY_MEMBER) }; - struct { GLOBNAMES_MEMBERS (64) } init_names_buf; - struct globnames *init_names = (struct globnames *) &init_names_buf; - 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_buf; - - init_names->next = NULL; - init_names->count = ((sizeof init_names_buf - - offsetof (struct globnames, name)) - / sizeof init_names->name[0]); + globnames_array_init (&globnames); meta = __glob_pattern_type (pattern, !(flags & GLOB_NOESCAPE)); if (meta == GLOBPAT_NONE && (flags & (GLOB_NOCHECK|GLOB_NOMAGIC))) @@ -1294,34 +1297,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 nameoff = offsetof (struct globnames, name); - size_t size = FLEXSIZEOF (struct globnames, name, - count * sizeof (char *)); - if ((SIZE_MAX - nameoff) / 2 / sizeof (char *) - < names->count) - goto memory_error; - 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) + globnames_array_add (&globnames, strdup (d.name)); + if (globnames_array_has_failed (&globnames)) goto memory_error; + nfound++; } } } @@ -1331,10 +1310,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; @@ -1355,59 +1337,24 @@ glob_in_dir (const char *pattern, const char *directory, int flags, if (new_gl_pathv == NULL) { memory_error: - while (1) - { - struct globnames *old = names; - for (size_t 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; - for (size_t 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); + = ret.names[i]; + pglob->gl_pathv = new_gl_pathv; + 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); } }