summaryrefslogtreecommitdiffstats
path: root/src/charset.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2019-05-19 19:59:35 +0200
committerBram Moolenaar <Bram@vim.org>2019-05-19 19:59:35 +0200
commit16e9b85113e0b354ece1cb4f5fcc7866850f3685 (patch)
tree2abe4e3cffe8b0281f0690e5570a47eb2198a826 /src/charset.c
parentf5842c5a533346c4ff41ff666e465c85f1de35d5 (diff)
patch 8.1.1355: obvious mistakes are accepted as valid expressionsv8.1.1355
Problem: Obvious mistakes are accepted as valid expressions. Solution: Be more strict about parsing numbers. (Yasuhiro Matsumoto, closes #3981)
Diffstat (limited to 'src/charset.c')
-rw-r--r--src/charset.c31
1 files changed, 19 insertions, 12 deletions
diff --git a/src/charset.c b/src/charset.c
index 3eb5b58055..cff62e1857 100644
--- a/src/charset.c
+++ b/src/charset.c
@@ -1776,25 +1776,30 @@ vim_isblankline(char_u *lbuf)
* If "what" contains STR2NR_HEX recognize hex numbers
* If "what" contains STR2NR_FORCE always assume bin/oct/hex.
* If maxlen > 0, check at a maximum maxlen chars.
+ * If strict is TRUE, check the number strictly. return *len = 0 if fail.
*/
void
vim_str2nr(
char_u *start,
- int *prep, /* return: type of number 0 = decimal, 'x'
- or 'X' is hex, '0' = octal, 'b' or 'B'
- is bin */
- int *len, /* return: detected length of number */
- int what, /* what numbers to recognize */
- varnumber_T *nptr, /* return: signed result */
- uvarnumber_T *unptr, /* return: unsigned result */
- int maxlen) /* max length of string to check */
+ int *prep, // return: type of number 0 = decimal, 'x'
+ // or 'X' is hex, '0' = octal, 'b' or 'B'
+ // is bin
+ int *len, // return: detected length of number
+ int what, // what numbers to recognize
+ varnumber_T *nptr, // return: signed result
+ uvarnumber_T *unptr, // return: unsigned result
+ int maxlen, // max length of string to check
+ int strict) // check strictly
{
char_u *ptr = start;
- int pre = 0; /* default is decimal */
+ int pre = 0; // default is decimal
int negative = FALSE;
uvarnumber_T un = 0;
int n;
+ if (len != NULL)
+ *len = 0;
+
if (ptr[0] == '-')
{
negative = TRUE;
@@ -1836,9 +1841,7 @@ vim_str2nr(
}
}
- /*
- * Do the string-to-numeric conversion "manually" to avoid sscanf quirks.
- */
+ // Do the conversion manually to avoid sscanf() quirks.
n = 1;
if (pre == 'B' || pre == 'b' || what == STR2NR_BIN + STR2NR_FORCE)
{
@@ -1907,6 +1910,10 @@ vim_str2nr(
break;
}
}
+ // Check for an alpha-numeric character immediately following, that is
+ // most likely a typo.
+ if (strict && n - 1 != maxlen && ASCII_ISALNUM(*ptr))
+ return;
if (prep != NULL)
*prep = pre;