aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/math.c
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2024-04-11 09:34:43 +0800
committerGitHub <noreply@github.com>2024-04-11 09:34:43 +0800
commit7aa56370f326f5f1b3fdfa1c79bbffc7cc6ae267 (patch)
tree3e01c5c1e5e883cfa7d517670ac5ab1a10042ac8 /src/nvim/math.c
parent4528a69c8772a5fe182fb293b929dfcfad112abc (diff)
downloadrneovim-7aa56370f326f5f1b3fdfa1c79bbffc7cc6ae267.tar.gz
rneovim-7aa56370f326f5f1b3fdfa1c79bbffc7cc6ae267.tar.bz2
rneovim-7aa56370f326f5f1b3fdfa1c79bbffc7cc6ae267.zip
vim-patch:9.0.2114: overflow detection not accurate when adding digits (#28271)
Problem: overflow detection not accurate when adding digits Solution: Use a helper function Use a helper function to better detect overflows before adding integer digits to a long or an integer variable respectively. Signal the overflow to the caller function. closes: vim/vim#13539 https://github.com/vim/vim/commit/22cbc8a4e17ce61aa460c451a26e1bff2c3d2af9 Co-authored-by: Christian Brabandt <cb@256bit.org>
Diffstat (limited to 'src/nvim/math.c')
-rw-r--r--src/nvim/math.c15
1 files changed, 14 insertions, 1 deletions
diff --git a/src/nvim/math.c b/src/nvim/math.c
index 2fd9cd6ce7..47a667416c 100644
--- a/src/nvim/math.c
+++ b/src/nvim/math.c
@@ -1,14 +1,16 @@
// uncrustify:off
#include <math.h>
// uncrustify:on
+#include <limits.h>
#include <stdint.h>
#include <string.h>
-#ifdef _MSC_VER
+#ifdef HAVE_BITSCANFORWARD64
# include <intrin.h> // Required for _BitScanForward64
#endif
#include "nvim/math.h"
+#include "nvim/vim_defs.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "math.c.generated.h"
@@ -74,3 +76,14 @@ int xctz(uint64_t x)
return count;
#endif
}
+
+/// For overflow detection, add a digit safely to an int value.
+int vim_append_digit_int(int *value, int digit)
+{
+ int x = *value;
+ if (x > ((INT_MAX - digit) / 10)) {
+ return FAIL;
+ }
+ *value = x * 10 + digit;
+ return OK;
+}