Skip to content

Commit

Permalink
[ysh builtin translation] Avoid crash in promptVal()
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Jan 30, 2024
1 parent cf43b50 commit 7e1d31f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
3 changes: 2 additions & 1 deletion builtin/meta_osh.py
Expand Up @@ -200,7 +200,8 @@ def _PrintFreeForm(row):
if kind == 'file':
what = resolved
elif kind == 'alias':
what = ('an alias for %s' % j8_lite.EncodeString(resolved, unquoted_ok=True))
what = ('an alias for %s' %
j8_lite.EncodeString(resolved, unquoted_ok=True))
else: # builtin, function, keyword
what = 'a shell %s' % kind

Expand Down
7 changes: 7 additions & 0 deletions builtin/method_io.py
Expand Up @@ -3,6 +3,7 @@

from _devbuild.gen.value_asdl import value, value_t

from core import error
from core import vm
from mycpp.mylib import log
from osh import prompt
Expand Down Expand Up @@ -32,5 +33,11 @@ def Call(self, rd):
what = rd.PosStr()
rd.Done() # no more args

# Bug fix: protect against crash later in PromptVal()
if len(what) != 1:
raise error.Expr(
'promptVal() expected a single char, got %r' % what,
rd.LeftParenToken())

prompt_ev = cast(prompt.Evaluator, io.prompt_ev)
return value.Str(prompt_ev.PromptVal(what))
3 changes: 2 additions & 1 deletion core/dev.py
Expand Up @@ -575,7 +575,8 @@ def OnShAssignment(self, lval, op, val, flags, which_scopes):
left = '%s[%d]' % (lval.name, lval.index)
elif case(sh_lvalue_e.Keyed):
lval = cast(sh_lvalue.Keyed, UP_lval)
left = '%s[%s]' % (lval.name, j8_lite.MaybeShellEncode(lval.key))
left = '%s[%s]' % (lval.name, j8_lite.MaybeShellEncode(
lval.key))
buf.write(left)

# Only two possibilities here
Expand Down
3 changes: 1 addition & 2 deletions data_lang/j8_lite.py
Expand Up @@ -4,6 +4,7 @@

import fastfunc # Skip pyj8 and fastlex


def EncodeString(s, unquoted_ok=False):
# type: (str, bool) -> str
"""Convenience API that doesn't require instance of j8.Printer()
Expand Down Expand Up @@ -43,5 +44,3 @@ def YshEncode(s, unquoted_ok=False):
return s

return fastfunc.ShellEncodeString(s, 1) # ysh_fallback


11 changes: 6 additions & 5 deletions frontend/flag_def.py
Expand Up @@ -468,11 +468,12 @@ def _DefineCompletionActions(spec):
# u means I want \u{1234}
# raw is utf-8
if 0:
WRITE_SPEC.LongFlag('--unicode', ['raw', 'u', 'x'],
default='raw',
help='Encode QSN with these options. '
'x assumes an opaque byte string, while raw and u try to '
'decode UTF-8.')
WRITE_SPEC.LongFlag(
'--unicode', ['raw', 'u', 'x'],
default='raw',
help='Encode QSN with these options. '
'x assumes an opaque byte string, while raw and u try to '
'decode UTF-8.')

PUSH_REGISTERS_SPEC = FlagSpec('push-registers')

Expand Down
14 changes: 8 additions & 6 deletions mycpp/NINJA_subgraph.py
Expand Up @@ -163,12 +163,14 @@ def TranslatorSubgraph(ru, translator, ex):
# But don't pass it on the command line.
translator_wrapper = '_bin/shwrap/%s_main' % translator

n.build(raw,
'translate-%s' % translator,
to_translate,
implicit=[translator_wrapper],
# examples/parse uses pyext/fastfunc.pyi
variables=[('mypypath', '$NINJA_REPO_ROOT/mycpp:$NINJA_REPO_ROOT/pyext')])
n.build(
raw,
'translate-%s' % translator,
to_translate,
implicit=[translator_wrapper],
# examples/parse uses pyext/fastfunc.pyi
variables=[('mypypath',
'$NINJA_REPO_ROOT/mycpp:$NINJA_REPO_ROOT/pyext')])

p = 'mycpp/examples/%s_preamble.h' % ex
# Ninja empty string!
Expand Down

0 comments on commit 7e1d31f

Please sign in to comment.