Bug 1530240 - (test code only) - TestAUSHelper.cpp coverity fixes. r=mhowell
authorRobert Strong <robert.bugzilla@gmail.com>
Mon, 25 Feb 2019 17:50:58 -0800
changeset 518993 c04aa3d7aa8e46bd547d8e0e1a1ac7a88ae3e4c4
parent 518992 3c030119c0dcf447dd1afe7165b2d6846a0bc7f0
child 518994 13457088df2c75726f901023a31c61d63cf3bcb5
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmhowell
bugs1530240
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1530240 - (test code only) - TestAUSHelper.cpp coverity fixes. r=mhowell Remove unnecessary access check in CheckMsg Change call to strcpy to NS_tsnprintf Add checks for the return value of mkdir, rmdir, chmod, lstat, and unlink
toolkit/mozapps/update/tests/TestAUSHelper.cpp
--- a/toolkit/mozapps/update/tests/TestAUSHelper.cpp
+++ b/toolkit/mozapps/update/tests/TestAUSHelper.cpp
@@ -57,16 +57,17 @@ typedef char NS_tchar;
 #  define NS_tstat stat
 #  define NS_tgetcwd getcwd
 #  define NS_tfputs fputs
 #  define LOG_S "%s"
 #endif
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <errno.h>
 #include <string.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
 #ifndef MAXPATHLEN
 #  ifdef PATH_MAX
 #    define MAXPATHLEN PATH_MAX
 #  elif defined(MAX_PATH)
@@ -87,20 +88,16 @@ static void WriteMsg(const NS_tchar *pat
   }
 
   fprintf(outFP, "%s\n", status);
   fclose(outFP);
   outFP = nullptr;
 }
 
 static bool CheckMsg(const NS_tchar *path, const char *expected) {
-  if (NS_taccess(path, F_OK)) {
-    return false;
-  }
-
   FILE *inFP = NS_tfopen(path, NS_T("rb"));
   if (!inFP) {
     return false;
   }
 
   struct stat ms;
   if (fstat(fileno(inFP), &ms)) {
     fclose(inFP);
@@ -140,17 +137,18 @@ int NS_main(int argc, NS_tchar **argv) {
     if (!NS_tstrcmp(argv[1], NS_T("post-update-async")) ||
         !NS_tstrcmp(argv[1], NS_T("post-update-sync"))) {
       NS_tchar exePath[MAXPATHLEN];
 #ifdef XP_WIN
       if (!::GetModuleFileNameW(0, exePath, MAXPATHLEN)) {
         return 1;
       }
 #else
-      strcpy(exePath, argv[0]);
+      NS_tsnprintf(exePath, sizeof(exePath) / sizeof(exePath[0]), NS_T("%s"),
+                   argv[0]);
 #endif
       NS_tchar runFilePath[MAXPATHLEN];
       NS_tsnprintf(runFilePath, sizeof(runFilePath) / sizeof(runFilePath[0]),
                    NS_T("%s.running"), exePath);
 #ifdef XP_WIN
       if (!NS_taccess(runFilePath, F_OK)) {
         // This makes it possible to check if the post update process was
         // launched twice which happens when the service performs an update.
@@ -233,68 +231,88 @@ int NS_main(int argc, NS_tchar **argv) {
 #endif
   }
 
   if (!NS_tstrcmp(argv[1], NS_T("setup-symlink"))) {
 #ifdef XP_UNIX
     NS_tchar path[MAXPATHLEN];
     NS_tsnprintf(path, sizeof(path) / sizeof(path[0]), NS_T("%s/%s"),
                  NS_T("/tmp"), argv[2]);
-    mkdir(path, 0755);
+    if (mkdir(path, 0755)) {
+      return 1;
+    }
     NS_tsnprintf(path, sizeof(path) / sizeof(path[0]), NS_T("%s/%s/%s"),
                  NS_T("/tmp"), argv[2], argv[3]);
-    mkdir(path, 0755);
+    if (mkdir(path, 0755)) {
+      return 1;
+    }
     NS_tsnprintf(path, sizeof(path) / sizeof(path[0]), NS_T("%s/%s/%s/%s"),
                  NS_T("/tmp"), argv[2], argv[3], argv[4]);
     FILE *file = NS_tfopen(path, NS_T("w"));
     if (file) {
       NS_tfputs(NS_T("test"), file);
       fclose(file);
     }
     if (symlink(path, argv[5]) != 0) {
       return 1;
     }
     NS_tsnprintf(path, sizeof(path) / sizeof(path[0]), NS_T("%s/%s"),
                  NS_T("/tmp"), argv[2]);
     if (argc > 6 && !NS_tstrcmp(argv[6], NS_T("change-perm"))) {
-      chmod(path, 0644);
+      if (chmod(path, 0644)) {
+        return 1;
+      }
     }
     return 0;
 #else
     // Not implemented on non-Unix platforms
     return 1;
 #endif
   }
 
   if (!NS_tstrcmp(argv[1], NS_T("remove-symlink"))) {
 #ifdef XP_UNIX
+    // The following can be called at the start of a test in case these symlinks
+    // need to be removed if they already exist and at the end of a test to
+    // remove the symlinks created by the test so ignore file doesn't exist
+    // errors.
     NS_tchar path[MAXPATHLEN];
     NS_tsnprintf(path, sizeof(path) / sizeof(path[0]), NS_T("%s/%s"),
                  NS_T("/tmp"), argv[2]);
-    chmod(path, 0755);
+    if (chmod(path, 0755) && errno != ENOENT) {
+      return 1;
+    }
     NS_tsnprintf(path, sizeof(path) / sizeof(path[0]), NS_T("%s/%s/%s/%s"),
                  NS_T("/tmp"), argv[2], argv[3], argv[4]);
-    unlink(path);
+    if (unlink(path) && errno != ENOENT) {
+      return 1;
+    }
     NS_tsnprintf(path, sizeof(path) / sizeof(path[0]), NS_T("%s/%s/%s"),
                  NS_T("/tmp"), argv[2], argv[3]);
-    rmdir(path);
+    if (rmdir(path) && errno != ENOENT) {
+      return 1;
+    }
     NS_tsnprintf(path, sizeof(path) / sizeof(path[0]), NS_T("%s/%s"),
                  NS_T("/tmp"), argv[2]);
-    rmdir(path);
+    if (rmdir(path) && errno != ENOENT) {
+      return 1;
+    }
     return 0;
 #else
     // Not implemented on non-Unix platforms
     return 1;
 #endif
   }
 
   if (!NS_tstrcmp(argv[1], NS_T("check-symlink"))) {
 #ifdef XP_UNIX
     struct stat ss;
-    lstat(argv[2], &ss);
+    if (lstat(argv[2], &ss)) {
+      return 1;
+    }
     return S_ISLNK(ss.st_mode) ? 0 : 1;
 #else
     // Not implemented on non-Unix platforms
     return 1;
 #endif
   }
 
   if (!NS_tstrcmp(argv[1], NS_T("wait-for-service-stop"))) {
@@ -425,16 +443,19 @@ int NS_main(int argc, NS_tchar **argv) {
 
   {
     // Command line argument test helper section
     NS_tchar logFilePath[MAXPATHLEN];
     NS_tsnprintf(logFilePath, sizeof(logFilePath) / sizeof(logFilePath[0]),
                  NS_T("%s"), argv[2]);
 
     FILE *logFP = NS_tfopen(logFilePath, NS_T("wb"));
+    if (!logFP) {
+      return 1;
+    }
     for (int i = 1; i < argc; ++i) {
       fprintf(logFP, LOG_S "\n", argv[i]);
     }
 
     fclose(logFP);
     logFP = nullptr;
   }