summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'util.c')
-rw-r--r--util.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/util.c b/util.c
index 4edf601..7bff5c4 100644
--- a/util.c
+++ b/util.c
@@ -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);