The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxc/pull/2552

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Run linter `cppcheck` over `src/lxc/cmd`.

This is i tiny PR to see if `LXC` is into using cppcheck for linting.  Patch one makes a change that is directly related to the discussion in #2542 (in regards to declaring/initializing local variables).  Patch 2 is a stylistic change reducing the scope of a variable (and minuscule amounts of required cognitive load).  Please be harsh to critique these changes, I'll follow on the ruling here with further linting if deemed useful.

thanks,
Tobin. 

Signed-off-by: Tobin C. Harding <[email protected]>
From e3d0c8ad085afe52947cab7f35a5d730e96ca45e Mon Sep 17 00:00:00 2001
From: "Tobin C. Harding" <[email protected]>
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 <[email protected]>
---
 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" <[email protected]>
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 <[email protected]>
---
 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;
_______________________________________________
lxc-devel mailing list
[email protected]
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to