Skip to content

Commit

Permalink
[patsub] Fix crash in ${x/^} and ${x/!}
Browse files Browse the repository at this point in the history
In the glob to ERE converter, we weren't handling the ^ and ! char class
operators outside of char classes.

Addresses issue reported in oils-for-unix#653.
  • Loading branch information
Andy Chu committed Apr 5, 2020
1 parent daab1d6 commit dbf9e00
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
2 changes: 1 addition & 1 deletion frontend/lexer_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ def IsKeyword(name):
C('*', Id.Glob_Star),
C('?', Id.Glob_QMark),

# For negation.
# For negation. Treated as operators inside [], but literals outside.
C('!', Id.Glob_Bang),
C('^', Id.Glob_Caret),

Expand Down
9 changes: 7 additions & 2 deletions osh/glob_.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,12 @@ def _GenerateERE(parts):
out.append('\\')
out.append(c)

elif part.id == Id.Glob_CleanLiterals:
# ! is only for char class
elif part.id in (Id.Glob_CleanLiterals, Id.Glob_Bang):
out.append(part.s) # e.g. 'py' doesn't need to be escaped

elif part.id == Id.Glob_OtherLiteral:
# ^ is only for char class
elif part.id in( Id.Glob_OtherLiteral, Id.Glob_Caret):
assert len(part.s) == 1, part.s
c = part.s
if c in _REGEX_CHARS_TO_ESCAPE:
Expand All @@ -288,6 +290,9 @@ def _GenerateERE(parts):
elif part.id == Id.Glob_BadBackslash:
out.append('\\\\')

elif part.id == Id.Glob_Caret:
out.append('^')

else:
raise AssertionError(part.id)

Expand Down
10 changes: 10 additions & 0 deletions spec/dbracket.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,13 @@ cat $TMP/x.txt
--
STDERR
## END

#### special chars
[[ ^ == ^ ]]
echo caret $?
[[ '!' == ! ]]
echo bang $?
## STDOUT:
caret 0
bang 0
## END
28 changes: 28 additions & 0 deletions spec/var-op-patsub.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,31 @@ _μ_ and _μ_
_μ_ and _μ_
_μ_ and _μ_
## END

#### ${x/^} regression
x=abc
echo ${x/^}
echo ${x/!}

y=^^^
echo ${y/^}
echo ${y/!}

z=!!!
echo ${z/^}
echo ${z/!}

s=a^b!c
echo ${s/a^}
echo ${s/b!}

## STDOUT:
abc
abc
^^
^^^
!!!
!!
b!c
a^c
## END

0 comments on commit dbf9e00

Please sign in to comment.