From c4b3d1badd067954ab59970c93d98ff3408c641f Mon Sep 17 00:00:00 2001 From: Jan Safranek Date: Mon, 7 Jun 2010 11:58:46 +0200 Subject: [PATCH] Fix overlaping src and dest buffers in sprintf() libcgroup compiled with various optimization flags produce strange results when sprintf's destination and source pointers overlap or are the same. In addition, use strncpy() instead. This is more or less useless, because cg_build_path() does not check sizes either, but just for my personal feeling of correct C code... Signed-off-by: Jan Safranek Signed-off-by: Dhaval Giani --- src/api.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/api.c b/src/api.c index 5409c9c2..dcdd75b4 100644 --- a/src/api.c +++ b/src/api.c @@ -2848,6 +2848,7 @@ int cgroup_read_stats_begin(const char *controller, const char *path, { int ret = 0; char stat_file[FILENAME_MAX]; + char stat_path[FILENAME_MAX]; FILE *fp; if (!cgroup_initialized) @@ -2856,10 +2857,11 @@ int cgroup_read_stats_begin(const char *controller, const char *path, if (!cgroup_stat || !handle) return ECGINVAL; - if (!cg_build_path(path, stat_file, controller)) + if (!cg_build_path(path, stat_path, controller)) return ECGOTHER; - sprintf(stat_file, "%s/%s.stat", stat_file, controller); + snprintf(stat_file, sizeof(stat_file), "%s/%s.stat", stat_path, + controller); fp = fopen(stat_file, "re"); if (!fp) { -- 2.47.2