Fix a common mistake about errno(3) usage
authorSoo-Hyun Choi <sh9.choi@samsung.com>
Thu, 26 Sep 2013 05:52:30 +0000 (14:52 +0900)
committerGerrit Code Review <gerrit@gerrit.vlan144.tizendev.org>
Fri, 27 Sep 2013 05:31:20 +0000 (05:31 +0000)
[Issue#]   N/A
[Problem]  errno may have been changed by other system call.
[Cause]    errno should be preserved.
[Solution] Save errno to a local variable

[Remarks] ERRNO(3)

A common mistake is to do

    if (somecall() == -1) {
        printf("somecall() failed\n");
        if (errno == ...) { ... }
    }

where  errno  no  longer  needs  to have the value it had upon return from somecall()
(i.e., it may have been changed by the printf(3)).
If the value of errno should be preserved across a library call, it must be saved:

if (somecall() == -1) {
    int errsv = errno;
    printf("somecall() failed\n");
    if (errsv == ...) { ... }
}

Change-Id: Ib407b8a7b1cd13d37c3f182c4c28f059bb1749ea

src/jobs/widget_install/task_manifest_file.cpp

index cb7080d..4e6699b 100644 (file)
@@ -191,9 +191,11 @@ void TaskManifestFile::stepCreateExecFile()
         std::string boxSymlink = m_context.locations->getExecFile();
         boxSymlink += ".d-box";
 
+        errno = 0;
         if (symlink(boxExec.c_str(), boxSymlink.c_str()) != 0) {
-            if (errno) {
-                _E("Failed to make a symbolic name for a file [%s]", DPL::GetErrnoString(errno).c_str());
+            int error = errno;
+            if (error) {
+                _E("Failed to make a symbolic name for a file [%s]", DPL::GetErrnoString(error).c_str());
             }
         }
     }