aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua
diff options
context:
space:
mode:
authorVanaIgr <vanaigranov@gmail.com>2024-02-26 04:12:55 -0600
committerGitHub <noreply@github.com>2024-02-26 18:12:55 +0800
commitad5a155b1f4b387d3aaa54c91d0146cb0287bb9f (patch)
treeae35dff22d4f418f040d39acc88206e64ffb1984 /src/nvim/lua
parent8b4e26915612caf2d143edca31919cae18a848a1 (diff)
downloadrneovim-ad5a155b1f4b387d3aaa54c91d0146cb0287bb9f.tar.gz
rneovim-ad5a155b1f4b387d3aaa54c91d0146cb0287bb9f.tar.bz2
rneovim-ad5a155b1f4b387d3aaa54c91d0146cb0287bb9f.zip
fix(mbyte): fix bugs in utf_cp_*_off() functions
Problems: - Illegal bytes after valid UTF-8 char cause utf_cp_*_off() to fail. - When stream isn't NUL-terminated, utf_cp_*_off() may go over the end. Solution: Don't go over end of the char of end of the string.
Diffstat (limited to 'src/nvim/lua')
-rw-r--r--src/nvim/lua/stdlib.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c
index 5fea1ba5d8..8f58fd1a1a 100644
--- a/src/nvim/lua/stdlib.c
+++ b/src/nvim/lua/stdlib.c
@@ -226,11 +226,12 @@ static int nlua_str_utf_start(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
size_t s1_len;
const char *s1 = luaL_checklstring(lstate, 1, &s1_len);
ptrdiff_t offset = luaL_checkinteger(lstate, 2);
- if (offset < 0 || offset > (intptr_t)s1_len) {
+ if (offset <= 0 || offset > (intptr_t)s1_len) {
return luaL_error(lstate, "index out of range");
}
- int head_offset = -utf_cp_head_off(s1, s1 + offset - 1);
- lua_pushinteger(lstate, head_offset);
+ size_t const off = (size_t)(offset - 1);
+ int head_off = -utf_cp_bounds_len(s1, s1 + off, (int)(s1_len - off)).begin_off;
+ lua_pushinteger(lstate, head_off);
return 1;
}
@@ -246,11 +247,12 @@ static int nlua_str_utf_end(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
size_t s1_len;
const char *s1 = luaL_checklstring(lstate, 1, &s1_len);
ptrdiff_t offset = luaL_checkinteger(lstate, 2);
- if (offset < 0 || offset > (intptr_t)s1_len) {
+ if (offset <= 0 || offset > (intptr_t)s1_len) {
return luaL_error(lstate, "index out of range");
}
- int tail_offset = utf_cp_tail_off(s1, s1 + offset - 1);
- lua_pushinteger(lstate, tail_offset);
+ size_t const off = (size_t)(offset - 1);
+ int tail_off = utf_cp_bounds_len(s1, s1 + off, (int)(s1_len - off)).end_off - 1;
+ lua_pushinteger(lstate, tail_off);
return 1;
}