From 3af4724225de2eef2df28d29e74182236c97d4ca Mon Sep 17 00:00:00 2001 From: jinlun Date: Sat, 19 Oct 2024 15:32:59 +0800 Subject: [PATCH 1/2] Optimized directory suffix matching Fix an issue where file names were not matched correctly when iterating over a directory in the XFS file system --- src/common/dim_utils.c | 15 --------------- src/common/dim_utils.h | 3 +-- .../static_baseline/dim_core_static_baseline.c | 11 ++++++++--- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/common/dim_utils.c b/src/common/dim_utils.c index 57ea3e9..6746d88 100644 --- a/src/common/dim_utils.c +++ b/src/common/dim_utils.c @@ -48,21 +48,6 @@ out: return ret; } -bool dim_string_end_with(const char *str, const char *ext) -{ - int name_len, ext_len; - - if (str == NULL || ext == NULL) - return false; - - name_len = strlen(str); - ext_len = strlen(ext); - if (name_len < ext_len) - return false; - - return dim_strcmp(str + name_len - ext_len, ext) == 0; -} - int dim_parse_line_buf(char *buf, loff_t len, int (*line_parser)(char *, int, void *), void *data) { int ret = 0; diff --git a/src/common/dim_utils.h b/src/common/dim_utils.h index 8c7d855..db43546 100644 --- a/src/common/dim_utils.h +++ b/src/common/dim_utils.h @@ -18,7 +18,6 @@ #define dim_devel(fmt, ...) int dim_get_absolute_path(const char *path, const char **result); -bool dim_string_end_with(const char *str, const char *ext); int dim_parse_line_buf(char *buf, loff_t len, int (*line_parser)(char *, int, void *), void *data); -#endif \ No newline at end of file +#endif diff --git a/src/core/static_baseline/dim_core_static_baseline.c b/src/core/static_baseline/dim_core_static_baseline.c index 4fb6e51..7ae5171 100644 --- a/src/core/static_baseline/dim_core_static_baseline.c +++ b/src/core/static_baseline/dim_core_static_baseline.c @@ -17,6 +17,9 @@ #include "dim_core_measure.h" #include "dim_core_static_baseline.h" +#define BASELINE_FILE_SUFFIX ".hash" +#define BASELINE_FILE_SUFFIX_LEN 5 + static bool baseline_match_policy(const char *name, int type) { const char *kr = init_uts_ns.name.release; @@ -86,15 +89,17 @@ baseline_fill_dir(struct dir_context *__ctx, struct name_entry *entry = NULL; /* baseline file must end with '.hash' */ - if (d_type != DT_REG || strlen(name) >= NAME_MAX || - (!dim_string_end_with(name, ".hash"))) + if (d_type != DT_REG || name_len >= NAME_MAX || + name_len <= BASELINE_FILE_SUFFIX_LEN || + strncmp(name + name_len - BASELINE_FILE_SUFFIX_LEN, + BASELINE_FILE_SUFFIX, BASELINE_FILE_SUFFIX_LEN)) goto out; /* ignore invalid files */ entry = dim_kzalloc_gfp(sizeof(struct name_entry)); if (entry == NULL) goto out; - strcpy(entry->name, name); + strncpy(entry->name, name, name_len); list_add( &entry->list, &ctx->name_list); out: #if LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0) -- 2.33.0