Skip to contentSkip to conversation

selftests: cgroup: add memory controller self-tests

bug report dan.carpenter at oracle.com (Dan Carpenter) 1 participant 2 messages May 17, 2018

Subsystems (2)
lore
Share & tools
lore

dan.carpenter at oracle.com (Dan Carpenter)

#0 lore raw

Hello Roman Gushchin,

The patch a62213fe9b77: "selftests: cgroup: add memory controller self-tests" from May 11, 2018, leads to the following static checker warning:

./tools/testing/selftests/cgroup/cgroup_util.c:62 cg_name()
warn: variable dereferenced before check 'name' (see line 59)

./tools/testing/selftests/cgroup/cgroup_util.c

    57  char *cg_name(const char *root, const char *name)
    58  {
    59          size_t len = strlen(root) + strlen(name) + 2;
                                            ^^^^^^^^^^^^
    60          char *ret = malloc(len);
    61  
    62          if (name)
                    ^^^^
    63                  snprintf(ret, len, "%s/%s", root, name);
    64  
    65          return ret;
    66  }
    67  
    68  char *cg_name_indexed(const char *root, const char *name, int index)
    69  {
    70          size_t len = strlen(root) + strlen(name) + 10;
                                            ^^^^^^^^^^^^
    71          char *ret = malloc(len);
    72  
    73          if (name)
                    ^^^^
    74                  snprintf(ret, len, "%s/%s_%d", root, name, index);
    75  
    76          return ret;
    77  }

regards,
dan carpenter
--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html

#1 lore raw
Quoted 9 lines · Dan Carpenter

Hello Roman Gushchin,

The patch a62213fe9b77: "selftests: cgroup: add memory controller self-tests" from May 11, 2018, leads to the following static checker warning:

./tools/testing/selftests/cgroup/cgroup_util.c:62 cg_name()
warn: variable dereferenced before check 'name' (see line 59)

Hi Dan!

Thank you for the report!
The fix is below.

Roman

--

Quoted 1 line

From ec990dc816b0a36201bed84e5b38b928b9bb9138 Mon Sep 17 00:00:00 2001

From: Roman Gushchin <[email protected]>
Date: Thu, 17 May 2018 16:53:15 +0100
Subject: [PATCH] kselftest/cgroup: fix variable dereferenced before check
warning

cg_name(const char *root, const char *name) is always called with non-empty root and name arguments, so there is no sense in checking it in the function body (after using in strlen()).

Signed-off-by Roman Gushchin <guro at fb.com>
Reported-by Dan Carpenter <dan.carpenter at oracle.com>
Cc linux-kselftest at vger.kernel.org
---
 tools/testing/selftests/cgroup/cgroup_util.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/testing/selftests/cgroup/cgroup_util.c +2 -4
index 41cc3b5e5be1..b69bdeb4b9fe 100644
@@ -59,8 +59,7 @@ char *cg_name(const char *root, const char *name)
 	size_t len = strlen(root) + strlen(name) + 2;
 	char *ret = malloc(len);
 
-	if (name)
-		snprintf(ret, len, "%s/%s", root, name);
+	snprintf(ret, len, "%s/%s", root, name);
 
 	return ret;
 }
@@ -70,8 +69,7 @@ char *cg_name_indexed(const char *root, const char *name, int index)
 	size_t len = strlen(root) + strlen(name) + 10;
 	char *ret = malloc(len);
 
-	if (name)
-		snprintf(ret, len, "%s/%s_%d", root, name, index);
+	snprintf(ret, len, "%s/%s_%d", root, name, index);
 
 	return ret;
 }
Signature · 6 lines
2.14.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kselftest" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html