[lxc-devel] [lxc/master] cmd: Lint with cppcheck

tcharding on Github lxc-bot at linuxcontainers.org
Mon Aug 20 06:38:44 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 848 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20180820/2f9378d0/attachment.bin>
-------------- next part --------------
From e3d0c8ad085afe52947cab7f35a5d730e96ca45e Mon Sep 17 00:00:00 2001
From: "Tobin C. Harding" <me at tobin.cc>
Date: Mon, 20 Aug 2018 16:28:39 +1000
Subject: [PATCH 1/2] cmd: Do not reassign variable before it is used

cppcheck emits warning

  Variable 'ofd' is reassigned a value before the old one has been used.

We do not need to initialise a variable if it is assigned to on first use.

Signed-off-by: Tobin C. Harding <me at tobin.cc>
---
 src/lxc/cmd/lxc_user_nic.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/lxc/cmd/lxc_user_nic.c b/src/lxc/cmd/lxc_user_nic.c
index 153940b86..6a1ea35c6 100644
--- a/src/lxc/cmd/lxc_user_nic.c
+++ b/src/lxc/cmd/lxc_user_nic.c
@@ -867,7 +867,8 @@ static char *lxc_secure_rename_in_ns(int pid, char *oldname, char *newname,
 	uid_t ruid, suid, euid;
 	char ifname[IFNAMSIZ];
 	char *string_ret = NULL, *name = NULL;
-	int fd = -1, ifindex = -1, ofd = -1;
+	int fd = -1, ifindex = -1;
+	int ofd;
 
 	pid_self = lxc_raw_getpid();
 
@@ -1039,7 +1040,7 @@ static bool is_privileged_over_netns(int netns_fd)
 	pid_t pid_self;
 	uid_t euid, ruid, suid;
 	bool bret = false;
-	int ofd = -1;
+	int ofd;
 
 	pid_self = lxc_raw_getpid();
 

From ef60341ddc09648f485db608ea92523300b2e0ec Mon Sep 17 00:00:00 2001
From: "Tobin C. Harding" <me at tobin.cc>
Date: Mon, 20 Aug 2018 16:31:33 +1000
Subject: [PATCH 2/2] cmd: Reduce scope of 'count' variable

Variable is used in one plaice only within a nested statement block.
The code is cleaner if the variable is declared near where it is used.
Found using cppcheck.

Reduce the scope of 'count' variable.

Signed-off-by: Tobin C. Harding <me at tobin.cc>
---
 src/lxc/cmd/lxc_user_nic.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/lxc/cmd/lxc_user_nic.c b/src/lxc/cmd/lxc_user_nic.c
index 6a1ea35c6..a2c5b655a 100644
--- a/src/lxc/cmd/lxc_user_nic.c
+++ b/src/lxc/cmd/lxc_user_nic.c
@@ -707,7 +707,6 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
 	char nicname[IFNAMSIZ];
 	struct stat sb;
 	struct alloted_s *n;
-	int count = 0;
 	char *buf = NULL;
 
 	for (n = names; n != NULL; n = n->next)
@@ -735,6 +734,8 @@ static char *get_nic_if_avail(int fd, struct alloted_s *names, int pid,
 		owner = NULL;
 
 		for (n = names; n != NULL; n = n->next) {
+			int count;
+
 			count = count_entries(buf, sb.st_size, n->name, intype, br);
 			if (count >= n->allowed)
 				continue;


More information about the lxc-devel mailing list