From b8d37052d18723ff2a8572f5092069e80785f93a Mon Sep 17 00:00:00 2001 From: Thorsten Töpper Date: Wed, 25 Jun 2025 21:58:57 +0200 Subject: data_management: create_node asan fix When executing on /usr/bin or /usr/lib it sometimes happened after a few hundred files, that even if source and target were arrays of type char[256] memcpy would be noted as a heap buffer overflow. Switching to strnlen(fname,256) and dst[255]='\0'; to be flexible and getting rid of the asan error. --- src/data_management.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/data_management.c b/src/data_management.c index d4cf76c..ff2b3a9 100644 --- a/src/data_management.c +++ b/src/data_management.c @@ -21,6 +21,12 @@ inline struct list_node *create_node(char *fname, struct stat *ln_stat) { struct list_node *node = NULL; + size_t length = 0; +#ifdef DEBUGBUILD + static size_t count = 0; + + DBGTRC("DEBUG: Called fname '%s', attempting to create node #%lu\n", fname, ++count); +#endif if (fname == NULL || fname[0] == '\0') { LOGERR("ERROR: No valid filename given\n"); @@ -37,12 +43,17 @@ inline struct list_node *create_node(char *fname, struct stat *ln_stat) { return NULL; } + length = strnlen(fname,256); + if (length>255) { + LOGERR("ERROR: strnlen(\"%s\",256) == %lu, terminating with '\\0' at char[255].\n", + fname, length); + length = 255; + } node->next = NULL; - /* With strncpy and strict compiler options it complained. A file name - * can't be longer than 256 bytes, including the string terminating \0 - * byte at the end. As the code no longer's in the same file...*/ - memcpy(node->fname, fname, 255); - node->fname[255] = '\0'; + memcpy(node->fname, fname, length); + if (node->fname[255] != '\0') { + node->fname[255] = '\0'; + } memcpy(&(node->ln_stat), ln_stat, sizeof(struct stat)); return node; } -- cgit v1.2.3-70-g09d2