Fix coverity issues 96/180996/2 accepted/tizen/unified/20180611.015316 submit/tizen/20180607.013257
authoryeji01.kim <yeji01.kim@samsung.com>
Thu, 7 Jun 2018 06:53:40 +0000 (15:53 +0900)
committeryeji01.kim <yeji01.kim@samsung.com>
Thu, 7 Jun 2018 07:11:00 +0000 (16:11 +0900)
- Buffer not null terminated
- Unchecked return value from library

Change-Id: Idc43d3153ed29bf975083ea57c8db6128873d782
Signed-off-by: yeji01.kim <yeji01.kim@samsung.com>
tools/tests/groups/account.cpp
tools/tests/groups/file.cpp
tools/tests/groups/ipc.cpp
tools/tests/groups/network.cpp
tools/tests/groups/system.cpp

index ce0f83403d3b3458b49a6692d48404f5d2f63180..b66abf7a73a706d47833767690821973b55eec21 100644 (file)
@@ -51,9 +51,15 @@ public:
                for (auto entry : testfiles) {
                        struct stat st;
                        std::string logStr(entry + " change");
-                       lstat(entry.c_str(), &st);
-                       chmod(entry.c_str(), st.st_mode);
-                       Display::printResult(Display::SUCCESS, logStr);
+
+                       if (lstat(entry.c_str(), &st) == 0) {
+                               if (chmod(entry.c_str(), st.st_mode) == 0)
+                                       Display::printResult(Display::SUCCESS, logStr);
+                               else
+                                       Display::printResult(Display::FAIL, logStr);
+                       } else {
+                               Display::printResult(Display::FAIL, logStr);
+                       }
                }
        }
 
index 91d5727b987955b3870f6ca51881ca06b9569a45..bb8bc2e3baee2d8a308ef85c1f6544e324d6f456 100644 (file)
@@ -204,8 +204,7 @@ public:
 
                //restore
                {
-                       struct stat st;
-                       if ((lstat(fileName.c_str(), &st) == 0) && (unlink(fileName.c_str()) != 0)) {
+                       if (unlink(fileName.c_str()) != 0) {
                                Display::printError();
                                Display::printResult(Display::FAIL, "accessFile restore");
                        }
@@ -278,8 +277,7 @@ public:
 
                //restore
                {
-                       struct stat st;
-                       if ((lstat(fileName.c_str(), &st) == 0) && (unlink(fileName.c_str()) != 0)) {
+                       if (unlink(fileName.c_str()) != 0) {
                                Display::printError();
                                Display::printResult(Display::FAIL, "destroyFile restore");
                        }
@@ -388,10 +386,9 @@ public:
 
                //restore
                {
-                       struct stat st;
                        if (fd >= 0)
                                close(fd);
-                       if ((lstat(fileName.c_str(), &st) == 0) && (unlink(fileName.c_str()) != 0)) {
+                       if (unlink(fileName.c_str()) != 0) {
                                Display::printError();
                                Display::printResult(Display::FAIL, "createLink restore");
                        }
@@ -511,8 +508,7 @@ public:
 
                //restore
                {
-                       struct stat st;
-                       if ((lstat(fileName.c_str(), &st) == 0) && (unlink(fileName.c_str()) != 0)) {
+                       if (unlink(fileName.c_str()) != 0) {
                                Display::printError();
                                Display::printResult(Display::FAIL, "changeFileConf restore");
                        }
@@ -618,8 +614,7 @@ public:
 
                //restore
                {
-                       struct stat st;
-                       if ((lstat(fileName.c_str(), &st) == 0) && (unlink(fileName.c_str()) != 0)) {
+                       if (unlink(fileName.c_str()) != 0) {
                                Display::printError();
                                Display::printResult(Display::FAIL, "changeFileConfMAC restore");
                        }
index ff1762ff104b61420d547f22727f223ba7a21bf9..927dfb11385eda4a4320479eff05faa0a3c22aea 100644 (file)
@@ -99,9 +99,9 @@ public:
                        }
 
                msgctl_result:
-                       struct stat st;
-                       if (lstat(msgKeyPath.c_str(), &st) == 0)
-                               rmdir(msgKeyPath.c_str());
+                       if (rmdir(msgKeyPath.c_str()) != 0)
+                               Display::printError();
+
                        Display::printResult(ret, "msgctl(303) positive");
                }
 
@@ -186,7 +186,6 @@ public:
                int recvlen = 0;
                int backlog = 10;
                struct sockaddr_un addr;
-               struct stat st;
                std::thread client;
 
                if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
@@ -198,6 +197,7 @@ public:
                memset(&addr, 0, sizeof(addr));
                addr.sun_family = AF_UNIX;
                strncpy(addr.sun_path, sockaddr.c_str(), sizeof(sockaddr_un::sun_path));
+               addr.sun_path[sizeof(sockaddr_un::sun_path) - 1] = '\0';
 
                if (bind(fd, reinterpret_cast<struct sockaddr *>(&addr), sizeof(struct sockaddr_un)) == -1) {
                        close(fd);
@@ -226,13 +226,18 @@ public:
                        memset(&serveraddr, 0, sizeof(serveraddr));
                        serveraddr.sun_family = AF_UNIX;
                        strncpy(serveraddr.sun_path, sockaddr.c_str(), sizeof(sockaddr_un::sun_path));
+                       serveraddr.sun_path[sizeof(sockaddr_un::sun_path) - 1] = '\0';
 
                        if (connect(clientFd, reinterpret_cast<struct sockaddr *>(&serveraddr), sizeof(serveraddr)) == -1) {
                                close(clientFd);
                                Display::printError();
                                return;
                        }
-                       send(clientFd, message.c_str(), message.size(), 0);
+
+                       if (send(clientFd, message.c_str(), message.size(), 0) < 0) {
+                               Display::printError();
+                               Display::printResult(Display::FAIL, "useSocket positive");
+                       }
 
                        if (clientFd >= 0)
                                close(clientFd);
@@ -245,11 +250,11 @@ public:
                        return;
                }
                memset(buffer, 0, sizeof(buffer));
-
                if((recvlen = recv(cliFd, buffer, sizeof(buffer), 0)) == -1)
                        Display::printError();
 
                if (recvlen > 0) {
+                       buffer[recvlen] = '\0';
                        Display::printResult(Display::NONE, std::string(buffer));
                        Display::printResult(Display::SUCCESS, "useSocket positive");
                } else {
@@ -262,7 +267,9 @@ public:
                        close(cliFd);
                if (fd >= 0)
                        close(fd);
-               if ((lstat(sockaddr.c_str(), &st) == 0) && (unlink(sockaddr.c_str()) != 0))
+
+
+               if (unlink(sockaddr.c_str()) != 0)
                        Display::printError();
        }
 };
index dbebca6f8039a19e30a59efd6f3b50125b5eaa8f..7175bda0ff5be867a7f1f496ff18b8b60f859227 100644 (file)
@@ -54,9 +54,14 @@ public:
                        for (auto entry : paths) {
                                struct stat st;
                                std::string logStr(entry + " change");
-                               lstat(entry.c_str(), &st);
-                               chmod(entry.c_str(), st.st_mode);
-                               Display::printResult(Display::SUCCESS, logStr);
+                               if (lstat(entry.c_str(), &st) == 0) {
+                                       if (chmod(entry.c_str(), st.st_mode) == 0)
+                                               Display::printResult(Display::SUCCESS, logStr);
+                                       else
+                                               Display::printResult(Display::FAIL, logStr);
+                               } else {
+                                       Display::printResult(Display::FAIL, logStr);
+                               }
                        }
                }
 
index 2e7c1c1b6150f3136289e3d5a1a1578f4fe3c08c..7d4e07fbc827676552a3cce1ffb1548830ae3e5e 100644 (file)
@@ -75,9 +75,15 @@ public:
                for (auto entry : testPath) {
                        struct stat st;
                        std::string logStr(entry + " access");
-                       lstat(entry.c_str(), &st);
-                       chmod(entry.c_str(), st.st_mode);
-                       Display::printResult(Display::SUCCESS, logStr);
+
+                       if (lstat(entry.c_str(), &st) == 0) {
+                               if (chmod(entry.c_str(), st.st_mode) == 0)
+                                       Display::printResult(Display::SUCCESS, logStr);
+                               else
+                                       Display::printResult(Display::FAIL, logStr);
+                       } else {
+                               Display::printResult(Display::FAIL, logStr);
+                       }
                }
        }
 
@@ -140,8 +146,7 @@ public:
                }
 
                for (std::string entry : mountPath) {
-                       struct stat st;
-                       if ((lstat(entry.c_str(), &st) == 0) && (rmdir(entry.c_str()) != 0))
+                       if (rmdir(entry.c_str()) != 0)
                                Display::printError();
                }
        }
@@ -220,9 +225,15 @@ public:
                std::string testPath("/etc/ld.so.conf");
                struct stat st;
 
-               lstat(testPath.c_str(), &st);
-               chmod(testPath.c_str(), st.st_mode);
-               Display::printResult(Display::SUCCESS, "change /etc/ld.so.conf");
+               if (lstat(testPath.c_str(), &st) == 0) {
+                       if (chmod(testPath.c_str(), st.st_mode) == 0)
+                               Display::printResult(Display::SUCCESS, "change /etc/ld.so.conf");
+                       else
+                               Display::printResult(Display::FAIL, "change /etc/ld.so.conf");
+               } else {
+                       Display::printError();
+                       Display::printResult(Display::FAIL, "change /etc/ld.so.conf");
+               }
        }
 
        void changeKernelModule() {
@@ -230,9 +241,15 @@ public:
                std::string testPath("/etc/modules-load.d");
                struct stat st;
 
-               lstat(testPath.c_str(), &st);
-               chmod(testPath.c_str(), st.st_mode);
-               Display::printResult(Display::SUCCESS, "change /etc/modules-load.d");
+               if (lstat(testPath.c_str(), &st) == 0) {
+                       if (chmod(testPath.c_str(), st.st_mode) == 0)
+                               Display::printResult(Display::SUCCESS, "change /etc/modules-load.d");
+                       else
+                               Display::printResult(Display::FAIL, "change /etc/modules-load.d");
+               } else {
+                       Display::printError();
+                       Display::printResult(Display::FAIL, "change /etc/modules-load.d");
+               }
        }
 
        void useKernelModule() {
@@ -261,9 +278,14 @@ public:
 
                for (auto entry : testPath) {
                        std::string logStr(entry + " access");
-                       lstat(entry.c_str(), &st);
-                       chmod(entry.c_str(), st.st_mode);
-                       Display::printResult(Display::SUCCESS, logStr);
+                       if (lstat(entry.c_str(), &st) == 0) {
+                               if (chmod(entry.c_str(), st.st_mode) == 0)
+                                       Display::printResult(Display::SUCCESS, logStr);
+                               else
+                                       Display::printResult(Display::FAIL, logStr);
+                       } else {
+                               Display::printResult(Display::FAIL, logStr);
+                       }
                }
        }