aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThorsten Töpper <atsutane@freethoughts.de>2025-06-25 21:58:57 +0200
committerThorsten Töpper <atsutane@freethoughts.de>2025-06-25 21:58:57 +0200
commitb8d37052d18723ff2a8572f5092069e80785f93a (patch)
tree32b6d87adeede5d09679d59819a93b62735b11fd
parentf85727f042f18d42e77336de138fc81663e32899 (diff)
downloaddir_monitor-b8d37052d18723ff2a8572f5092069e80785f93a.tar.gz
dir_monitor-b8d37052d18723ff2a8572f5092069e80785f93a.tar.bz2
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.
-rw-r--r--src/data_management.c21
1 files 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;
}