Skip to content

No tilde expansion in subst in double-quoted parameter#177

Merged
magicant merged 2 commits into
trunkfrom
tilde-param
Jun 7, 2025
Merged

No tilde expansion in subst in double-quoted parameter#177
magicant merged 2 commits into
trunkfrom
tilde-param

Conversation

@magicant

@magicant magicant commented Jun 7, 2025

Copy link
Copy Markdown
Owner

If a parameter expansion with a PT_MINUS, PT_PLUS, PT_ASSIGN, or PT_ERROR type is in double quotes, tilde expansion is not performed on the substitution part of the expansion. This is what is specified in POSIX.1-2024 XCU 2.2.3:

For parameter expansions other than the four varieties that provide
for substring processing, within the string of characters from an
enclosed "${" to the matching '}', the double-quotes within which the
expansion occurs shall preserve the literal value of all characters,
with the exception of the characters double-quote, backquote,
dollar-sign, and backslash.

Fixes #175

See also commit 4eccc94, which addressed a similar issue for single quotes.

This pull request does not address the issue of whether tilde expansion should apply to each colon-separated tilde prefix or only to the initial part of the substitution word in an unquoted parameter expansion in an assignment (e.g., x=${unset-~:~}) because POSIX is unclear on this issue and existing shells behave differently.

Summary by Copilot

This pull request addresses a bug in parameter expansions where tilde expansion was incorrectly performed on the word part of certain expansions, even when enclosed in double quotes. It includes fixes in the core logic of the expand.c file and updates to the test suite to ensure the issue is resolved and properly validated.

Bug Fixes in Parameter Expansions:

  • Tilde Expansion Fix: Updated the logic in expand_param and print_subst_as_error functions to prevent tilde expansion on the word part of ${foo+word}, ${foo-word}, ${foo=word}, and ${foo?word} when the expansion is in double quotes. Introduced tildetype_T to manage tilde behavior. ([[1]](https://github.com/magicant/yash/pull/177/files#diff-4e9f03b0fcb26abe26e8d83441df7ea667ec7e16089a316713421ec74247dce7L80-R81), [[2]](https://github.com/magicant/yash/pull/177/files#diff-4e9f03b0fcb26abe26e8d83441df7ea667ec7e16089a316713421ec74247dce7R882), [[3]](https://github.com/magicant/yash/pull/177/files#diff-4e9f03b0fcb26abe26e8d83441df7ea667ec7e16089a316713421ec74247dce7L893-R895), [[4]](https://github.com/magicant/yash/pull/177/files#diff-4e9f03b0fcb26abe26e8d83441df7ea667ec7e16089a316713421ec74247dce7L916-R918), [[5]](https://github.com/magicant/yash/pull/177/files#diff-4e9f03b0fcb26abe26e8d83441df7ea667ec7e16089a316713421ec74247dce7L941-R943), [[6]](https://github.com/magicant/yash/pull/177/files#diff-4e9f03b0fcb26abe26e8d83441df7ea667ec7e16089a316713421ec74247dce7L1100-R1107))

Documentation Updates:

  • Release Notes: Added entries in NEWS and NEWS.ja files to document the bug fix and clarify the behavior of tilde expansion in parameter substitutions. ([[1]](https://github.com/magicant/yash/pull/177/files#diff-7ee66c4f1536ac84dc5bbff1b8312e2eef24b974b3e48a5c5c2bcfdf2eb8f3ceR14-R17), [[2]](https://github.com/magicant/yash/pull/177/files#diff-b87696c2a5e686064d064bd85872d4d5e797abb0a16fd93f6a21593770d9ec81R12-R14))

Test Suite Enhancements:

  • New Test Cases: Added tests in quote-p.tst to validate the correct handling of tilde expansion in parameter substitutions for ${a+b}, ${a-b}, and ${a=b} forms. These tests ensure the fix is robust and behaves as expected across different scenarios. ([[1]](https://github.com/magicant/yash/pull/177/files#diff-c6be1aff379ddbac283e4d243fc2f29127956a24d54bf15de46d38e1ddbe0485R606-R612), [[2]](https://github.com/magicant/yash/pull/177/files#diff-c6be1aff379ddbac283e4d243fc2f29127956a24d54bf15de46d38e1ddbe0485R633-R639), [[3]](https://github.com/magicant/yash/pull/177/files#diff-c6be1aff379ddbac283e4d243fc2f29127956a24d54bf15de46d38e1ddbe0485R667-R675))

If a parameter expansion with a PT_MINUS, PT_PLUS, PT_ASSIGN, or
PT_ERROR type is in double quotes, tilde expansion is not performed
on the substitution part of the expansion. This is what is specified
in POSIX.1-2024 XCU 2.2.3:

> For parameter expansions other than the four varieties that provide
> for substring processing, within the string of characters from an
> enclosed "${" to the matching '}', the double-quotes within which the
> expansion occurs shall preserve the literal value of all characters,
> with the exception of the characters double-quote, backquote,
> <dollar-sign>, and <backslash>.

Fixes #175

See also commit 4eccc94, which addressed a similar issue for single
quotes.
@magicant magicant added this to the 2.60 milestone Jun 7, 2025
@magicant
magicant requested a review from Copilot June 7, 2025 02:59
@magicant magicant self-assigned this Jun 7, 2025
@magicant magicant added the bug Something isn't working label Jun 7, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

A fix for POSIX-compliant tilde expansion behavior in double-quoted parameter substitutions and corresponding test and documentation updates.

  • Disable tilde expansion in the word part of ${foo+word}, ${foo-word}, ${foo=word}, and ${foo?word} when double-quoted.
  • Update expand_param, expand_single, and print_subst_as_error to respect the new tilde rules.
  • Add tests and update release notes (NEWS, NEWS.ja) to document and verify the change.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
tests/quote-p.tst Added tests (test_oE) covering tilde expansion in double-quoted parameter substitutions
expand.c Introduced substt flag, updated calls to expand_four/expand_single, and modified print_subst_as_error signature
NEWS Documented the tilde expansion bug fix in the English release notes (typo present)
NEWS.ja Documented the tilde expansion change in the Japanese release notes
Comments suppressed due to low confidence (1)

expand.c:1102

  • Add the static keyword to the definition of print_subst_as_error so it matches its forward declaration and retains internal linkage.
void print_subst_as_error(const paramexp_T *p, tildetype_T tilde, quoting_T quoting)

Comment thread NEWS Outdated
@magicant
magicant merged commit 0b3403e into trunk Jun 7, 2025
5 checks passed
@magicant
magicant deleted the tilde-param branch June 7, 2025 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Should tilde expansion happen in "${unset-~}"?

2 participants