fixes for array subscripts and values containing 0x01 characters

This commit is contained in:
Chet Ramey
2021-09-28 15:13:02 -04:00
parent fa1e33671d
commit 70d7c6430d
8 changed files with 198 additions and 23 deletions
+8 -4
View File
@@ -760,8 +760,12 @@ declare -a bug3=([0]="" [1]="5" [2]="" [3]="1" [4]="")
declare -a not_bug=([0]="no" [1]="nulls")
declare -a workaround=([0]="")
declare -a var=([0]=$'\001\001\001\001')
declare -A v2=([$'\001']=$'ab\001c' )
declare -a foo=([0]=$'\001\001\001\001')
declare -a foo=([0]=$'\001\001')
declare -a foo=([0]=$'\001\001')
declare -A foo=([v]=$'\001\001' )
declare -A foo=([v]=$'\001\001' )
declare -A foo=([$'\001']=$'ab\001c' )
declare -a foo=([0]=$'\001\001\001\001')
declare -a foo=([0]=$'\001\001\001\001')
declare -A foo=([v]=$'\001\001\001\001' )
declare -A foo=([v]=$'\001\001\001\001' )
declare -A foo=([$'\001']=$'ab\001c' )
declare -A foo=([$'\001']=$'ab\001c' )
+32
View File
@@ -11,8 +11,16 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Issues with CTLESC characters in array subscripts and values. Bash-5.1 and
# earlier didn't quote them correctly and therefore halved the number of
# CTLESCs.
declare -a var
var=( $'\x01\x01\x01\x01' )
declare -p var
declare -A v2
v2=( $'\x01' ab$'\x01'c )
declare -p v2
pv()
{
@@ -22,6 +30,15 @@ pv()
}
pv
unset -f pv
pv()
{
local -A foo
eval foo=\( "${v2[@]@k}" \)
declare -p foo
}
pv
# these are wrong through bash-5.1; there is a fix tagged for bash-5.2
# when I uncomment that fix, these results will reflect it
@@ -52,3 +69,18 @@ pv4()
declare -p foo
}
pv4
unset -f pv3 pv4
pv3()
{
local -A foo=( $'\x01' "${v2[@]}" )
declare -p foo
}
pv3
pv4()
{
local -A foo=( [$'\x01']="${v2[@]}" )
declare -p foo
}
pv4
+23
View File
@@ -329,3 +329,26 @@ argv[7] = <'foo'>
argv[8] = <'bar'>
declare -A clone=([hello]="world" ["key with spaces"]="value with spaces" [foo]="bar" [one]="1" )
declare -A posparams=([hello]="world" ["key with spaces"]="value with spaces" [foo]="bar" [one]="1" )
declare -A var=([$'\001']=$'\001\001\001\001' )
declare -A v2=([$'\001']=$'\001\001\001\001' )
argv[1] = <^A>
argv[2] = <^A^A^A^A>
declare -A foo=([$'\001']=$'\001\001\001\001' )
declare -A var=([$'\001']=$'\001\001\001\001' )
argv[1] = <^A>
argv[2] = <^A^A^A^A>
declare -A foo=([$'\001']=$'\001\001\001\001' )
declare -A var=([$'\001']=$'\001\001\001\001' )
argv[1] = <^A>
argv[2] = <^A^A^A^A>
declare -A foo=([$'\001']=$'\001\001\001\001' )
declare -a var=([0]=$'\001\001\001\001')
argv[1] = <$'\001\001\001\001'>
declare -a foo=([0]=$'\001\001\001\001')
declare -a var=([0]=$'\001\001\001\001')
argv[1] = <$'\001\001\001\001'>
declare -a foo=([0]=$'\001\001\001\001')
declare -A var=([two]=$'ab\001cd' [one]=$'\001\001\001\001' )
declare -A foo=([two]=$'ab\001cd' [one]=$'\001\001\001\001' )
declare -A foo=([$'\001']=$'ab\001cd' )
declare -A foo=([$'\001']=$'\001\001\001\001' )
+3
View File
@@ -253,3 +253,6 @@ ${THIS_SH} ./assoc13.sub
# tests of the @k transformation on associative arrays
${THIS_SH} ./assoc14.sub
# tests with subscripts and values containing 0x01 (some indexed array tests too)
${THIS_SH} ./assoc15.sub
+92
View File
@@ -0,0 +1,92 @@
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# associative arrays first
v=$'\x01'
declare -A var foo v2
var=( $'\x01' $'\x01\x01\x01\x01' )
declare -p var
v2=( $v $v$v$v$v )
declare -p v2
recho "${var[@]@k}"
eval foo=\( "${var[@]@k}" \)
declare -p foo
var=(   )
declare -p var
recho "${var[@]@k}"
eval foo=\( "${var[@]@k}" \)
declare -p foo
var=( []= )
declare -p var
recho "${var[@]@k}"
eval foo=\( "${var[@]@k}" \)
declare -p foo
# now indexed arrays
unset -v var foo
declare -a var
var=( [0]= )
declare -p var
declare -a foo
recho "${var[@]@Q}"
eval foo=\( "${var[@]@Q}" \)
declare -p foo
var=(  )
declare -p var
unset foo
declare -a foo
recho "${var[@]@Q}"
eval foo=\( "${var[@]@Q}" \)
declare -p foo
# similar to array29.sub
unset -v var foo v2
declare -A var
var=( one $'\x01\x01\x01\x01' two ab$'\001'cd )
declare -p var
pv()
{
local -A foo
eval foo=\( "${var[@]@k}" \)
declare -p foo
}
pv
pv1()
{
local -A foo=( $'\x01' "${var[two]}" )
declare -p foo
}
pv1
pv2()
{
local -A foo=( [$'\x01']="${var[one]}" )
declare -p foo
}
pv2