Fix assigning array variables in ksh

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Fix assigning array variables in ksh

Vadim Zhukov
Hello all.

This continues the 'Another potential ksh bug?' thread on misc@:
https://marc.info/?l=openbsd-misc&m=160736700220621&w=2
This present is a bit too late for Christmas, but at least the Day of
Red Army is coming soon. I'm sure noone is against this patch then.

The code in typeset() function, which is responsible for almost all
shell variable tweaking, contains a bug: in case of array it passes
"foo=var" instead of only variable name ("foo"), if the value was
ever given. This results in, e.g., a bug reported by Jordan Geoghegan.
Generally speaking, we had creating a (temporary) variable via global()
in vpbase, and of course it didn't get the read-only marker.

This fix is to use 'tvar' variable, which always contains shell
variable name, without value.

As a bonus, this fixes the ifs.t:IFS-null-1 test (previously failing).
I'm too lazy to check why. :-)

Okay anyone?

--
WBR,
  Vadim Zhukov


Index: regress/bin/ksh/obsd-regress.t
===================================================================
RCS file: /cvs/src/regress/bin/ksh/obsd-regress.t,v
retrieving revision 1.10
diff -u -p -r1.10 obsd-regress.t
--- regress/bin/ksh/obsd-regress.t 8 Dec 2018 21:03:51 -0000 1.10
+++ regress/bin/ksh/obsd-regress.t 21 Feb 2021 18:51:54 -0000
@@ -503,3 +503,16 @@ description:
 stdin:
  kill -s SIGINFO $$
 ---
+
+name: overwrite-ro-array
+description:
+ do not allow to override first element of a read-only array
+ via the non-array access.
+stdin:
+ arr[0]=foo
+ readonly arr
+ arr=bar
+expected-exit: e == 1
+expected-stderr-pattern:
+ /: arr: is read only$/
+---
Index: bin/ksh/var.c
===================================================================
RCS file: /cvs/src/bin/ksh/var.c,v
retrieving revision 1.71
diff -u -p -r1.71 var.c
--- bin/ksh/var.c 21 Feb 2020 18:21:23 -0000 1.71
+++ bin/ksh/var.c 21 Feb 2021 18:51:54 -0000
@@ -644,7 +644,7 @@ typeset(const char *var, int set, int cl
     global(tvar);
  set &= ~(LOCAL|LOCAL_COPY);
 
- vpbase = (vp->flag & ARRAY) ? global(arrayname(var)) : vp;
+ vpbase = (vp->flag & ARRAY) ? global(arrayname(tvar)) : vp;
 
  /* only allow export flag to be set.  at&t ksh allows any attribute to
  * be changed, which means it can be truncated or modified