From c00db68300bd61c57c04a92a279d5f0e36eef8cc Mon Sep 17 00:00:00 2001 From: Jiri Slaby Date: Tue, 4 Jan 2011 17:56:39 +0100 Subject: [PATCH] [PATCH 2/3] cgsnapshot: fix strn* lengths The 'n' parameter in strncat stands for how much to copy from src, not what's dest overall space. So we need to subtract full strlen we have constructed so far. Also fix one strncpy where we may pass too much as well as in strncpy. Signed-off-by: Jiri Slaby Signed-off-by: Balbir Singh --- src/tools/cgsnapshot.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/tools/cgsnapshot.c b/src/tools/cgsnapshot.c index b2239701..88f2142b 100644 --- a/src/tools/cgsnapshot.c +++ b/src/tools/cgsnapshot.c @@ -209,7 +209,7 @@ static int display_permissions(const char *path, /* get tasks file statistic */ strncpy(tasks_path, path, FILENAME_MAX); tasks_path[FILENAME_MAX-1] = '\0'; - strncat(tasks_path, "/tasks", FILENAME_MAX); + strncat(tasks_path, "/tasks", FILENAME_MAX - strlen(tasks_path) - 1); tasks_path[FILENAME_MAX-1] = '\0'; ret = stat(tasks_path, &sbt); if (ret) { @@ -332,11 +332,15 @@ static int display_cgroup_data(struct cgroup *group, variable files in the root group to find out whether the variable is writable. */ + if (root_path_len >= FILENAME_MAX) + root_path_len = FILENAME_MAX - 1; strncpy(var_path, group_path, root_path_len); var_path[root_path_len] = '\0'; - strncat(var_path, "/", FILENAME_MAX); + strncat(var_path, "/", FILENAME_MAX - + strlen(var_path) - 1); var_path[FILENAME_MAX-1] = '\0'; - strncat(var_path, name, FILENAME_MAX); + strncat(var_path, name, FILENAME_MAX - + strlen(var_path) - 1); var_path[FILENAME_MAX-1] = '\0'; /* test whether the write permissions */ -- 2.47.2