ash: fix corruption of ${#var} if $var contains UTF-8 characters
authorRon Yorston <rmy@pobox.com>
Fri, 18 Mar 2016 11:29:19 +0000 (11:29 +0000)
committerMike Frysinger <vapier@gentoo.org>
Tue, 22 Mar 2016 22:19:29 +0000 (18:19 -0400)
As reported in bug 8506:

   $ X=abcdÉfghÍjklmnÓpqrstÚvwcyz
   $ echo ${#X}
   abcd26

The result should be 26.

This regression was introduced by:

   <d68d1fb> 2015-05-18 [Ron Yorston]  ash: code shrink around varvalue

The length in characters was being used to discard the contents of
the variable instead of the length in bytes.

URL: https://bugs.busybox.net/8506
Reported-by: Martijn Dekker <martijn@inlv.org>
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
shell/ash.c
shell/ash_test/ash-vars/var-utf8-length.right [new file with mode: 0644]
shell/ash_test/ash-vars/var-utf8-length.tests [new file with mode: 0755]

index b5a2d961d9a481ec42b08f103b5dd36baf95f01d..5613e1f336cebfb8af7259053bf0c3397745a9e2 100644 (file)
@@ -6692,6 +6692,8 @@ varvalue(char *name, int varflags, int flags, struct strlist *var_str_list)
                if (subtype == VSLENGTH && len > 0) {
                        reinit_unicode_for_ash();
                        if (unicode_status == UNICODE_ON) {
+                               STADJUST(-len, expdest);
+                               discard = 0;
                                len = unicode_strlen(p);
                        }
                }
diff --git a/shell/ash_test/ash-vars/var-utf8-length.right b/shell/ash_test/ash-vars/var-utf8-length.right
new file mode 100644 (file)
index 0000000..6f4247a
--- /dev/null
@@ -0,0 +1 @@
+26
diff --git a/shell/ash_test/ash-vars/var-utf8-length.tests b/shell/ash_test/ash-vars/var-utf8-length.tests
new file mode 100755 (executable)
index 0000000..d04b2cb
--- /dev/null
@@ -0,0 +1,2 @@
+X=abcdÉfghÍjklmnÓpqrstÚvwcyz
+echo ${#X}