Cleanup cppcheck statistics 58/45358/6
authorKrzysztof Dynowski <k.dynowski@samsung.com>
Fri, 31 Jul 2015 13:01:38 +0000 (15:01 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Thu, 6 Aug 2015 14:13:49 +0000 (07:13 -0700)
[Feature]       N/A
[Cause]         Cppcheck results reported on jenkins contains error/warning messages
[Solution]      Make code correction to satisfy cppcheck,
                add cppcheck configuration to suppress some messages (e.g. API funcs)
[Verification]  Have installed cppcheck >= v1.66
                Perform ./tests/cppcheck/cppcheck.sh script before commiting to review

Change-Id: Ice10bbc118ab921feb9cb7261d4a2bdc22282353

27 files changed:
common/netlink/netlink-message.cpp
common/utils/same-thread-guard.hpp
common/utils/worker.cpp
libs/config/fields.hpp
libs/config/sqlite3/connection.cpp
libs/config/types.hpp
libs/ipc/client.cpp
libs/ipc/internals/processor.hpp
libs/ipc/internals/socket.cpp
server/host-dbus-connection.cpp
server/host-dbus-connection.hpp
server/netdev.cpp
server/zone-provision.cpp
server/zone-provision.hpp
server/zones-manager.cpp
tests/cppcheck/cppcheck.sh [new file with mode: 0755]
tests/cppcheck/cppcheck.suppress [new file with mode: 0644]
tests/unit_tests/config/testconfig-example.hpp
tests/unit_tests/ipc/ut-ipc.cpp
tests/unit_tests/lxc/ut-zone.cpp
tests/unit_tests/server/ut-server.cpp
tests/unit_tests/server/ut-zone-provision.cpp
tests/unit_tests/server/ut-zone.cpp
wrapper/wrapper-compatibility.cpp
zone-daemon/daemon.cpp
zone-daemon/daemon.hpp
zone-daemon/runner.hpp

index 29b079a..58aa1a2 100644 (file)
@@ -289,14 +289,15 @@ inline int NetlinkResponse::getHdrPosition() const
 
 NetlinkResponse send(const NetlinkMessage& msg, int pid)
 {
-    assert(msg.hdr().nlmsg_flags & NLM_F_ACK);
+    const auto &hdr = msg.hdr();
+    assert(hdr.nlmsg_flags & NLM_F_ACK);
 
     std::unique_ptr<std::vector<char>> data;
     Netlink nl;
     nl.open(pid);
     try {
-        nl.send(&msg.hdr());
-        data = nl.rcv(msg.hdr().nlmsg_seq);
+        nl.send(&hdr);
+        data = nl.rcv(hdr.nlmsg_seq);
     } catch (const std::exception& ex) {
         LOGE("Sending failed (" << ex.what() << ")");
         nl.close();
index 3a173b5..ec62b70 100644 (file)
@@ -67,8 +67,8 @@ private:
 
 #else // ENABLE_SAME_THREAD_GUARD
 #   define ASSERT_SAME_THREAD(g)
-    bool check() {return true;}
-    void reset() {}
+    static bool check() {return true;}
+    static void reset() {}
 #endif // ENABLE_SAME_THREAD_GUARD
 };
 
index 09069e8..eeac593 100644 (file)
@@ -145,7 +145,7 @@ private:
         LOGT("Worker thread exited");
     }
 
-    void execute(const TaskInfo& taskInfo)
+    static void execute(const TaskInfo& taskInfo)
     {
         try {
             LOGT("Executing task from subgroup " << taskInfo.groupID);
index 8169cb4..d1fc4be 100644 (file)
 
 #define CONFIG_REGISTER_EMPTY                                      \
     template<typename Visitor>                                     \
-    void accept(Visitor ) {                                        \
-    }                                                              \
-    template<typename Visitor>                                     \
-    void accept(Visitor ) const {                                  \
+    static void accept(Visitor ) {                                 \
     }                                                              \
 
 #define CONFIG_REGISTER(...)                                       \
index cb25694..3887352 100644 (file)
@@ -50,9 +50,7 @@ Connection::Connection(const std::string& path)
 
 Connection::~Connection()
 {
-    if (::sqlite3_close(mDbPtr) != SQLITE_OK) {
-        throw ConfigException("Error during closing the database. Error: " + getErrorMessage());
-    }
+    ::sqlite3_close(mDbPtr);
 }
 
 void Connection::exec(const std::string& query)
index 3355f8f..f8b788d 100644 (file)
@@ -41,4 +41,4 @@ struct FileDescriptor {
 
 }// config
 
-#endif //COMMON_CONFIG_TYPES_HPP
\ No newline at end of file
+#endif //COMMON_CONFIG_TYPES_HPP
index ad2eb47..245faee 100644 (file)
@@ -32,6 +32,7 @@ namespace ipc {
 
 Client::Client(epoll::EventPoll& eventPoll, const std::string& socketPath)
     : mEventPoll(eventPoll),
+      mServiceID(-1),
       mProcessor(eventPoll, "[CLIENT]  "),
       mSocketPath(socketPath)
 {
index 4d0089f..7f7cd63 100644 (file)
@@ -329,8 +329,6 @@ public:
     FileDescriptor getEventFD();
 
 private:
-    typedef std::function<void(int fd, std::shared_ptr<void>& data)> SerializeCallback;
-    typedef std::function<std::shared_ptr<void>(int fd)> ParseCallback;
     typedef std::unique_lock<std::mutex> Lock;
     typedef RequestQueue<Event>::Request Request;
 
@@ -340,7 +338,7 @@ private:
 
     struct RegisterSignalsProtocolMessage {
         RegisterSignalsProtocolMessage() = default;
-        RegisterSignalsProtocolMessage(const std::vector<MethodID> ids)
+        RegisterSignalsProtocolMessage(const std::vector<MethodID>& ids)
             : ids(ids) {}
 
         std::vector<MethodID> ids;
index 1961a52..c58809d 100644 (file)
@@ -182,11 +182,15 @@ int Socket::createSocketInternal(const std::string& path)
 Socket Socket::createSocket(const std::string& path)
 {
     // Initialize a socket
-    int fd = -1;
+    int fd;
 #ifdef HAVE_SYSTEMD
     fd = getSystemdSocketInternal(path);
+    if (fd == -1) {
+       fd = createSocketInternal(path);
+    }
+#else
+    fd = createSocketInternal(path);
 #endif // HAVE_SYSTEMD
-    fd = fd != -1 ? fd : createSocketInternal(path);
 
     return Socket(fd);
 }
index 1229acb..ce415c8 100644 (file)
@@ -424,8 +424,9 @@ void HostDbusConnection::onSignalCall(const std::string& /* senderBusName */,
                                       const std::string& objectPath,
                                       const std::string& interface,
                                       const std::string& /* signalName */,
-                                      GVariant* /* parameters */)
+                                      GVariant* /* parameters */) const
 {
+    (void)this; // satisfy cpp-check
     if (objectPath != api::dbus::OBJECT_PATH || interface != api::dbus::INTERFACE) {
         return;
     }
index bb59afc..1a703e7 100644 (file)
@@ -97,7 +97,7 @@ private:
                       const std::string& objectPath,
                       const std::string& interface,
                       const std::string& signalName,
-                      GVariant* parameters);
+                      GVariant* parameters) const;
 };
 
 
index bf2576c..3086e7f 100644 (file)
@@ -595,7 +595,7 @@ void setAttrs(const pid_t nsPid, const std::string& netdev, const Attrs& attrs)
                 size_t pos = addrAttr.find(":");
                 if (pos == string::npos || pos == addrAttr.length()) {
                     LOGE("Wrong input data format: ill formed address attribute: " << addrAttr);
-                    VasumException("Wrong input data format: ill formed address attribute");
+                    throw VasumException("Wrong input data format: ill formed address attribute");
                 }
                 attrs.push_back(make_tuple(addrAttr.substr(0, pos), addrAttr.substr(pos + 1)));
             }
index 70c3711..4e99f83 100644 (file)
@@ -255,7 +255,7 @@ void ZoneProvision::link(const ZoneProvisioningConfig::Link& config)
     throw UtilsException("Failed to hard link: path prefix is not valid");
 }
 
-std::string ZoneProvision::getId(const ZoneProvisioningConfig::File& file) const
+std::string ZoneProvision::getId(const ZoneProvisioningConfig::File& file)
 {
     //TODO output of type,flags and mode should be more user friendly
     return "file " +
@@ -265,7 +265,7 @@ std::string ZoneProvision::getId(const ZoneProvisioningConfig::File& file) const
            std::to_string(file.mode);
 }
 
-std::string ZoneProvision::getId(const ZoneProvisioningConfig::Mount& mount) const
+std::string ZoneProvision::getId(const ZoneProvisioningConfig::Mount& mount)
 {
     //TODO output of flags should be more user friendly
     return "mount " +
@@ -276,12 +276,12 @@ std::string ZoneProvision::getId(const ZoneProvisioningConfig::Mount& mount) con
            mount.data;
 }
 
-std::string ZoneProvision::getId(const ZoneProvisioningConfig::Link& link) const
+std::string ZoneProvision::getId(const ZoneProvisioningConfig::Link& link)
 {
     return "link " + link.source + " " + link.target;
 }
 
-std::string ZoneProvision::getId(const ZoneProvisioningConfig::Provision& provision) const
+std::string ZoneProvision::getId(const ZoneProvisioningConfig::Provision& provision)
 {
     using namespace vasum;
     if (provision.is<ZoneProvisioningConfig::File>()) {
index 2f123ae..cf3d5c6 100644 (file)
@@ -113,10 +113,10 @@ private:
     void file(const ZoneProvisioningConfig::File& config);
     void link(const ZoneProvisioningConfig::Link& config);
 
-    std::string getId(const ZoneProvisioningConfig::File& file) const;
-    std::string getId(const ZoneProvisioningConfig::Mount& mount) const;
-    std::string getId(const ZoneProvisioningConfig::Link& link) const;
-    std::string getId(const ZoneProvisioningConfig::Provision& provision) const;
+    static std::string getId(const ZoneProvisioningConfig::File& file);
+    static std::string getId(const ZoneProvisioningConfig::Mount& mount);
+    static std::string getId(const ZoneProvisioningConfig::Link& link);
+    static std::string getId(const ZoneProvisioningConfig::Provision& provision);
 };
 
 } // namespace vasum
index 0d88d24..5280d21 100644 (file)
@@ -399,7 +399,7 @@ void ZonesManager::focusInternal(Zones::iterator iter)
     }
 
     Zone& zoneToFocus = get(iter);
-    const std::string idToFocus = zoneToFocus.getId();
+    const std::string& idToFocus = zoneToFocus.getId();
 
     if (idToFocus == mActiveZoneId) {
         return;
diff --git a/tests/cppcheck/cppcheck.sh b/tests/cppcheck/cppcheck.sh
new file mode 100755 (executable)
index 0000000..b8bb637
--- /dev/null
@@ -0,0 +1,15 @@
+#!/bin/bash
+OPT=""
+OPT+=" --enable=all"
+OPT+=" --std=c++11"
+OPT+=" --inconclusive"
+OPT+=" --suppressions tests/cppcheck/cppcheck.suppress"
+OPT+=" --suppress=missingIncludeSystem"
+OPT+=" --inline-suppr"
+OPT+=" -iCMakeFiles"
+OPT+=" -I common"
+OPT+=" -I libs"
+OPT+=" -I client"
+OPT+=" -I server"
+OPT+=" -U__NR_capset"
+cppcheck ${OPT} --template=gcc ./ 1>/dev/null
diff --git a/tests/cppcheck/cppcheck.suppress b/tests/cppcheck/cppcheck.suppress
new file mode 100644 (file)
index 0000000..d3b098f
--- /dev/null
@@ -0,0 +1,7 @@
+// public API functions (or write tests to use them all)
+unusedFunction:wrapper/wrapper-compatibility.cpp
+unusedFunction:wrapper/wrapper.cpp
+
+// lambda not recognized to catch exception (v1.68)
+exceptThrowInNoexecptFunction:client/vasum-client-impl.cpp
+
index ff5c2ef..549985b 100644 (file)
@@ -34,13 +34,7 @@ struct TestConfig {
     struct SubConfig {
 
         struct SubSubConfig {
-            int intVal;
-            bool moved;
 
-            CONFIG_REGISTER
-            (
-                intVal
-            )
             SubSubConfig() : intVal(), moved(false) {}
             SubSubConfig(const SubSubConfig& config) : intVal(config.intVal), moved(false) {}
             SubSubConfig(SubSubConfig&& config) : intVal(std::move(config.intVal)), moved(false) {
@@ -60,6 +54,13 @@ struct TestConfig {
             bool isMoved() const {
                 return moved;
             }
+
+            int intVal;
+            bool moved;
+            CONFIG_REGISTER
+            (
+                intVal
+            )
         };
 
         int intVal;
index ab920fe..8bb829e 100644 (file)
@@ -159,15 +159,10 @@ struct EmptyData {
 
 struct ThrowOnAcceptData {
     template<typename Visitor>
-    void accept(Visitor)
+    static void accept(Visitor)
     {
         throw std::runtime_error("intentional failure in accept");
     }
-    template<typename Visitor>
-    void accept(Visitor) const
-    {
-        throw std::runtime_error("intentional failure in accept const");
-    }
 };
 
 void returnEmptyCallback(const PeerID,
index 6702773..80c003f 100644 (file)
@@ -60,7 +60,7 @@ struct Fixture {
         cleanup();
     }
 
-    void cleanup()
+    static void cleanup()
     {
         LxcZone lxc(ZONE_PATH, ZONE_NAME);
         if (lxc.isDefined()) {
@@ -71,7 +71,7 @@ struct Fixture {
         }
     }
 
-    void waitForInit()
+    static void waitForInit()
     {
         // wait for init fully started (wait for bash to be able to trap SIGTERM)
         std::this_thread::sleep_for(std::chrono::milliseconds(200));
index e4d1ce4..79a7d72 100644 (file)
@@ -63,7 +63,7 @@ struct Fixture {
         LOGI("------------ setup complete -----------");
     }
 
-    void prepare()
+    static void prepare()
     {
         ScopedGlibLoop loop;
         ipc::epoll::ThreadDispatcher mDispatcher;
index 45a74d7..034552e 100644 (file)
@@ -69,7 +69,7 @@ struct Fixture {
         BOOST_REQUIRE(utils::saveFileContent(SOME_FILE_PATH.string(), "text"));
     }
 
-    ZoneProvision create(const std::vector<std::string>& validLinkPrefixes)
+    static ZoneProvision create(const std::vector<std::string>& validLinkPrefixes)
     {
         return ZoneProvision(ROOTFS_PATH.string(),
                              TEST_CONFIG_PATH,
@@ -78,12 +78,12 @@ struct Fixture {
                              validLinkPrefixes);
     }
 
-    void load(ZoneProvisioningConfig& config)
+    static void load(ZoneProvisioningConfig& config)
     {
         config::loadFromKVStoreWithJsonFile(DB_PATH.string(), TEST_CONFIG_PATH, config, DB_PREFIX);
     }
 
-    void save(const ZoneProvisioningConfig& config)
+    static void save(const ZoneProvisioningConfig& config)
     {
         config::saveToKVStore(DB_PATH.string(), config, DB_PREFIX);
     }
index e492c37..0072d74 100644 (file)
@@ -102,12 +102,12 @@ struct Fixture {
     }
 
 
-    void ensureStarted()
+    static void ensureStarted()
     {
         // wait for zones init to fully start
         std::this_thread::sleep_for(std::chrono::milliseconds(200));
     }
-    void ensureStop()
+    static void ensureStop()
     {
         // wait for fully stop
         std::this_thread::sleep_for(std::chrono::milliseconds(200));
index 634c187..04c043d 100644 (file)
@@ -342,10 +342,9 @@ API int wait_for_pid_status(pid_t pid)
 API vsm_fso_type_t fso_string_to_type(char *str)
 {
     LOGS("");
-    int len;
     int i;
     for (i = 0; i <= VSM_FSO_MAX_TYPE; i++) {
-        len = strlen(fso_type_strtab[i]);
+        int len = strlen(fso_type_strtab[i]);
         if (strncmp(str, fso_type_strtab[i], len) == 0)
             return static_cast<vsm_fso_type_t>(i);
     }
@@ -358,12 +357,11 @@ API int mkdir_p(const char *dir, mode_t mode)
     LOGS("");
     const char *tmp = dir;
     const char *orig = dir;
-    char *makeme;
 
     do {
         dir = tmp + strspn(tmp, "/");
         tmp = dir + strcspn(dir, "/");
-        makeme = strndup(orig, dir - orig);
+        char *makeme = strndup(orig, dir - orig);
         if (*makeme) {
             if (mkdir(makeme, mode) && errno != EEXIST) {
                 free(makeme);
@@ -421,8 +419,6 @@ API int remove_file(char *path)
 {
     LOGS("");
     struct stat path_stat;
-    DIR *dp;
-    struct dirent *d;
     int status = 0;
 
     if (lstat(path, &path_stat) < 0) {
@@ -433,6 +429,8 @@ API int remove_file(char *path)
     }
 
     if (S_ISDIR(path_stat.st_mode)) {
+        struct dirent *d;
+        DIR *dp;
         if ((dp = opendir(path)) == NULL) {
             ERROR("Unable to opendir %s", path);
             return -1;
@@ -475,7 +473,6 @@ API int copy_file(const char *source, const char *dest, int /*flags*/)
     LOGS("");
     int ret;
     FILE *sfp, *dfp;
-    size_t nread, nwritten, size = BUF_SIZE;
     char buffer[BUF_SIZE];
 
     if ((sfp = fopen(source, "r")) == NULL) {
@@ -490,6 +487,7 @@ API int copy_file(const char *source, const char *dest, int /*flags*/)
     }
 
     while (1) {
+        size_t nread, nwritten, size = BUF_SIZE;
         nread = fread(buffer, 1, size, sfp);
 
         if (nread != size && ferror(sfp)) {
@@ -840,7 +838,7 @@ API pid_t get_init_pid(const char *name)
     fp = fopen(filename, "r");
 
     if (fp != NULL) {
-        if (fscanf(fp, "%d", &ret) < 0) {
+        if (fscanf(fp, "%7d", &ret) < 0) {
             ERROR("Failed to read %s\n", filename);
             ret = -2;
         }
@@ -894,7 +892,7 @@ API pid_t get_zone_pid(const char *name, const char *target)
         FILE *cmdfp;
         char cmdpath[PATH_MAX];
 
-        res = sscanf(line, "%d", &pid);
+        res = sscanf(line, "%7d", &pid);
         if (res != 1) {
             ERROR("Failed to read %s\n", path);
             res = -1;
@@ -915,7 +913,7 @@ API pid_t get_zone_pid(const char *name, const char *target)
             continue;
         }
 
-        if (fscanf(cmdfp, "%s", cmd) < 0) {
+        if (fscanf(cmdfp, "%1023s", cmd) < 0) {
             ERROR("Failed to read cmdline - pid : %d\n", pid);
             continue;
         }
@@ -1271,11 +1269,12 @@ static int parse_statement(struct parser_context *ctx, int argc, char **argv,
 {
     struct parser_state state;
     char *args[PARSER_MAXARGS];
-    int i, nargs, done, rc;
+    int i;
     int ret = 0;
     UNUSED(ctx);
 
     for (i = 0; i < argc; i++) {
+        int nargs, done, rc;
         done = nargs = 0;
         parser_init_state(&state, argv[i]);
 
index 0ded40a..c4979e0 100644 (file)
@@ -45,21 +45,23 @@ Daemon::~Daemon()
 {
 }
 
-void Daemon::onNameLostCallback()
+void Daemon::onNameLostCallback() const
 {
+    (void)this; // satisfy cpp-check
     //TODO: Try to reconnect or close the daemon.
     LOGE("Dbus name lost");
 }
 
-void Daemon::onGainFocusCallback()
+void Daemon::onGainFocusCallback() const
 {
+    (void)this; // satisfy cpp-check
     LOGD("Gained Focus");
 }
 
-void Daemon::onLoseFocusCallback()
+void Daemon::onLoseFocusCallback() const
 {
+    (void)this; // satisfy cpp-check
     LOGD("Lost Focus");
-
 }
 
 } // namespace zone_daemon
index 92dcccf..ab52e69 100644 (file)
@@ -40,9 +40,9 @@ public:
     virtual ~Daemon();
 
 private:
-    void onNameLostCallback();
-    void onGainFocusCallback();
-    void onLoseFocusCallback();
+    void onNameLostCallback() const;
+    void onGainFocusCallback() const;
+    void onLoseFocusCallback() const;
     std::unique_ptr<DaemonConnection> mConnectionPtr;
 };
 
index 1a88530..1827553 100644 (file)
@@ -39,13 +39,13 @@ public:
     /**
      * Starts all the daemon and blocks until SIGINT or SIGTERM
      */
-    void run();
+    static void run();
 
     /**
      * Terminates the daemon.
      * Equivalent of sending SIGINT or SIGTERM signal
      */
-    void terminate();
+    static void terminate();
 };