diff options
-rw-r--r-- | example/ndpiReader.c | 37 | ||||
-rw-r--r-- | src/include/ndpi_api.h | 12 | ||||
-rw-r--r-- | src/lib/ndpi_main.c | 17 |
3 files changed, 66 insertions, 0 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 73d469952..f04ee9f1e 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -5630,6 +5630,42 @@ void strtonumUnitTest() { /* *********************************************** */ +void strlcpyUnitTest() { + // Test empty string + char dst_empty[10] = ""; + assert(ndpi_strlcpy(dst_empty, "", sizeof(dst_empty)) == 0); + assert(dst_empty[0] == '\0'); + + // Basic copy test + char dst1[10] = ""; + assert(ndpi_strlcpy(dst1, "abc", sizeof(dst1)) == 3); + assert(strcmp(dst1, "abc") == 0); + + // Test with dst_len smaller than src_len + char dst2[4] = ""; + assert(ndpi_strlcpy(dst2, "abcdef", sizeof(dst2)) == 6); + assert(strcmp(dst2, "abc") == 0); // Should truncate "abcdef" to "abc" + + // Test with dst_len bigger than src_len + char dst3[10] = ""; + assert(ndpi_strlcpy(dst3, "abc", sizeof(dst3)) == 3); + assert(strcmp(dst3, "abc") == 0); + + // Test with dst_len equal to 1 (only null terminator should be copied) + char dst4[1]; + assert(ndpi_strlcpy(dst4, "abc", sizeof(dst4)) == 3); + assert(dst4[0] == '\0'); // Should only contain the null terminator + + // Test with NULL source, expecting return value to be 0 + char dst5[10]; + assert(ndpi_strlcpy(dst5, NULL, sizeof(dst5)) == 0); + + // Test with NULL destination, should also return 0 without crashing + assert(ndpi_strlcpy(NULL, "abc", sizeof(dst5)) == 0); +} + +/* *********************************************** */ + void filterUnitTest() { ndpi_filter* f = ndpi_filter_alloc(); u_int32_t v, i; @@ -5995,6 +6031,7 @@ int main(int argc, char **argv) { analysisUnitTest(); compressedBitmapUnitTest(); strtonumUnitTest(); + strlcpyUnitTest(); #endif } diff --git a/src/include/ndpi_api.h b/src/include/ndpi_api.h index acc01fb0f..ca1855532 100644 --- a/src/include/ndpi_api.h +++ b/src/include/ndpi_api.h @@ -2245,6 +2245,18 @@ extern "C" { void* ndpi_memmem(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len); + /** + * Copies up to -dst_len - 1- characters from -src- to -dst-, ensuring the result is + * null-terminated. Measures -src- length and handles potential null pointers. + * + * @par dst = destination buffer + * @par src = source string + * @par dst_len = size of the destination buffer, includes the null terminator + * + * @return Length of `src`. If greater or equal to -dst_len-, -dst- has been truncated. + */ + size_t ndpi_strlcpy(char* dst, const char* src, size_t dst_len); + #ifdef __cplusplus } #endif diff --git a/src/lib/ndpi_main.c b/src/lib/ndpi_main.c index ec848a0d2..815aa2e1c 100644 --- a/src/lib/ndpi_main.c +++ b/src/lib/ndpi_main.c @@ -11504,3 +11504,20 @@ void* ndpi_memmem(const void* haystack, size_t haystack_len, const void* needle, return NULL; } + +size_t ndpi_strlcpy(char *dst, const char* src, size_t dst_len) +{ + if (!dst || !src) { + return 0; + } + + size_t src_len = strlen(src); + + if (dst_len != 0) { + size_t len = (src_len < dst_len - 1) ? src_len : dst_len - 1; + memcpy(dst, src, len); + dst[len] = '\0'; + } + + return src_len; +} |