From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 23291 invoked from network); 20 Jan 2022 20:37:38 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 20 Jan 2022 20:37:38 -0000 Received: (qmail 1277 invoked by uid 550); 20 Jan 2022 20:37:35 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 1243 invoked from network); 20 Jan 2022 20:37:35 -0000 From: Ismael Luceno To: musl@lists.openwall.com Cc: Rich Felker , Ismael Luceno Date: Thu, 20 Jan 2022 21:40:39 +0100 Message-Id: <20220120204039.642-1-ismael@iodev.co.uk> X-Mailer: git-send-email 2.33.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [RESEND PATCH v2] glob: introduce context struct for do_glob this reduces the function frame by sharing more state in the recursion, and produces a slightly smaller object file with GCC 10.3 on x86_64: text data bss dec hex filename 2303 0 0 2303 8ff glob-ctx.lo 2356 0 0 2356 934 glob-noctx.lo Signed-off-by: Ismael Luceno --- Notes: Changes since v1: - Make context struct constant src/regex/glob.c | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/src/regex/glob.c b/src/regex/glob.c index 9de080ed9ccd..e00e12e1f811 100644 --- a/src/regex/glob.c +++ b/src/regex/glob.c @@ -32,10 +32,16 @@ static int append(struct match **tail, const char *name, size_t len, int mark) return 0; } -static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (*errfunc)(const char *path, int err), struct match **tail) +struct glob_ctx { + struct match **tail; + int flags; + int (*errfunc)(const char *path, int err); +}; + +static int do_glob(char *buf, size_t pos, int type, char *pat, const struct glob_ctx *restrict ctx) { /* If GLOB_MARK is unused, we don't care about type. */ - if (!type && !(flags & GLOB_MARK)) type = DT_REG; + if (!type && !(ctx->flags & GLOB_MARK)) type = DT_REG; /* Special-case the remaining pattern being all slashes, in * which case we can use caller-passed type if it's a dir. */ @@ -55,7 +61,7 @@ static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (* break; } else if (pat[i] == '[') { in_bracket = 1; - } else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE)) { + } else if (pat[i] == '\\' && !(ctx->flags & GLOB_NOESCAPE)) { /* Backslashes inside a bracket are (at least by * our interpretation) non-special, so if next * char is ']' we have a complete expression. */ @@ -100,23 +106,23 @@ static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (* * or if that fails, use lstat for determining existence to * avoid false negatives in the case of broken symlinks. */ struct stat st; - if ((flags & GLOB_MARK) && (!type||type==DT_LNK) && !stat(buf, &st)) { + if ((ctx->flags & GLOB_MARK) && (!type||type==DT_LNK) && !stat(buf, &st)) { if (S_ISDIR(st.st_mode)) type = DT_DIR; else type = DT_REG; } if (!type && lstat(buf, &st)) { - if (errno!=ENOENT && (errfunc(buf, errno) || (flags & GLOB_ERR))) + if (errno!=ENOENT && (ctx->errfunc(buf, errno) || (ctx->flags & GLOB_ERR))) return GLOB_ABORTED; return 0; } - if (append(tail, buf, pos, (flags & GLOB_MARK) && type==DT_DIR)) + if (append(ctx->tail, buf, pos, (ctx->flags & GLOB_MARK) && type==DT_DIR)) return GLOB_NOSPACE; return 0; } char *p2 = strchr(pat, '/'), saved_sep = '/'; /* Check if the '/' was escaped and, if so, remove the escape char * so that it will not be unpaired when passed to fnmatch. */ - if (p2 && !(flags & GLOB_NOESCAPE)) { + if (p2 && !(ctx->flags & GLOB_NOESCAPE)) { char *p; for (p=p2; p>pat && p[-1]=='\\'; p--); if ((p2-p)%2) { @@ -126,7 +132,7 @@ static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (* } DIR *dir = opendir(pos ? buf : "."); if (!dir) { - if (errfunc(buf, errno) || (flags & GLOB_ERR)) + if (ctx->errfunc(buf, errno) || (ctx->flags & GLOB_ERR)) return GLOB_ABORTED; return 0; } @@ -142,22 +148,22 @@ static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (* if (p2) *p2 = 0; - int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) - | ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0); + int fnm_flags= ((ctx->flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) + | ((!(ctx->flags & GLOB_PERIOD)) ? FNM_PERIOD : 0); if (fnmatch(pat, de->d_name, fnm_flags)) continue; /* With GLOB_PERIOD, don't allow matching . or .. unless * fnmatch would match them with FNM_PERIOD rules in effect. */ - if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.' + if (p2 && (ctx->flags & GLOB_PERIOD) && de->d_name[0]=='.' && (!de->d_name[1] || de->d_name[1]=='.' && !de->d_name[2]) && fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD)) continue; memcpy(buf+pos, de->d_name, l+1); if (p2) *p2 = saved_sep; - int r = do_glob(buf, pos+l, de->d_type, p2 ? p2 : "", flags, errfunc, tail); + int r = do_glob(buf, pos+l, de->d_type, p2 ? p2 : "", ctx); if (r) { closedir(dir); return r; @@ -166,7 +172,7 @@ static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (* int readerr = errno; if (p2) *p2 = saved_sep; closedir(dir); - if (readerr && (errfunc(buf, errno) || (flags & GLOB_ERR))) + if (readerr && (ctx->errfunc(buf, errno) || (ctx->flags & GLOB_ERR))) return GLOB_ABORTED; errno = old_errno; return 0; @@ -248,8 +254,14 @@ int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, i char *s = p; if ((flags & (GLOB_TILDE | GLOB_TILDE_CHECK)) && *p == '~') error = expand_tilde(&s, buf, &pos); - if (!error) - error = do_glob(buf, pos, 0, s, flags, errfunc, &tail); + if (!error) { + const struct glob_ctx ctx = { + .tail = &tail, + .flags = flags, + .errfunc = errfunc, + }; + error = do_glob(buf, pos, 0, s, &ctx); + } free(p); } -- 2.33.0