diff --git a/src/util/strings.c b/src/util/strings.c index bd513fda4..eee8836f8 100644 --- a/src/util/strings.c +++ b/src/util/strings.c @@ -76,9 +76,10 @@ size_t str_strlcpy(char *dst, const char *src, size_t len) /* Not enough room in dst, add NUL and traverse rest of src */ if (n == 0) { - if (len != 0) - *d = '\0'; /* NUL-terminate dst */ - return (d - dst); /* count does not include NUL */ + if (len != 0) { + *d = '\0'; /* NUL-terminate dst */ + } + return (s - src) + strlen(s); /* count does not include NUL */ } return (s - src - 1); /* count does not include NUL */ @@ -283,8 +284,8 @@ void sbs_strcat(struct sbstring *sbs, const char *str) size_t len; assert(sbs); len = sbs->size - (sbs->end - sbs->begin); - len = str_strlcpy(sbs->end, str, len); - sbs->end += len; + str_strlcpy(sbs->end, str, len); + sbs->end += strlen(sbs->end); assert(sbs->begin + sbs->size >= sbs->end); } diff --git a/src/util/strings.test.c b/src/util/strings.test.c index 23816a72d..80ad531e9 100644 --- a/src/util/strings.test.c +++ b/src/util/strings.test.c @@ -119,10 +119,10 @@ static void test_str_strlcpy(CuTest * tc) CuAssertStrEquals(tc, "herp", buffer); CuAssertIntEquals(tc, 0x7f, buffer[5]); - CuAssertIntEquals(tc, 3, (int)str_strlcpy(buffer, "herp", 4)); + CuAssertIntEquals(tc, 4, (int)str_strlcpy(buffer, "herp", 4)); CuAssertStrEquals(tc, "her", buffer); - CuAssertIntEquals(tc, 7, (int)str_strlcpy(buffer, "herpderp", 8)); + CuAssertIntEquals(tc, 8, (int)str_strlcpy(buffer, "herpderp", 8)); CuAssertStrEquals(tc, "herpder", buffer); CuAssertIntEquals(tc, 0x7f, buffer[8]);