patch 9.0.1144: reading beyond text
Problem: Reading beyond text.
Solution: Add strlen_maxlen() and use it.
diff --git a/src/strings.c b/src/strings.c
index edcae6f..9174265 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -525,6 +525,19 @@
mch_memmove(to + tolen, from, fromlen + 1);
}
+/*
+ * A version of strlen() that has a maximum length.
+ */
+ size_t
+vim_strlen_maxlen(char *s, size_t maxlen)
+{
+ size_t i;
+ for (i = 0; i < maxlen; ++i)
+ if (s[i] == NUL)
+ break;
+ return i;
+}
+
#if (!defined(HAVE_STRCASECMP) && !defined(HAVE_STRICMP)) || defined(PROTO)
/*
* Compare two strings, ignoring case, using current locale.
@@ -582,7 +595,7 @@
* 128 to 255 correctly. It also doesn't return a pointer to the NUL at the
* end of the string.
*/
- char_u *
+ char_u *
vim_strchr(char_u *string, int c)
{
char_u *p;