From patchwork Tue Nov 21 13:55:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 119357 Delivered-To: patch@linaro.org Received: by 10.80.225.132 with SMTP id k4csp5124980edl; Tue, 21 Nov 2017 05:56:43 -0800 (PST) X-Google-Smtp-Source: AGs4zMbIt1EomZU/oBfakC42w/hqCIL2j1CGoj6nv4RFFvxmI9kScOAjC2r2IdlzMbTAeo08HAXV X-Received: by 10.99.6.23 with SMTP id 23mr16953657pgg.276.1511272603391; Tue, 21 Nov 2017 05:56:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1511272603; cv=none; d=google.com; s=arc-20160816; b=xwqYtbILMZIYUSOsNaQqMyUolLgEHBugbN6BK6+wd0wnX0tlsD7lLDDcvgrOmCVTv1 TArTyGheyhs2WWQ34GmX7zICYJgquzKMAkxKO9juTR2IwnFZL7EJ3PFwPsd0Buv+fc3y uhULHVZ/QaTX8ZVTWY/M3P/CGusxI1fjSob29zqO5weE5n6EODpcWTp6TK0i2HWTvJUJ bs3Hav7Vhni+eW4M1J5wviLMzl6Lq43TT7DcvZdKfQWqVg0M7Coho9RxmPU9WZ1Fl7JH ex4my3i4lNBdGj37lGlV2Oya3+wzCOYxkikbnFqBf1iNAkx4xx0lrMfpSS9wtfU69X33 GRVw== 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=PZcLXXrn4ygnX8WblCf8ReQzmBgLF67j/Z7UtyUJsmA=; b=D2psq/pYYfUeX4u3cAB23HHL0lVtPBeO+tm60PUaFMp/i180zUw86LvimsCN4jgqPq I/v7ThFFXtcetJXXO4UnuAAhbdmtGjj10tV6mFSr6cuHrs1B5klmwTNEQt5HRF6XgBt9 s4l5CFJoDwhUmfGccSQyIaofPUMo7eWIMIqUEfJiUQb9Zslue5PvUzhvlZLfrvuopO5A 0RumjKnkqayPldlwG7eP225BxsS+0fg0u0+nGvcK3ePedFT/WAacwKT607z5+RNPlD/G MFgSbjajcrZkJJQRbO3OAfuDdy+1NKrtVARqVPUcYe9+INu341ZNEq5+rnMy5Gs5/W2/ fNpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=Bf+TP3a4; spf=pass (google.com: domain of libc-alpha-return-87361-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-87361-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 l7si10972355pgn.364.2017.11.21.05.56.43 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Nov 2017 05:56:43 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-return-87361-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=Bf+TP3a4; spf=pass (google.com: domain of libc-alpha-return-87361-patch=linaro.org@sourceware.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=libc-alpha-return-87361-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=NNcef30mncSwXFQdQklzLuXFkmO3Ngs t6lJu9Fub6+Fa30MAwbVkIk8bCR11lVxCD56tzX3RCEZcYcOmodMHLcM16kEp9dK j2XgJnqcn955osETxZ1padnq5pxgbS9eQtY8bjVF+JJ3AaGOi039JpzB1yX5GD8S cp0Ia78iswuY= 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=O4MvwUh7rr7554we8oQBmWCpp4w=; b=Bf+TP 3a42H5NIXVqWrBEKDWV6o2utV6eMB5rXMn9UBdL7W5wthG81TfXdjMJW0/OMmKOv a7gyLL6mzYAaSJfvKuww+Gjraeamp4eGZS4wOdmzj7Sxp5Un4p3DvvmeycBzTs1j l2+zkfkyYCN8476u4UlCxwmDhlRk0ExBUjRaf8= Received: (qmail 44647 invoked by alias); 21 Nov 2017 13:55:48 -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 44595 invoked by uid 89); 21 Nov 2017 13:55:48 -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, KB_WAM_FROM_NAME_SINGLEWORD, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=135459 X-HELO: mail-qk0-f174.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=PZcLXXrn4ygnX8WblCf8ReQzmBgLF67j/Z7UtyUJsmA=; b=Y1/SlEPyt5r2ORjovvzRP1/ZAp6brKHMS6qZpd45WzuoSlfy6vF9sr0QIbrzEskQMn 3Fumzar/hHKkyyTM9mbFL6CO8/LQijWjbIVdI2WORL2uaUyriJxTTFGU9QbQ6jlgddYw zBNhoFLnv26aGXjsuWJnLadvPuK3M/7fwdtAmCuZ/CYfEcM10IM1WADR7lTVVkU9xI2/ yfgg1kXGPDDbcUTQfuiHKSPFJ94VpWd7XEGZxlImLS6I0O6q7Hmmtj5dC5LP0mZBm0KJ /OHlFlTpqLPd6nrCksdCy1ymCn7M5GttsqxZ0bdTGkOrcJdMEVPVVA5NU5iV4LJgJ2iN w/qA== X-Gm-Message-State: AJaThX5IMs04XV4BH1CL99VJNypU7Wt5AeeGzC57iuWpaBPqabTcJiNb 8LykRYKaJjfM9ywzwU6H5n+FwzOQA1Q= X-Received: by 10.55.141.68 with SMTP id p65mr1627034qkd.326.1511272544655; Tue, 21 Nov 2017 05:55:44 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 5/8] posix: Use dynarray for globname in glob Date: Tue, 21 Nov 2017 11:55:27 -0200 Message-Id: <1511272530-10936-6-git-send-email-adhemerval.zanella@linaro.org> In-Reply-To: <1511272530-10936-1-git-send-email-adhemerval.zanella@linaro.org> References: <1511272530-10936-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 c83954d..59baf62 100644 --- a/posix/glob.c +++ b/posix/glob.c @@ -1188,6 +1188,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. @@ -1198,25 +1213,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))) @@ -1293,34 +1296,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++; } } } @@ -1330,10 +1309,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; @@ -1354,59 +1336,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); } }