Replace usage of strerror with strerror_r and localtime with localtime_r 15/215115/4
authorOskar Chodowicz <o.chodowicz@samsung.com>
Tue, 1 Oct 2019 15:07:31 +0000 (17:07 +0200)
committerJongmin Lee <jm105.lee@samsung.com>
Mon, 7 Oct 2019 08:17:09 +0000 (17:17 +0900)
This patch eliminates potential thread safety risks.

Change-Id: I9dbbe18a1782068d20268b723f7736fbfebfb85c

src/ScreenshotActivity.cpp
src/batch/Dlog.cpp

index 9868b0a1e1d64a857e378b6d309639e5dc374546..929f5fdac5e1f2b71db04c39a055b3ec42877a65 100644 (file)
@@ -139,6 +139,12 @@ std::string ScreenshotActivity::generateFileName()
        auto formated = std::chrono::system_clock::to_time_t(now);
 
        std::stringstream ss;
-       ss << std::put_time(std::localtime(&formated), "%Y%m%d-%H%M%S") << ".png";
+       tm time, *ret;
+       ret = localtime_r(&formated, &time);
+       if (ret)
+               ss << std::put_time(&time, "%Y%m%d-%H%M%S") << ".png";
+       else
+               ss << "screenshot.png";
+
        return ss.str();
 }
index f1dd1e45900efe1dce07267df50224559c75701d..dfb7bc13cf80b200a01db182ae74d63754491beb 100644 (file)
@@ -60,16 +60,17 @@ void Dlog::terminate()
 bool Dlog::start()
 {
        int closingFd[2] = { -1, -1 };
+       char buf[2048];
 
        if (pipe(closingFd) == -1) {
-               ERROR("failed to create pipes (%s)", strerror(errno));
+               ERROR("failed to create pipes (%s)", strerror_r(errno, buf, sizeof(buf)));
                return false;
        }
 
        auto const dlogFilePath = "/opt/usr/home/owner/media/Documents/qe9fh483fb4i3fb8we3_universal_switch_dlog.txt";
        int dlogFile = open(dlogFilePath, O_RDONLY);
        if (dlogFile < 0) {
-               ERROR("failed to open file '%s' (%s)", dlogFilePath, strerror(errno));
+               ERROR("failed to open file '%s' (%s)", dlogFilePath, strerror_r(errno, buf, sizeof(buf)));
                return false;
        }
 
@@ -78,6 +79,7 @@ bool Dlog::start()
                        std::vector<char> tmp(1024 * 128);
                        std::string line;
                        size_t pos = 0;
+                       char buf[2048];
 
                        while (true)
                        {
@@ -100,10 +102,10 @@ bool Dlog::start()
                                        ASSERT(pos + 1024 <= tmp.size());
                                        auto count = read(dlogFile, tmp.data() + pos, 1024);
                                        if (count < 0) {
-                                               ERROR("failed to read from pipe (%s)", strerror(errno));
+                                               ERROR("failed to read from pipe (%s)", strerror_r(errno, buf, sizeof(buf)));
                                                break;
                                        } else if (count == 0) {
-                                               ERROR("other side has close the pipe (%s)", strerror(errno));
+                                               ERROR("other side has close the pipe (%s)", strerror_r(errno, buf, sizeof(buf)));
                                                break;
                                        }
                                        {