Skip to content

Commit

Permalink
Fix crash when listing indexed arrays with 'typeset -a'
Browse files Browse the repository at this point in the history
There is a bug in print_scan() function that may cause ksh to crash
while listing indexed arrays. The crash happens in nv_search() when
called from print_scan().

This applies a fix from Siteshwar Vashist:
https://www.mail-archive.com/[email protected]/msg01944.html

src/cmd/ksh93/bltins/typeset.c:
- Call nv_scan() without the NV_IARRAY flag, even for a null scan.

src/cmd/ksh93/tests/arrays.sh:
- Add regression test for 'typeset -a' crash and check output.
  • Loading branch information
McDutchie committed Jul 9, 2020
1 parent a8f6d6b commit 5e7d335
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Any uppercase BUG_* names are modernish shell bug IDs.

- Fixed a crash on syntax error when sourcing/dotting multiple files.

- Fixed a crash when listing indexed arrays.

2020-07-07:

- Four of the date formats accepted by 'printf %()T' have had their
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/ksh93/bltins/typeset.c
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ static void print_scan(Sfio_t *file, int flag, Dt_t *root, int option,struct tda
tp->scanmask |= (NV_DOUBLE|NV_EXPNOTE);
if(flag==NV_LTOU || flag==NV_UTOL)
tp->scanmask |= NV_UTOL|NV_LTOU;
namec = nv_scan(root,nullscan,(void*)tp,tp->scanmask,flag);
namec = nv_scan(root, nullscan, (void*)tp, tp->scanmask, flag&~NV_IARRAY);
argv = tp->argnam = (char**)stkalloc(tp->sh->stk,(namec+1)*sizeof(char*));
namec = nv_scan(root, pushname, (void*)tp, tp->scanmask, flag&~NV_IARRAY);
if(mbcoll())
Expand Down
9 changes: 9 additions & 0 deletions src/cmd/ksh93/tests/arrays.sh
Original file line number Diff line number Diff line change
Expand Up @@ -666,4 +666,13 @@ typeset -A Foo
Foo=( [a]=AA;[b]=BB)
[[ ${Foo[a]} == AA ]] || err_exit 'Fooa[a] is {Foo[a]} not AA'
# ======
# Crash when listing an indexed array
expect=$'A=($\'\\\'\')\nB=(aa)\nC=(aa)'
actual=$("$SHELL" -c 'A[0]="'\''" B[0]=aa C[0]=aa; typeset -a') \
|| err_exit "Crash when listing indexed array (exit status $?)"
[[ $actual == "$expect" ]] || err_exit 'Incorrect output when listing indexed array' \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
# ======
exit $((Errors<125?Errors:125))

0 comments on commit 5e7d335

Please sign in to comment.