Fix for lp:1123153 (poor use of strtol).
[awesomized/libmemcached] / libtest / main.cc
index 6721b5aa14c32363a80eec88d26540c1cb9f089d..80ffa769344d5ac89c5d72283c0c37cfa3a23b1b 100644 (file)
@@ -34,7 +34,7 @@
  *
  */
 
-#include <config.h>
+#include "libtest/yatlcon.h"
 #include <libtest/common.h>
 
 #include <cassert>
@@ -132,7 +132,7 @@ int main(int argc, char *argv[])
     {
       { "version", no_argument, NULL, OPT_LIBYATL_VERSION },
       { "quiet", no_argument, NULL, OPT_LIBYATL_QUIET },
-      { "repeat", no_argument, NULL, OPT_LIBYATL_REPEAT },
+      { "repeat", required_argument, NULL, OPT_LIBYATL_REPEAT },
       { "collection", required_argument, NULL, OPT_LIBYATL_MATCH_COLLECTION },
       { "wildcard", required_argument, NULL, OPT_LIBYATL_MATCH_WILDCARD },
       { "massive", no_argument, NULL, OPT_LIBYATL_MASSIVE },
@@ -158,7 +158,14 @@ int main(int argc, char *argv[])
         break;
 
       case OPT_LIBYATL_REPEAT:
+        errno= 0;
         opt_repeat= strtoul(optarg, (char **) NULL, 10);
+        if (errno != 0)
+        {
+          Error << "unknown value passed to --repeat: `" << optarg << "`";
+          exit(EXIT_FAILURE);
+        }
+
         break;
 
       case OPT_LIBYATL_MATCH_COLLECTION:
@@ -186,9 +193,16 @@ int main(int argc, char *argv[])
 
   srandom((unsigned int)time(NULL));
 
-  if (bool(getenv("YATL_REPEAT")) and (strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10) > 1))
+  errno= 0;
+  if (bool(getenv("YATL_REPEAT")))
   {
+    errno= 0;
     opt_repeat= strtoul(getenv("YATL_REPEAT"), (char **) NULL, 10);
+    if (errno != 0)
+    {
+      Error << "ENV YATL_REPEAT passed an invalid value: `" << getenv("YATL_REPEAT") << "`";
+      exit(EXIT_FAILURE);
+    }
   }
 
   if ((bool(getenv("YATL_QUIET")) and (strcmp(getenv("YATL_QUIET"), "0") == 0)) or opt_quiet)
@@ -220,22 +234,24 @@ int main(int argc, char *argv[])
     is_massive(opt_massive);
   }
 
-  char buffer[1024];
+  libtest::vchar_t tmp_directory;
+  tmp_directory.resize(1024);
   if (getenv("LIBTEST_TMP"))
   {
-    snprintf(buffer, sizeof(buffer), "%s", getenv("LIBTEST_TMP"));
+    snprintf(&tmp_directory[0], tmp_directory.size(), "%s", getenv("LIBTEST_TMP"));
   }
   else
   {
-    snprintf(buffer, sizeof(buffer), "%s", LIBTEST_TEMP);
+    snprintf(&tmp_directory[0], tmp_directory.size(), "%s", LIBTEST_TEMP);
   }
 
-  if (chdir(buffer) == -1)
+  if (chdir(&tmp_directory[0]) == -1)
   {
-    char getcwd_buffer[1024];
-    char *dir= getcwd(getcwd_buffer, sizeof(getcwd_buffer));
+    libtest::vchar_t getcwd_buffer;
+    getcwd_buffer.resize(1024);
+    char *dir= getcwd(&getcwd_buffer[0], getcwd_buffer.size());
 
-    Error << "Unable to chdir() from " << dir << " to " << buffer << " errno:" << strerror(errno);
+    Error << "Unable to chdir() from " << dir << " to " << &tmp_directory[0] << " errno:" << strerror(errno);
     return EXIT_FAILURE;
   }
 
@@ -282,6 +298,7 @@ int main(int argc, char *argv[])
       std::auto_ptr<libtest::Framework> frame(new libtest::Framework(signal, binary_name, collection_to_run, wildcard));
 
       // Run create(), bail on error.
+      try
       {
         switch (frame->create())
         {
@@ -292,10 +309,14 @@ int main(int argc, char *argv[])
           return EXIT_SKIP;
 
         case TEST_FAILURE:
-          std::cerr << "frame->create()" << std::endl;
+          std::cerr << "Could not call frame->create()" << std::endl;
           return EXIT_FAILURE;
         }
       }
+      catch (const libtest::__skipped& e)
+      {
+        return EXIT_SKIP;
+      }
 
       frame->exec();
 
@@ -329,10 +350,7 @@ int main(int argc, char *argv[])
 
       std::ofstream xml_file;
       std::string file_name;
-      if (getenv("LIBTEST_TEMP"))
-      {
-        file_name.append(getenv("LIBTEST_TEMP"));
-      }
+      file_name.append(&tmp_directory[0]);
       file_name.append(frame->name());
       file_name.append(".xml");
       xml_file.open(file_name.c_str(), std::ios::trunc);
@@ -341,17 +359,26 @@ int main(int argc, char *argv[])
       Outn(); // Generate a blank to break up the messages if make check/test has been run
     } while (exit_code == EXIT_SUCCESS and --opt_repeat);
   }
-  catch (libtest::fatal& e)
+  catch (const libtest::__skipped& e)
+  {
+    return EXIT_SKIP;
+  }
+  catch (const libtest::__failure& e)
+  {
+    libtest::stream::make_cout(e.file(), e.line(), e.func()) << e.what();
+    exit_code= EXIT_FAILURE;
+  }
+  catch (const libtest::fatal& e)
   {
     std::cerr << "FATAL:" << e.what() << std::endl;
     exit_code= EXIT_FAILURE;
   }
-  catch (libtest::disconnected& e)
+  catch (const libtest::disconnected& e)
   {
     std::cerr << "Unhandled disconnection occurred:" << e.what() << std::endl;
     exit_code= EXIT_FAILURE;
   }
-  catch (std::exception& e)
+  catch (const std::exception& e)
   {
     std::cerr << "std::exception:" << e.what() << std::endl;
     exit_code= EXIT_FAILURE;