Skip to content

Single quotes should be literal in subst part of "${var-subst}"#176

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

Single quotes should be literal in subst part of "${var-subst}"#176
magicant merged 2 commits into
trunkfrom
sq-param-dq

Conversation

@magicant

@magicant magicant commented Jun 6, 2025

Copy link
Copy Markdown
Owner

This pull request addresses a bug related to the handling of single quotes in parameter expansions within double-quoted contexts. It also includes updates to the parser logic and corresponding test cases to ensure the issue is resolved and behaves as expected.

Bug Fixes:

  • Fixed a bug where single quotes in the word of parameter expansions (${foo+word}, ${foo-word}, ${foo=word}, ${foo?word}) were incorrectly treated as quoting characters when the expansion was inside double quotes. (NEWS, NEWS.ja) [1] [2]

Parser Updates:

  • Updated the parse_word function to include a new parameter allow_single_quotes and renamed the testfunc parameter to is_delimiter for clarity. (parser.c) [1] [2]
  • Modified the parse_paramexp_in_brace function to accept an indq parameter, ensuring correct behavior when parsing parameter expansions in double-quoted contexts. (parser.c) [1] [2]
  • Adjusted logic in the parse_name section to handle single quotes correctly based on the allow_single_quotes parameter. (parser.c) [1] [2]

Test Enhancements:

  • Added new test cases to validate the handling of single quotes in parameter expansions within double-quoted contexts for substitution forms ${a+b}, ${a-b}, and ${a=b}. (tests/quote-p.tst) [1] [2] [3]

magicant added 2 commits June 6, 2025 16:14
If a parameter expansion with a PT_MINUS, PT_PLUS, PT_ASSIGN, or
PT_ERROR type is in double quotes, single quotes are not treated as
quoting characters in the substitution word. 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>.

Commit a333044 attempted to fix this, but it only addressed the
expansion behavior. This commit fixes the parsing behavior.

The parser needs to be aware of whether the parameter expansion is in
double quotes or not, so the `parse_paramexp_in_brace` function now
takes an additional `indq` argument to indicate if the expansion is in
double quotes. This flag is passed down to the `parse_word` function,
which then decides whether to allow single quotes in the substitution
word.

Fixes #174
@magicant magicant added this to the 2.60 milestone Jun 6, 2025
@magicant
magicant requested a review from Copilot June 6, 2025 07:20
@magicant magicant self-assigned this Jun 6, 2025
@magicant magicant added the bug Something isn't working label Jun 6, 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

This PR fixes handling of single quotes in the word part of parameter expansions when inside double quotes, updates parser internals to support this behavior, and adds tests and release notes to cover the new behavior.

  • Treat single quotes as literal characters in ${foo+word}, ${foo-word}, ${foo=word}, ${foo?word} when within double quotes
  • Refactored parse_word, parse_paramexp_in_brace, and related logic to introduce allow_single_quotes and rename testfunc to is_delimiter
  • Added new test cases in tests/quote-p.tst and updated NEWS/NEWS.ja documents

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/quote-p.tst Added tests for single-quote literal behavior in substitution
parser.c Introduced allow_single_quotes flag, renamed testfunc param
NEWS Documented the bug fix for single-quote handling in expansions
NEWS.ja Japanese release note updated for the same fix
Comments suppressed due to low confidence (3)

parser.c:1177

  • The doc comment for parse_word is missing a description of the new allow_single_quotes parameter—add a line explaining its effect on parsing single-quote characters.
/* Parses a word at the current position.

NEWS:12

  • The code span for ${foo=word} is missing a leading backtick—wrap it as ${foo=word} for consistent formatting.
${foo=word}`, and `${foo?word}` were treated as quoting characters

NEWS.ja:10

  • The third expansion ${foo=word} is missing its opening backtick—add it to match the others.
`${foo+word}`, `${foo-word}`, ${foo=word}`, and `${foo?word}`

Comment thread tests/quote-p.tst
[a$'\t'b]
[a''c][a'b][a$'\t'b]
__OUT__
#'

Copilot AI Jun 6, 2025

Copy link

Choose a reason for hiding this comment

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

This looks like a stray #' marker in the test file—remove it so that the test harness will parse the closing marker correctly.

Suggested change
#'

Copilot uses AI. Check for mistakes.
Comment thread parser.c
}
goto check_closing_brace;

parse_subst2:

Copilot AI Jun 6, 2025

Copy link

Choose a reason for hiding this comment

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

[nitpick] The label parse_subst2 is not very descriptive—consider renaming it to something like parse_subst_in_double_quotes to clarify its purpose.

Suggested change
parse_subst2:
parse_subst_in_double_quotes:

Copilot uses AI. Check for mistakes.
@magicant
magicant merged commit a9b0fb0 into trunk Jun 7, 2025
5 checks passed
@magicant
magicant deleted the sq-param-dq branch June 7, 2025 01:01
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.

2 participants