aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2005-03-22 21:00:32 +0000
committerH. Peter Anvin <hpa@zytor.com>2005-03-22 21:00:32 +0000
commit4644bd92e21c7fd9a48dd8dfd0c4f8ef772843f0 (patch)
tree2f9288a7ab1b2e1faa75ca18e0f7e93312e691c0
parentccb24e02a3ffc6ef67a427aab2f0953bd40e1303 (diff)
downloadklibc-4644bd92e21c7fd9a48dd8dfd0c4f8ef772843f0.tar.gz
strlcpy()/strlcat() fixes, mostly from Kay Sieversklibc-1.0.5
-rw-r--r--klibc/strlcat.c4
-rw-r--r--klibc/strlcpy.c7
-rw-r--r--klibc/tests/strlcpycat.c112
3 files changed, 120 insertions, 3 deletions
diff --git a/klibc/strlcat.c b/klibc/strlcat.c
index 6111445f06299..f397857e76362 100644
--- a/klibc/strlcat.c
+++ b/klibc/strlcat.c
@@ -16,9 +16,11 @@ size_t strlcat(char *dst, const char *src, size_t size)
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 eb384c9885d2a..e6937445cd64e 100644
--- a/klibc/strlcpy.c
+++ b/klibc/strlcpy.c
@@ -13,13 +13,16 @@ size_t strlcpy(char *dst, const char *src, size_t size)
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 0000000000000..4d84c353a383a
--- /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);
+}