1 /* Tests for some interesting cases in non-standard strlcpy(). */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <strings.h> 6 7 int main(void) 8 { 9 size_t copied; 10 11 char *src = malloc(100); 12 if (src == NULL) { 13 fprintf(stderr, "Memory allocation failure.\n"); 14 return 1; 15 } 16 strcpy(src, "Hey, dude!"); 17 18 char *dst = malloc(10); 19 if (dst == NULL) { 20 fprintf(stderr, "Memory allocation failure.\n"); 21 return 1; 22 } 23 24 /* This is ok. */ 25 copied = strlcpy(dst, src, 10); 26 if (copied != 10) 27 fprintf(stderr, "Expected 10 but got %zu for test #1.\n", copied); 28 29 /* Here dst is not large enough. */ 30 copied = strlcpy(dst, src, strlen(src) + 1); 31 if (copied != 10) 32 fprintf(stderr, "Expected 10 but got %zu for test #2.\n", copied); 33 34 /* This is just a fancy way how to write strlen(src). 35 Undocumented but heavily used. */ 36 copied = strlcpy(NULL, src, 0); 37 if (copied != 10) 38 fprintf(stderr, "Expected 10 but got %zu for test #3.\n", copied); 39 40 /* Source and destination overlap. */ 41 strlcpy(src + 9, src, strlen(src) + 1); 42 /* Return value is not checked because function behaviour 43 is undefined in such case (and valgrind's differs). */ 44 45 return 0; 46 } 47 48