From patchwork Fri Sep 11 16:59:01 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 143490 X-Patchwork-Delegate: Wilco.Dijkstra@arm.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 08E7648FBCB8 for ; Fri, 11 Sep 2026 17:00:15 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by sourceware.org (Postfix) with ESMTPS id A85C748F536B for ; Fri, 11 Sep 2026 17:00:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A85C748F536B Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A85C748F536B Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::730 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789146001; cv=none; b=dW6LjsNjoz472Fp+5f0GH+8uYoCnyEtNpXzc6PBl9l/wdkYXs4+vjr2ladOGOisLkFoVI/SuT8lnV1/G3CKbtAGhdvWIX8EJu7ZL6tym1QnmlIzmbHevTCrFo+RtdeYIeYzJCqJrt/0sPzwGSL07l96Dp4lYABOIeKrF7Rnokk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1789146001; c=relaxed/simple; bh=Q8Bkqk1b9h/Hc8ggWH2LXXATSvGg+PvZYIW4h2jQHkg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=hl62jBbwr8ZKMSUyx8CujuAiPbpoI+KbjuL5KJ9ztzi5FeYnvYZwd3rEvmMGgcMAmmZKN3oSZ7qp/ftC1QU5Z+EdVdO4BWKOSTotCGn4/HF7zgHJ+n57K6n0xRmOyt6UCpuNBeKjKyJGwFcgiLqt22Rx/0OO14ei1wGqD2h8knU= ARC-Authentication-Results: i=1; sourceware.org Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-9399daa3c8eso92648385a.1 for ; Fri, 11 Sep 2026 10:00:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1789146001; x=1789750801; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=kuS03tPIeYYdJy7GVCF/9aWdHIWL8LHr2/f7OoQTylA=; b=fdx74zSSidxMVTWgaw8XgNwvv5BYZEh/4QUXcgzTmtbFheVHG/x/Jobp87NGuHRncC D1oHq/3N3CXnyTQipLw0gvKmz92e1wQRW4p/Y8M9uvBiQF1lDExTrtwAb7QOT34/Xbom Kpo4yed8cnwqL2mWQvcZiQEYuk1+/FrgJXZzTXmMWSPndPTJ0qMkuAAGL2P+XK0Bb1AT rHYYBa2mECBGQDJJl2S7EioZZtWblnMuwqumfuErtIeni6dPWqMP9zm3Z3AdYG99kaoA jIFg193MqIezbFtxor+GU/LY9n1kgjZMLF+z8ZmdLEy+GbQvWBJksSbaaUgcOd5gjid6 TuxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789146001; x=1789750801; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=kuS03tPIeYYdJy7GVCF/9aWdHIWL8LHr2/f7OoQTylA=; b=d7bdCaWS9oC6hOpYeYi1fanwXV4fqCPxDsD2ll8oE60eH1HX8xkkOpkr9f0vNOVxXm 5TK0/w4S4TPDyhxpnZAKsUJOTWUqAFdEnP7r2t7gdimX4eshxyKkkxXwv0RZ1JNpOvvY 6hCUP4InUwHlCFxOoH6jhCwjR8cRrAxrxrQY87gkPIdYoUtxZTj40pPU+ZAapiybezJf lHONobXafS7sIicNQmhYbcLo7dVBcKcaZdrCm7dn7V8amCFEzZz8BbjGqye58EThAnbj G0l8dp+Ye8y6eELqwkekSBOK7INruxSqRmwX5ujYwdXkAwU15s35UNQrvVHSNcbl0it8 xjsw== X-Gm-Message-State: AFuF++k9115s04k6syYOBd3qgKtL4w2meQZaQ+8RnRl5rmsxDbr7+fzx kYrsWxrgvYryLv6LuFoWrIUVBQFg5WKI61euSZhuFXkGjZr9z95WsRv4WJZKguPLgyjOxgWydmd qyaap X-Gm-Gg: AYBFou0NwQ2KmENGVonFWPI38809BK8MBZm/cA12Uwl+icDNI3Op3L5e2mmFPm/fRdP TUPbOqKMcvabkyJehB18dPiMGXjYOFw6HAEjrRXpZvhFlGrCAewkiPDI+3tLkV5juri0YUcKC9Q 2K7rermXQWmt1lxLGpv57QDAlBARdGeTl66HjZZbukcLdxOU/LURXWBcHfP/B5GdFJhiZisEc2Z JjZz7Ouc+jyzlIPD5OCVlMqkPq2jsSeM6VBSjLHx6L1vQxqNb/ITUHHErJzwRXu3e5YVcenfhsZ e0WEBJpdJ0ebrag07lyHojnCw4LH/5l85f1HV0/vNrON+qVuy+YnrIx4NTjNdT4k+SVEMpZCJYv g1rfRHIVVGsRK6iveiiCU8y0IyVP5ZwvMwT3xkDpwN08FvA2Z4hN/kIIjdA3XsH9BCaX4ThfgdG HPkcT28/ht3XiPsu424DqK/4L7PgMruh2go+I0QV4voWIPMhHrA1xxvr8anzOoZpBJNZK/Dse/Q +wmljG1+k/qEYSHhqrrOrgC4Pxyr9RmAtmgBkOw X-Received: by 2002:a05:620a:6601:b0:936:bc78:791b with SMTP id af79cd13be357-939ea06d2f2mr623860885a.15.1789146000732; Fri, 11 Sep 2026 10:00:00 -0700 (PDT) Received: from mandiga ([2804:1b3:a7c3:566e:1ba8:8d15:47b2:9eb]) by smtp.gmail.com with ESMTPSA id af79cd13be357-939f1dd527asm159040985a.16.2026.09.11.09.59.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 10:00:00 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Shamil Abdulaev Subject: [PATCH 3/3] posix: Fix wordexp word delimitation for empty expansions Date: Fri, 11 Sep 2026 13:59:01 -0300 Message-ID: <20260911165932.1737113-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260911165932.1737113-1-adhemerval.zanella@linaro.org> References: <20260911165932.1737113-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org wordexp added an empty word to the list as soon as a quoted section of the input expanded to nothing, without waiting for the word to be delimited. A quoted section is only part of the word it appears in, so the empty word must not be produced when something else follows: wordexp ("\"\"x", &we, 0) gave the two words "" and "x" wordexp ("''x", &we, 0) likewise wordexp ("\"$var\"x", &we, 0) likewise, with var set and null wordexp ("\"\"$(echo a b)") gave three words, "", "a" and "b" The tilde expansion is also not subject to field splitting, so "~" expands to a single empty field when HOME is set and empty, rather than to no field at all. Replace the immediate w_addword with a word_started flag that records that the current word has to be added to the list even if it is still empty, and consume it where the word is delimited. Checked on x86_64-linux-gnu and i686-linux-gnu. --- posix/tst-wordexp-noargs.c | 21 +++++++ posix/wordexp-test.c | 31 ++++++++++ posix/wordexp.c | 123 ++++++++++++++++++++++++++----------- 3 files changed, 138 insertions(+), 37 deletions(-) diff --git a/posix/tst-wordexp-noargs.c b/posix/tst-wordexp-noargs.c index 589028b6767..0fce79525f8 100644 --- a/posix/tst-wordexp-noargs.c +++ b/posix/tst-wordexp-noargs.c @@ -19,6 +19,7 @@ /* The testsuite runs this without positional parameters, so there is nothing for $* and $@ below to expand to. */ +#include #include #include @@ -58,6 +59,26 @@ do_test (void) check_one_field ("${#*}", "0"); check_one_field ("${#@}", "0"); + /* "$*" is not subject to field splitting, so it expands to a single + null string. */ + check_one_field ("\"$*\"", ""); + + /* POSIX requires "$@" to generate zero fields, even though it is + double-quoted. */ + check_no_field ("\"$@\""); + + setenv ("var", "", 1); + + check_no_field ("\"$@$@\""); + check_no_field ("\"$@\"\"$@\""); + check_one_field ("\"\"", ""); + check_one_field ("\"\"\"$@\"", ""); + check_one_field ("\"$@\"\"\"", ""); + check_one_field ("\"$var$@\"", ""); + check_one_field ("\"$@$var\"", ""); + + unsetenv ("var"); + return 0; } diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c index 6c10bd3458a..5f7740cb76f 100644 --- a/posix/wordexp-test.c +++ b/posix/wordexp-test.c @@ -105,6 +105,22 @@ struct test_case_struct { 0, "", "x\"$var\"", 0, 1, { "x", }, IFS }, { 0, "", "\"a${var}b\"", 0, 1, { "ab", }, IFS }, { 0, "", "one \"$var\" two", 0, 3, { "one", "", "two", }, IFS }, + + /* A quoted section is part of the word it appears in, so it only produces + a field of its own when it is the whole word. */ + { 0, NULL, "\"\"x", 0, 1, { "x", }, IFS }, + { 0, NULL, "''x", 0, 1, { "x", }, IFS }, + { 0, "", "\"$var\"x", 0, 1, { "x", }, IFS }, + { 0, NULL, "\"\"$(echo one two)", 0, 2, { "one", "two", }, IFS }, + + /* A quoted null merges into the first field of what follows, unless that + field is empty because the expansion starts with a separator. In this + case the quoted null is a field of its own. */ + { 0, "b", "\"\"$var", 0, 1, { "b", }, IFS }, + { 0, " b", "\"\"$var", 0, 2, { "", "b", }, IFS }, + { 0, "", "\"\"$var", 0, 1, { "", }, IFS }, + { 0, " ", "\"\"$var", 0, 1, { "", }, IFS }, + { 0, "a b", "\"\"$var", 0, 2, { "a", "b", }, IFS }, { 0, NULL, "explicit ``", 0, 1, { "explicit", }, IFS }, /* Simple command substitution */ @@ -320,6 +336,21 @@ do_test (int argc, char *argv[]) TEST_COMPARE (testit (&ts), 0); + /* "~" is not subject to field splitting, so it still expands to a + single (empty) field when HOME is set but empty. */ + + setenv ("HOME", "", 1); + ts.retval = 0; + ts.env = NULL; + ts.words = "~ ~/foo"; + ts.flags = 0; + ts.wordc = 2; + ts.wordv[0] = ""; + ts.wordv[1] = "/foo"; + ts.ifs = IFS; + + TEST_COMPARE (testit (&ts), 0); + /* "~" expands to home dir from passwd file if HOME is not set */ pw = getpwuid (getuid ()); diff --git a/posix/wordexp.c b/posix/wordexp.c index 7741a85c23a..6d32678ec6b 100644 --- a/posix/wordexp.c +++ b/posix/wordexp.c @@ -48,7 +48,7 @@ extern char **__libc_argv attribute_hidden; static int parse_dollars (char **word, size_t *word_length, size_t *max_length, const char *words, size_t *offset, int flags, wordexp_t *pwordexp, const char *ifs, - const char *ifs_white, int quoted); + const char *ifs_white, int quoted, bool *started); static int parse_backtick (char **word, size_t *word_length, size_t *max_length, const char *words, size_t *offset, int flags, wordexp_t *pwordexp, @@ -56,7 +56,7 @@ static int parse_backtick (char **word, size_t *word_length, static int parse_dquote (char **word, size_t *word_length, size_t *max_length, const char *words, size_t *offset, int flags, wordexp_t *pwordexp, const char *ifs, - const char *ifs_white); + const char *ifs_white, bool *started); static int eval_expr (char *expr, long int *result); /* The w_*() functions manipulate word lists. */ @@ -499,7 +499,7 @@ parse_glob (char **word, size_t *word_length, size_t *max_length, { error = parse_dollars (word, word_length, max_length, words, offset, flags, &glob_list, ifs, ifs_white, - quoted == 2); + quoted == 2, NULL); if (error) goto tidy_up; @@ -690,7 +690,8 @@ parse_arith (char **word, size_t *word_length, size_t *max_length, { case '$': error = parse_dollars (&expr, &expr_length, &expr_maxlen, - words, offset, flags, NULL, NULL, NULL, 1); + words, offset, flags, NULL, NULL, NULL, 1, + NULL); /* The ``1'' here is to tell parse_dollars not to * split the fields. */ @@ -1197,7 +1198,7 @@ parse_comm (char **word, size_t *word_length, size_t *max_length, static int parse_param (char **word, size_t *word_length, size_t *max_length, const char *words, size_t *offset, int flags, wordexp_t *pwordexp, - const char *ifs, const char *ifs_white, int quoted) + const char *ifs, const char *ifs_white, int quoted, bool *started) { /* We are poised just after "$" */ enum action @@ -1511,6 +1512,11 @@ envsubst: } else { + /* POSIX says that "$@" generates zero fields when there are no + positional parameters. */ + if (started != NULL) + *started = false; + free (env); free (pattern); return 0; @@ -1620,7 +1626,8 @@ envsubst: case '$': offset = 0; error = parse_dollars (&expanded, &exp_len, &exp_maxl, p, - &offset, flags, NULL, NULL, NULL, 1); + &offset, flags, NULL, NULL, NULL, 1, + NULL); if (error) { if (free_value) @@ -1947,9 +1954,11 @@ envsubst: { char *field_end = field_begin; char *next_field; + bool first_field = field_begin == value_copy; + size_t white; /* If this isn't the first field, start a new word */ - if (field_begin != value_copy) + if (!first_field) { if (w_addword (pwordexp, *word) == WRDE_NOSPACE) { @@ -1958,10 +1967,28 @@ envsubst: } *word = w_newword (word_length, max_length); + if (started != NULL) + *started = false; } /* Skip IFS whitespace before the field */ - field_begin += strspn (field_begin, ifs_white); + white = strspn (field_begin, ifs_white); + field_begin += white; + + /* A quoted null right before the expansion is a field of its own + when the expansion starts with a field separator. */ + if (first_field && white != 0 && *word == NULL + && (started != NULL && *started)) + { + if (w_addword (pwordexp, *word) == WRDE_NOSPACE) + { + free (value_copy); + goto no_space; + } + + *word = w_newword (word_length, max_length); + *started = false; + } if (!seen_nonws_ifs && *field_begin == 0) /* Nothing but whitespace */ @@ -2028,7 +2055,7 @@ static int parse_dollars (char **word, size_t *word_length, size_t *max_length, const char *words, size_t *offset, int flags, wordexp_t *pwordexp, const char *ifs, const char *ifs_white, - int quoted) + int quoted, bool *started) { /* We are poised _at_ "$" */ switch (words[1 + *offset]) @@ -2078,7 +2105,7 @@ parse_dollars (char **word, size_t *word_length, size_t *max_length, default: ++(*offset); /* parse_param needs to know if "{" is there */ return parse_param (word, word_length, max_length, words, offset, flags, - pwordexp, ifs, ifs_white, quoted); + pwordexp, ifs, ifs_white, quoted, started); } } @@ -2149,26 +2176,42 @@ parse_backtick (char **word, size_t *word_length, size_t *max_length, static int parse_dquote (char **word, size_t *word_length, size_t *max_length, const char *words, size_t *offset, int flags, - wordexp_t *pwordexp, const char * ifs, const char * ifs_white) + wordexp_t *pwordexp, const char * ifs, const char * ifs_white, + bool *started) { /* We are poised just after a double-quote */ int error; + /* Every element of the quoted section produces a field of its own, even + an empty one. The exception is "$@" that has no positional parameters + to expand to. */ + bool any = false; + bool contributes = false; for (; words[*offset]; ++(*offset)) { switch (words[*offset]) { case '"': + if (!any || contributes) + *started = true; return 0; case '$': - error = parse_dollars (word, word_length, max_length, words, offset, - flags, pwordexp, ifs, ifs_white, 1); - /* The ``1'' here is to tell parse_dollars not to - * split the fields. It may need to, however ("$@"). - */ - if (error) - return error; + { + bool dollar_started = true; + + error = parse_dollars (word, word_length, max_length, words, + offset, flags, pwordexp, ifs, ifs_white, 1, + &dollar_started); + /* The ``1'' here is to tell parse_dollars not to + * split the fields. It may need to, however ("$@"). + */ + if (error) + return error; + + any = true; + contributes |= dollar_started; + } break; @@ -2182,6 +2225,7 @@ parse_dquote (char **word, size_t *word_length, size_t *max_length, if (error) return error; + any = contributes = true; break; case '\\': @@ -2191,12 +2235,15 @@ parse_dquote (char **word, size_t *word_length, size_t *max_length, if (error) return error; + any = contributes = true; break; default: *word = w_addchar (*word, word_length, max_length, words[*offset]); if (*word == NULL) return WRDE_NOSPACE; + + any = contributes = true; } } @@ -2237,6 +2284,8 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags) size_t word_length; size_t max_length; char *word = w_newword (&word_length, &max_length); + bool word_started = false; + size_t started_wordc = 0; int error; char *ifs; char ifs_white[4]; @@ -2346,7 +2395,7 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags) case '$': error = parse_dollars (&word, &word_length, &max_length, words, &words_offset, flags, pwordexp, ifs, ifs_white, - 0); + 0, &word_started); if (error) goto do_error; @@ -2366,41 +2415,38 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags) case '"': ++words_offset; + /* parse_dquote sets WORD_STARTED unless the whole quoted section + was a "$@" without positional parameters, which produces no + field at all. */ error = parse_dquote (&word, &word_length, &max_length, words, - &words_offset, flags, pwordexp, ifs, ifs_white); + &words_offset, flags, pwordexp, ifs, ifs_white, + &word_started); if (error) goto do_error; - if (!word_length) - { - error = w_addword (pwordexp, NULL); - - if (error) - goto do_error; - } + if (word_started) + started_wordc = pwordexp->we_wordc; break; case '\'': ++words_offset; + word_started = true; + started_wordc = pwordexp->we_wordc; error = parse_squote (&word, &word_length, &max_length, words, &words_offset); if (error) goto do_error; - if (!word_length) - { - error = w_addword (pwordexp, NULL); - - if (error) - goto do_error; - } - break; case '~': + /* Tilde expansion is not subject to field splitting, so the word + exists even if the expansion is empty. */ + word_started = true; + started_wordc = pwordexp->we_wordc; error = parse_tilde (&word, &word_length, &max_length, words, &words_offset, pwordexp->we_wordc); @@ -2447,7 +2493,8 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags) } /* If a word has been delimited, add it to the list. */ - if (word != NULL) + if (word != NULL + || (word_started && started_wordc == pwordexp->we_wordc)) { error = w_addword (pwordexp, word); if (error) @@ -2455,12 +2502,14 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags) } word = w_newword (&word_length, &max_length); + word_started = false; } /* End of string */ /* There was a word separator at the end */ - if (word == NULL) /* i.e. w_newword */ + if (word == NULL + && !(word_started && started_wordc == pwordexp->we_wordc)) { free (saved_wordv); return 0;