From: Jan Safranek Date: Mon, 7 Jun 2010 09:58:46 +0000 (+0200) Subject: Fix overlaping src and dest buffers in sprintf() X-Git-Tag: v0.37.1~65 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c4b3d1badd067954ab59970c93d98ff3408c641f;p=thirdparty%2Flibcgroup.git 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 --- 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) {