diff options
author | pommicket <pommicket@gmail.com> | 2022-12-29 17:33:29 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-29 17:33:29 -0500 |
commit | 5f7af06a9085835a3d1ad3a51374c245859d7d97 (patch) | |
tree | f8ea4ad4d809334131b479ff8b9c0bd125c45f29 /util.c | |
parent | e4aac4859511cb22ed84946eb5510cd7fa14e147 (diff) |
fix GCC warnings
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 13 |
1 files changed, 8 insertions, 5 deletions
@@ -119,11 +119,14 @@ static bool streq(char const *a, char const *b) { } static size_t strn_len(const char *src, size_t n) { - const char *p = memchr(src, '\0', n); - if (p) - return (size_t)(p - src); - else - return n; + const char *p = src; + // in C99 and C++11/14, calling memchr with a size larger than + // the size of the object is undefined behaviour. + // i don't think there's any way of doing this (efficiently) with standard C functions. + for (size_t i = 0 ; i < n; ++i, ++p) + if (*p == '\0') + break; + return (size_t)(p - src); } // duplicates at most n characters from src |