strlcpy()/strlcat() fixes, mostly from Kay Sievers
diff --git a/klibc/strlcat.c b/klibc/strlcat.c
index 6111445..f397857 100644
--- a/klibc/strlcat.c
+++ b/klibc/strlcat.c
@@ -16,9 +16,11 @@
q++;
bytes++;
}
+ if (bytes == size)
+ return (bytes + strlen(src));
while ( (ch = *p++) ) {
- if ( bytes < size )
+ if ( bytes+1 < size )
*q++ = ch;
bytes++;
diff --git a/klibc/strlcpy.c b/klibc/strlcpy.c
index eb384c9..e693744 100644
--- a/klibc/strlcpy.c
+++ b/klibc/strlcpy.c
@@ -13,13 +13,16 @@
char ch;
while ( (ch = *p++) ) {
- if ( bytes < size )
+ if ( bytes+1 < size )
*q++ = ch;
bytes++;
}
- *q = '\0';
+ /* If size == 0 there is no space for a final null... */
+ if ( size )
+ *q = '\0';
+
return bytes;
}
diff --git a/klibc/tests/strlcpycat.c b/klibc/tests/strlcpycat.c
new file mode 100644
index 0000000..4d84c35
--- /dev/null
+++ b/klibc/tests/strlcpycat.c
@@ -0,0 +1,112 @@
+#include <stdio.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+int main(void)
+{
+ char temp[8];
+ size_t len;
+
+ printf("strlcpy:\n");
+ len = strlcpy(temp, "123", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "123") != 0)
+ goto error;
+
+ len = strlcpy(temp, "", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "") != 0)
+ goto error;
+
+ len = strlcpy(temp, "1234567890", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "1234567") != 0)
+ goto error;
+
+ len = strlcpy(temp, "123", 1);
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "") != 0)
+ goto error;
+
+ len = strlcpy(temp, "1234567890", 1);
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "") != 0)
+ goto error;
+
+ len = strlcpy(temp, "123", 0);
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "") != 0)
+ goto error;
+
+ len = strlcpy(temp, "1234567890", 0);
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "") != 0)
+ goto error;
+
+ len = strlcpy(temp, "1234567", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "1234567") != 0)
+ goto error;
+
+ len = strlcpy(temp, "12345678", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "1234567") != 0)
+ goto error;
+
+ printf("\n");
+ printf("strlcat:\n");
+ strcpy(temp, "");
+ len = strlcat(temp, "123", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "123") != 0)
+ goto error;
+
+ strcpy(temp, "ABC");
+ len = strlcat(temp, "", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "ABC") != 0)
+ goto error;
+
+ strcpy(temp, "");
+ len = strlcat(temp, "", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "") != 0)
+ goto error;
+
+ strcpy(temp, "ABC");
+ len = strlcat(temp, "123", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "ABC123") != 0)
+ goto error;
+
+ strcpy(temp, "ABC");
+ len = strlcat(temp, "1234567890", sizeof(temp));
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "ABC1234") != 0)
+ goto error;
+
+ strcpy(temp, "ABC");
+ len = strlcat(temp, "123", 5);
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "ABC1") != 0)
+ goto error;
+
+ strcpy(temp, "ABC");
+ len = strlcat(temp, "123", 1);
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "ABC") != 0)
+ goto error;
+
+ strcpy(temp, "ABC");
+ len = strlcat(temp, "123", 0);
+ printf("'%s'len:%zu strlen:%zu\n", temp, len, strlen(temp));
+ if (strcmp(temp, "ABC") != 0)
+ goto error;
+
+ exit(0);
+error:
+ printf("unexpected result\n");
+ exit(1);
+}