From 60f703558c8fede8ca9335f42555feda8a457047 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Tue, 14 Jul 2015 11:33:41 -0400 Subject: [PATCH] Fix: Follow struct dirent allocation guidelines of READDIR(3) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Galarneau --- src/common/config/config.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/common/config/config.c b/src/common/config/config.c index a4b59ff08..8608faf98 100644 --- a/src/common/config/config.c +++ b/src/common/config/config.c @@ -2495,6 +2495,23 @@ end: return ret; } +/* Allocate dirent as recommended by READDIR(3), NOTES on readdir_r */ +static +struct dirent *alloc_dirent(const char *path) +{ + size_t len; + long name_max; + struct dirent *entry; + + name_max = pathconf(path, _PC_NAME_MAX); + if (name_max == -1) { + name_max = PATH_MAX; + } + len = offsetof(struct dirent, d_name) + name_max + 1; + entry = zmalloc(len); + return entry; +} + static int load_session_from_path(const char *path, const char *session_name, struct session_config_validation_ctx *validation_ctx, int override) @@ -2530,7 +2547,7 @@ int load_session_from_path(const char *path, const char *session_name, goto end; } - entry = zmalloc(sizeof(*entry)); + entry = alloc_dirent(path); if (!entry) { ret = -LTTNG_ERR_NOMEM; goto end; -- 2.34.1