diff options
author | pommicket <pommicket@gmail.com> | 2022-12-10 12:32:21 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-10 12:32:21 -0500 |
commit | 17e12d6cd6dc769d714991da97db153e7225180e (patch) | |
tree | 69c84907f860e2b5c2c3347b3dabbec484bf3994 | |
parent | f982cffe0f35276a28e8be19b855a9008107bb77 (diff) |
fix slight bug in strn_dup
-rw-r--r-- | util.c | 10 |
1 files changed, 9 insertions, 1 deletions
@@ -69,9 +69,17 @@ static bool streq(char const *a, char const *b) { return strcmp(a, b) == 0; } +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; +} + // duplicates at most n characters from src static char *strn_dup(char const *src, size_t n) { - size_t len = strlen(src); + size_t len = strn_len(src, n); if (n > len) n = len; char *ret = malloc(n + 1); |