aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/charset.c
diff options
context:
space:
mode:
authorSean Dewar <seandewar@users.noreply.github.com>2021-05-30 22:50:09 +0100
committerSean Dewar <seandewar@users.noreply.github.com>2021-09-11 15:33:19 +0100
commit34cb0879551b8217db820f684d913a393f4f38cb (patch)
treecd8c7320d91be9344cfe859c5861b475c093e565 /src/nvim/charset.c
parentcd18fe17a88d8144e5b841f1ca737fb4adf7885b (diff)
downloadrneovim-34cb0879551b8217db820f684d913a393f4f38cb.tar.gz
rneovim-34cb0879551b8217db820f684d913a393f4f38cb.tar.bz2
rneovim-34cb0879551b8217db820f684d913a393f4f38cb.zip
vim-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 vim/vim#3981) https://github.com/vim/vim/commit/16e9b85113e0b354ece1cb4f5fcc7866850f3685 Update vim_str2nr_spec.lua to add more tests that use strict = true.
Diffstat (limited to 'src/nvim/charset.c')
-rw-r--r--src/nvim/charset.c18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/nvim/charset.c b/src/nvim/charset.c
index 4725c0d08f..9f11e85b01 100644
--- a/src/nvim/charset.c
+++ b/src/nvim/charset.c
@@ -1397,6 +1397,7 @@ bool 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.
///
/// @param start
/// @param prep Returns guessed type of number 0 = decimal, 'x' or 'X' is
@@ -1407,9 +1408,12 @@ bool vim_isblankline(char_u *lbuf)
/// @param nptr Returns the signed result.
/// @param unptr Returns the unsigned result.
/// @param maxlen Max length of string to check.
+/// @param strict If true, fail if the number has unexpected trailing
+/// alpha-numeric chars: *len is set to 0 and nothing else is
+/// returned.
void vim_str2nr(const char_u *const start, int *const prep, int *const len,
const int what, varnumber_T *const nptr,
- uvarnumber_T *const unptr, const int maxlen)
+ uvarnumber_T *const unptr, const int maxlen, const bool strict)
FUNC_ATTR_NONNULL_ARG(1)
{
const char *ptr = (const char *)start;
@@ -1419,6 +1423,10 @@ void vim_str2nr(const char_u *const start, int *const prep, int *const len,
const bool negative = (ptr[0] == '-');
uvarnumber_T un = 0;
+ if (len != NULL) {
+ *len = 0;
+ }
+
if (negative) {
ptr++;
}
@@ -1492,7 +1500,7 @@ void vim_str2nr(const char_u *const start, int *const prep, int *const len,
goto vim_str2nr_dec;
}
- // Do the string-to-numeric conversion "manually" to avoid sscanf quirks.
+ // Do the conversion manually to avoid sscanf() quirks.
abort(); // Should’ve used goto earlier.
#define PARSE_NUMBER(base, cond, conv) \
do { \
@@ -1524,6 +1532,12 @@ vim_str2nr_hex:
#undef PARSE_NUMBER
vim_str2nr_proceed:
+ // Check for an alpha-numeric character immediately following, that is
+ // most likely a typo.
+ if (strict && ptr - (const char *)start != maxlen && ASCII_ISALNUM(*ptr)) {
+ return;
+ }
+
if (prep != NULL) {
*prep = pre;
}