+2016-04-27 Paul Eggert <eggert@cs.ucla.edu>
+
+ xstrtol: prohibit monstrosities like "1bB"
+ Problem reported by Young Mo Kang in: http://bugs.gnu.org/23388
+ * lib/xstrtol.c (__xstrtol): Allow trailing second suffixes like
+ "B" only if the first suffix needs a base.
+ * tests/test-xstrtol.sh: Test this.
+
2016-04-21 Pádraig Brady <P@draigBrady.com>
xstrtod: reinstate setting of *result upon ERANGE
return err | LONGINT_INVALID_SUFFIX_CHAR;
}
- if (strchr (valid_suffixes, '0'))
+ switch (**p)
{
+ case 'E': case 'G': case 'g': case 'k': case 'K': case 'M': case 'm':
+ case 'P': case 'T': case 't': case 'Y': case 'Z':
+
/* The "valid suffix" '0' is a special flag meaning that
an optional second suffix is allowed, which can change
the base. A suffix "B" (e.g. "100MB") stands for a power
a power of 1024. If no suffix (e.g. "100M"), assume
power-of-1024. */
- switch (p[0][1])
- {
- case 'i':
- if (p[0][2] == 'B')
- suffixes += 2;
- break;
-
- case 'B':
- case 'D': /* 'D' is obsolescent */
- base = 1000;
- suffixes++;
- break;
- }
+ if (strchr (valid_suffixes, '0'))
+ switch (p[0][1])
+ {
+ case 'i':
+ if (p[0][2] == 'B')
+ suffixes += 2;
+ break;
+
+ case 'B':
+ case 'D': /* 'D' is obsolescent */
+ base = 1000;
+ suffixes++;
+ break;
+ }
}
switch (**p)
break;
case 'B':
+ /* This obsolescent first suffix is distinct from the 'B'
+ second suffix above. E.g., 'tar -L 1000B' means change
+ the tape after writing 1000 KiB of data. */
overflow = bkm_scale (&tmp, 1024);
break;
test-xstrtol 010 >> out 2>&1 || result=1
# suffix without integer is valid
test-xstrtol MiB >> out 2>&1 || result=1
+test-xstrtol 1bB >> out 2>&1 && result=1
# test xstrtoul
test-xstrtoul 1 >> out 2>&1 || result=1
test-xstrtoul 9x >> out 2>&1 && result=1
test-xstrtoul 010 >> out 2>&1 || result=1
test-xstrtoul MiB >> out 2>&1 || result=1
+test-xstrtoul 1bB >> out 2>&1 && result=1
# Find out how to remove carriage returns from output. Solaris /usr/ucb/tr
# does not understand '\r'.
invalid suffix in X argument '9x'
010->8 ()
MiB->1048576 ()
+invalid suffix in X argument '1bB'
1->1 ()
invalid X argument '-1'
1k->1024 ()
invalid suffix in X argument '9x'
010->8 ()
MiB->1048576 ()
+invalid suffix in X argument '1bB'
EOF
compare expected out || result=1