aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/charset.c
diff options
context:
space:
mode:
authorDundar Goc <gocdundar@gmail.com>2022-05-07 12:53:37 +0200
committerDundar Goc <gocdundar@gmail.com>2022-06-10 16:16:41 +0200
commita732c253b71f89702285d5ec6fd7803045f6add9 (patch)
treecd6b0dbad292dcbfaae637ffad385298594a2ff2 /src/nvim/charset.c
parente15d31b530c443daea04d7a772b24da737397c53 (diff)
downloadrneovim-a732c253b71f89702285d5ec6fd7803045f6add9.tar.gz
rneovim-a732c253b71f89702285d5ec6fd7803045f6add9.tar.bz2
rneovim-a732c253b71f89702285d5ec6fd7803045f6add9.zip
refactor: change type of linenr_T from long to int32_t
The size of long varies depending on architecture, in contrast to the MAXLNUM constant which sets the maximum allowable number of lines to 2^32-1. This discrepancy may lead to hard to detect bugs, for example https://github.com/neovim/neovim/issues/18454. Setting linenr_T to a fix maximum size of 2^32-1 will prevent this type of errors in the future. Also change the variables `amount` and `amount_after` to be linenr_T since they're referring to "the line number difference" between two texts.
Diffstat (limited to 'src/nvim/charset.c')
-rw-r--r--src/nvim/charset.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/nvim/charset.c b/src/nvim/charset.c
index e20c0e3f3f..9bc0bef446 100644
--- a/src/nvim/charset.c
+++ b/src/nvim/charset.c
@@ -1392,6 +1392,22 @@ long getdigits_long(char_u **pp, bool strict, long def)
return (long)number;
}
+/// Gets a int32_t number from a string.
+///
+/// @see getdigits
+int32_t getdigits_int32(char **pp, bool strict, long def)
+{
+ intmax_t number = getdigits((char_u **)pp, strict, def);
+#if SIZEOF_INTMAX_T > SIZEOF_INT32_T
+ if (strict) {
+ assert(number >= INT32_MIN && number <= INT32_MAX);
+ } else if (!(number >= INT32_MIN && number <= INT32_MAX)) {
+ return (int32_t)def;
+ }
+#endif
+ return (int32_t)number;
+}
+
/// Check that "lbuf" is empty or only contains blanks.
///
/// @param lbuf line buffer to check