Skip to content

Commit

Permalink
[osh] Fix behavior of unquoted ${*:-} and ${a[*]:-}
Browse files Browse the repository at this point in the history
IFS is only used to join if the var sub is quoted.

This fixes two cases in spec/var-op-test, written by Koichi.
  • Loading branch information
Andy C committed Jan 3, 2025
1 parent c326e7c commit 31ce9f6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
20 changes: 8 additions & 12 deletions osh/word_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ def _MakeWordFrames(part_vals):
if s is None:
continue # ignore undefined array entries

# Arrays parts are always quoted; otherwise they would have decayed to
# a string.
# Arrays parts are always quoted; otherwise they would have
# decayed to a string.
piece = Piece(s, True, False)
if is_first:
current.append(piece)
Expand Down Expand Up @@ -652,8 +652,8 @@ def _ApplyTestOp(
${a:?error} returns error word?
${a:=} returns part_value[] but also needs self.mem for side effects.
So I guess it should return part_value[], and then a flag for raising an
error, and then a flag for assigning it?
So I guess it should return part_value[], and then a flag for raising
an error, and then a flag for assigning it?
The original BracedVarSub will have the name.
Example of needing multiple part_value[]
Expand Down Expand Up @@ -697,16 +697,12 @@ def _ApplyTestOp(

if tok.id in (Id.VTest_ColonHyphen, Id.VTest_ColonEquals,
Id.VTest_ColonQMark, Id.VTest_ColonPlus):
# The first character of IFS is used as a separator only
# for the double-quoted "$*", or otherwise, a space " " is
# used (for $*, $@, and "$@").
# TODO: We current do not check whether the current $* is
# double-quoted or not. We should use IFS only when $* is
# double-quoted.
if vsub_state.join_array:
# "$*" - the separator is the first character of IFS
# $* $@ "$@" - the separator is a space
if quoted and vsub_state.join_array:
sep_width = len(self.splitter.GetJoinChar())
else:
sep_width = 1 # we use ' ' for a[@]
sep_width = 1

# We test whether the joined string will be empty. When
# the separator is empty, all the elements need to be
Expand Down
2 changes: 1 addition & 1 deletion spec/var-op-test.test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## compare_shells: bash dash mksh zsh
## oils_failures_allowed: 2
## oils_failures_allowed: 0

#### Lazy Evaluation of Alternative
i=0
Expand Down

0 comments on commit 31ce9f6

Please sign in to comment.