Commit 244b96b
committed
Fix crash on redefining array in subshell (re: 92f65cb)
The referenced commit left one test unexecuted because it crashes.
Minimal reproducer:
typeset -a arr=((a b c) 1)
got=$( typeset -a arr=( ( ((a b c)1))) )
The crash occurs when the array is redefined in a subshell.
Here are abridged ASan stack traces for the crash, for the use
after free, and for when it was freed:
=================================================================
==73147==ERROR: AddressSanitizer: heap-use-after-free [snippage]
READ of size 8 at 0x000107403eb0 thread T0
#0 0x104fded40 in nv_search nvdisc.c:1007
ksh-community#1 0x104fbeb1c in nv_create name.c:860
ksh-community#2 0x104fb8b9c in nv_open name.c:1440
ksh-community#3 0x104fb1edc in nv_setlist name.c:309
ksh-community#4 0x104fb4a30 in nv_setlist name.c:475
ksh-community#5 0x105055b58 in sh_exec xec.c:1079
ksh-community#6 0x105045cd4 in sh_subshell subshell.c:654
ksh-community#7 0x104f92c1c in comsubst macro.c:2266
[snippage]
0x000107403eb0 is located 0 bytes inside of 80-byte region [snippage]
freed by thread T0 here:
#0 0x105c5ade4 in wrap_free+0x98 (libclang_rt.asan_osx_dynamic.dylib:arm64e+0x3ede4)
ksh-community#1 0x105261da0 in dtclose dtclose.c:52
ksh-community#2 0x104f178cc in array_putval array.c:671
ksh-community#3 0x104fd7f4c in nv_putv nvdisc.c:144
ksh-community#4 0x104fbc5f0 in _nv_unset name.c:2435
ksh-community#5 0x104fb3250 in nv_setlist name.c:364
ksh-community#6 0x105055b58 in sh_exec xec.c:1079
ksh-community#7 0x105045cd4 in sh_subshell subshell.c:654
ksh-community#8 0x104f92c1c in comsubst macro.c:2266
[snippage]
So the crash is caused because array_putval (array.c:671) calls
dtclose, freeing ap->table, which is then reused after a recursive
nv_setlist call via nv_open() -> nv_create() -> nv_search().
This only happens whwn we're in a virtual subshell.
src/cmd/ksh93/sh/array.c:
- array_putval(): When redefining an array in a virtual subshell,
do not free the old ap->table; it will be needed by the parent
shell environment.1 parent 50c1b79 commit 244b96b
3 files changed
+8
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
10 | 13 | | |
11 | 14 | | |
12 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
665 | 665 | | |
666 | 666 | | |
667 | 667 | | |
668 | | - | |
| 668 | + | |
669 | 669 | | |
670 | 670 | | |
671 | 671 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
1003 | 1003 | | |
1004 | 1004 | | |
1005 | 1005 | | |
1006 | | - | |
1007 | 1006 | | |
1008 | 1007 | | |
1009 | 1008 | | |
| |||
1018 | 1017 | | |
1019 | 1018 | | |
1020 | 1019 | | |
1021 | | - | |
| 1020 | + | |
1022 | 1021 | | |
1023 | 1022 | | |
1024 | | - | |
1025 | | - | |
1026 | | - | |
1027 | | - | |
1028 | | - | |
| 1023 | + | |
| 1024 | + | |
1029 | 1025 | | |
1030 | 1026 | | |
1031 | 1027 | | |
| |||
0 commit comments