290-ash-fix-a-regression-in-handling-local-variables.patch 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. From: Felix Fietkau <nbd@openwrt.org>
  2. Date: Fri, 17 Apr 2015 01:54:51 +0200
  3. Subject: [PATCH] ash: fix a regression in handling local variables
  4. commit 109ee5d33694a03cda3424b4846584250832ba8e
  5. "ash: make "locak VAR" unset VAR (bash does that)"
  6. This commit introduced a regression where calling local on an already
  7. local variable unsets it. This does not match bash behavior.
  8. Update test case to check for this behavior
  9. Signed-off-by: Felix Fietkau <nbd@openwrt.org>
  10. ---
  11. --- a/shell/ash.c
  12. +++ b/shell/ash.c
  13. @@ -8961,6 +8961,21 @@ parse_command_args(char **argv, const ch
  14. }
  15. #endif
  16. +static bool
  17. +findlocal(struct var *vp)
  18. +{
  19. + struct localvar *lvp = localvars;
  20. +
  21. + while (lvp) {
  22. + if (lvp->vp == vp)
  23. + return true;
  24. +
  25. + lvp = lvp->next;
  26. + }
  27. +
  28. + return false;
  29. +}
  30. +
  31. /*
  32. * Make a variable a local variable. When a variable is made local, it's
  33. * value and flags are saved in a localvar structure. The saved values
  34. @@ -9000,7 +9015,7 @@ mklocal(char *name)
  35. vp->flags |= VSTRFIXED|VTEXTFIXED;
  36. if (eq)
  37. setvareq(name, 0);
  38. - else
  39. + else if (!findlocal(vp))
  40. /* "local VAR" unsets VAR: */
  41. setvar(name, NULL, 0);
  42. }
  43. --- a/shell/ash_test/ash-misc/local1.right
  44. +++ b/shell/ash_test/ash-misc/local1.right
  45. @@ -1,4 +1,5 @@
  46. A1:'A'
  47. A2:''
  48. -A3:''
  49. -A4:'A'
  50. +A3:'B'
  51. +A4:''
  52. +A5:'A'
  53. --- a/shell/ash_test/ash-misc/local1.tests
  54. +++ b/shell/ash_test/ash-misc/local1.tests
  55. @@ -3,9 +3,12 @@ f() {
  56. local a
  57. # the above line unsets $a
  58. echo "A2:'$a'"
  59. - unset a
  60. + a=B
  61. + local a
  62. echo "A3:'$a'"
  63. + unset a
  64. + echo "A4:'$a'"
  65. }
  66. echo "A1:'$a'"
  67. f
  68. -echo "A4:'$a'"
  69. +echo "A5:'$a'"