summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/commonlib/bsd/string.c17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/commonlib/bsd/string.c b/src/commonlib/bsd/string.c
index 56670e8862..6482279713 100644
--- a/src/commonlib/bsd/string.c
+++ b/src/commonlib/bsd/string.c
@@ -15,10 +15,19 @@ size_t strlen(const char *str)
size_t strnlen(const char *str, size_t maxlen)
{
- size_t len = 0;
- while (*str++ && len < maxlen)
- len++;
- return len;
+ const char *ptr = str;
+ const char *end = str + maxlen;
+
+ if (!maxlen)
+ return 0;
+
+ while (*ptr++) {
+ /* Make sure this checks for ==, not >=, because the calculation
+ for `end` may overflow in some edge cases. */
+ if (ptr == end)
+ return maxlen;
+ }
+ return ptr - str - 1;
}
char *strcat(char *dst, const char *src)