ash: fix handling of negative start value in ${v:start:len}
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 17 Nov 2014 19:27:18 +0000 (20:27 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 17 Nov 2014 19:27:18 +0000 (20:27 +0100)
function                                             old     new   delta
subevalvar                                          1140    1168     +28

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash.c

index 705fe9fa4af91de95533a5efd1ab1a504ef35757..90fb00fbd36e68d0f3ec4009e5a79c3245a0914b 100644 (file)
@@ -6411,7 +6411,15 @@ subevalvar(char *p, char *varname, int strloc, int subtype,
                                len = number(loc);
                        }
                }
-               if (pos >= orig_len) {
+               if (pos < 0) {
+                       /* ${VAR:$((-n)):l} starts n chars from the end */
+                       pos = orig_len + pos;
+               }
+               if ((unsigned)pos >= orig_len) {
+                       /* apart from obvious ${VAR:999999:l},
+                        * covers ${VAR:$((-9999999)):l} - result is ""
+                        * (bash-compat)
+                        */
                        pos = 0;
                        len = 0;
                }