mirror of
https://github.com/neovim/neovim.git
synced 2025-10-02 07:58:35 +00:00
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
22cbc8a4e1
Co-authored-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user