unfuck str_strlcpy again.

This commit is contained in:
Enno Rehling 2018-11-27 21:30:08 +01:00
parent 5520372dfa
commit 2483232537
2 changed files with 8 additions and 7 deletions

View File

@ -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 */ /* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) { if (n == 0) {
if (len != 0) if (len != 0) {
*d = '\0'; /* NUL-terminate dst */ *d = '\0'; /* NUL-terminate dst */
return (d - dst); /* count does not include NUL */ }
return (s - src) + strlen(s); /* count does not include NUL */
} }
return (s - src - 1); /* 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; size_t len;
assert(sbs); assert(sbs);
len = sbs->size - (sbs->end - sbs->begin); len = sbs->size - (sbs->end - sbs->begin);
len = str_strlcpy(sbs->end, str, len); str_strlcpy(sbs->end, str, len);
sbs->end += len; sbs->end += strlen(sbs->end);
assert(sbs->begin + sbs->size >= sbs->end); assert(sbs->begin + sbs->size >= sbs->end);
} }

View File

@ -119,10 +119,10 @@ static void test_str_strlcpy(CuTest * tc)
CuAssertStrEquals(tc, "herp", buffer); CuAssertStrEquals(tc, "herp", buffer);
CuAssertIntEquals(tc, 0x7f, buffer[5]); 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); 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); CuAssertStrEquals(tc, "herpder", buffer);
CuAssertIntEquals(tc, 0x7f, buffer[8]); CuAssertIntEquals(tc, 0x7f, buffer[8]);