0
0
mirror of https://github.com/vim/vim.git synced 2025-09-27 04:14:06 -04:00

patch 8.1.1355: obvious mistakes are accepted as valid expressions

Problem:    Obvious mistakes are accepted as valid expressions.
Solution:   Be more strict about parsing numbers. (Yasuhiro Matsumoto,
            closes #3981)
This commit is contained in:
Bram Moolenaar
2019-05-19 19:59:35 +02:00
parent f5842c5a53
commit 16e9b85113
13 changed files with 86 additions and 28 deletions

View File

@@ -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;