mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-03-09 15:50:02 +00:00
Fix unsetting array element after expanding array subscript range
Simple reproducer:
set -A arr a b c d; : ${arr[1..2]}; unset arr[1]; echo ${arr[@]}
Output:
a
Expected output:
a c d
The ${arr[1..2]} expansion broke the subsequent 'unset' command
so that it unsets element 1 and on, instead of only 1.
This regression was introduced in nv_endsubscript() on 2009-07-31:
c47896b4/src/cmd/ksh93/sh/array.c
That change checks for the ARRAY_SCAN attribute which enables
processing ranges of array elements instead of single array
elements, and restores it after. That restore is evidently not
correct as it causes the subsequent unset command to malfunction.
If we revert that change, the bug disappears and the regression
tests show no failures. However, I don't know what this was meant
to accomplish and what other bug we might introduce by reverting
this. However, no corresponding regression test was added along
with the 2009-07-31 change, nor is there any corresponding message
in the changelog. So this looks to be one of those mystery changes
that we'll never know the reason for.
Since we currently have proof that this change causes breakage and
no evidence that it fixes anything, I'll go ahead and revert it
(and add a regression test, of course). If that causes another
regression, hopefully someone will find it at some point.
src/cmd/ksh93/sh/array.c: nv_endsubscript():
- Revert the 2009-07-31 change that saves/restores the ARRAY_SCAN
attribute.
- Keep the 'ap' pointer as it is now used by newer code. Move the
declaration up to the beginning of the block, as is customary.
src/cmd/ksh93/sh/init.c:
- Cosmetic change: remove an unused array_scan() macro that I found
when grepping the code for ARRAY_SCAN. The macro was introduced
in version 2001-06-01 but the code that used it was replaced in
version 2001-07-04, without removing the macro itself.
Resolves: https://github.com/ksh93/ksh/issues/254
This commit is contained in:
parent
56b530c433
commit
db2b1affdf
5 changed files with 27 additions and 10 deletions
|
@ -709,5 +709,24 @@ actual="$(typeset -p foo)"
|
|||
# $expect and $actual are quoted intentionally
|
||||
[[ "$expect" == "$actual" ]] || err_exit "Multidimensional associative arrays are created with an extra array member (expected $expect, got $actual)"
|
||||
|
||||
# ======
|
||||
# Unsetting an array element removed all following elements if after subscript range expansion
|
||||
# https://github.com/ksh93/ksh/issues/254
|
||||
for range in \
|
||||
0..3 1..3 0..2 0..1 1..2 2..3 0..-1 0..-2 \
|
||||
.. 0.. 1.. 2.. 3.. ..0 ..1 ..2 ..3 \
|
||||
-4.. -3.. -2.. -1.. ..-4 ..-3 ..-2 ..-1 \
|
||||
-4..-1 -3..-1 -2..-1 -1..-1
|
||||
do
|
||||
unset foo
|
||||
set -A foo a b c d e f
|
||||
eval "true \${foo[$range]}"
|
||||
unset foo[2] # remove 3rd element 'c'
|
||||
exp="a b d e f"
|
||||
got=${foo[@]}
|
||||
[[ $got == "$exp" ]] || err_exit "Expanding indexed array range \${foo[$range]} breaks 'unset foo[2]'" \
|
||||
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
||||
done
|
||||
|
||||
# ======
|
||||
exit $((Errors<125?Errors:125))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue