]> git.ipfire.org Git - thirdparty/libcgroup.git/commitdiff
api.c: Fix string truncation warnings
authorTom Hromatka <tom.hromatka@oracle.com>
Mon, 12 Apr 2021 15:18:56 +0000 (09:18 -0600)
committerTom Hromatka <tom.hromatka@oracle.com>
Mon, 12 Apr 2021 16:26:45 +0000 (10:26 -0600)
Fix two snprintf string truncation warnings in
cg_build_path_locked().

api.c:1475:38: warning: ‘snprintf’ output may be truncated before the
last format character [-Wformat-truncation=]
 1475 |     snprintf(path, FILENAME_MAX, "%s/",
      |                                      ^
api.c:1475:5: note: ‘snprintf’ output between 2 and 4097 bytes into a
destination of size 4096
 1475 |     snprintf(path, FILENAME_MAX, "%s/",
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1476 |       cg_mount_table[i].mount.path);
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
api.c:1470:40: warning: ‘/’ directive output may be truncated writing 1
byte into a region of size between 0 and 4095 [-Wformat-truncation=]
 1470 |     snprintf(path, FILENAME_MAX, "%s/%s/",
      |                                        ^
api.c:1470:5: note: ‘snprintf’ output 3 or more bytes (assuming 4098)
into a destination of size 4096
 1470 |     snprintf(path, FILENAME_MAX, "%s/%s/",
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1471 |       cg_mount_table[i].mount.path,
      |       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1472 |       cg_namespace_table[i]);
      |       ~~~~~~~~~~~~~~~~~~~~~~

Signed-off-by: Tom Hromatka <tom.hromatka@oracle.com>
src/api.c

index a6f4e46b673a306b2f91f32573d5aefd2b3e10fe..68357e673a114d918626198b6acf9b8274f5e427 100644 (file)
--- a/src/api.c
+++ b/src/api.c
@@ -1463,18 +1463,27 @@ static char *cg_concat_path(const char *pref, const char *suf, char *path)
 char *cg_build_path_locked(const char *name, char *path,
                           const char *type)
 {
-       int i;
+       int i, ret;
        for (i = 0; cg_mount_table[i].name[0] != '\0'; i++) {
                if (strcmp(cg_mount_table[i].name, type) == 0) {
                        if (cg_namespace_table[i]) {
-                               snprintf(path, FILENAME_MAX, "%s/%s/",
+                               ret = snprintf(path, FILENAME_MAX, "%s/%s/",
                                                cg_mount_table[i].mount.path,
                                                cg_namespace_table[i]);
                                path[FILENAME_MAX-1] = '\0';
+                               if (ret >= FILENAME_MAX)
+                                       cgroup_dbg("Warning: filename too long:"
+                                               "%s/%s/",
+                                               cg_mount_table[i].mount.path,
+                                               cg_namespace_table[i]);
                        } else {
-                               snprintf(path, FILENAME_MAX, "%s/",
+                               ret = snprintf(path, FILENAME_MAX, "%s/",
                                                cg_mount_table[i].mount.path);
                                path[FILENAME_MAX-1] = '\0';
+                               if (ret >= FILENAME_MAX)
+                                       cgroup_dbg("Warning: filename too long:"
+                                               "%s/",
+                                               cg_mount_table[i].mount.path);
                        }
 
                        if (name) {