From 512ec4632fa978dc08753aa26e3bfda7e5ae6868 Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Sat, 27 Nov 2021 10:07:58 -0500 Subject: [PATCH] fix: allow str_utfindex second argument to be an explicit nil (#16448) * str_utfindex checks number of arguments only, but ignores the case in which the second argument is an explicit nil. Previously this required dropping the second argument entirely. * Modify the C binding to explicitly check if the second argument is nil --- src/nvim/lua/stdlib.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index db79e9e7e9..b5553060a1 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -175,13 +175,13 @@ int nlua_str_utfindex(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL size_t s1_len; const char *s1 = luaL_checklstring(lstate, 1, &s1_len); intptr_t idx; - if (lua_gettop(lstate) >= 2) { + if (lua_isnoneornil(lstate, 2)) { + idx = (intptr_t)s1_len; + } else { idx = luaL_checkinteger(lstate, 2); if (idx < 0 || idx > (intptr_t)s1_len) { return luaL_error(lstate, "index out of range"); } - } else { - idx = (intptr_t)s1_len; } size_t codepoints = 0, codeunits = 0;