Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: [PATCH (not final)] (take three?) unset "array[$anything]"



2021-06-02 23:43:50 -0700, Bart Schaefer:
[...]
> > There's also the question of what to do with
> 
> ... a bunch of things that aren't documented to work with associative
> array elements but accidentally do ...
[...]

That's quite a natural thing to want to do though and is
supported by all shells with arrays (except yash it seems).

See for instance CSV processing such as:

typeset -A field
IFS=,
{
  read -rA headers
  (($#headers)) && while read -r 'field['$^headers']'; do
    typeset -p field
    do-something-with "$field[firstname]" "$field[lastname]"
  done
} < file.csv

That code above currently fails if the header contains some
special characters and has a code injection vulnerability (like
if the first line of the csv is $(reboot)), and making read a
keyword wouldn't help.

It can currently be avoided with:

typeset -A field
IFS=,
{
  read -rA headers
  (($#headers)) && while read -r 'field[$headers['{1..$#headers}']]'; do
    typeset -p field
    do-something-with "$field[firstname]" "$field[lastname]"
  done
} < file.csv

-- 
Stephane




Messages sorted by: Reverse Date, Date, Thread, Author