Fix for lp:1123153 (poor use of strtol).
[awesomized/libmemcached] / clients / memslap.cc
index 295ea0dbd74e1c2552527523549fdb9f3c55518e..e66ab0fd584dbba094d675e55ef127b5083680e3 100644 (file)
@@ -39,6 +39,7 @@
 #include <mem_config.h>
 
 #include <cassert>
+#include <cerrno>
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
@@ -50,7 +51,6 @@
 #include <sys/stat.h>
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/types.h>
 #include <unistd.h>
 
 #include <iostream>
@@ -224,7 +224,15 @@ int main(int argc, char *argv[])
   pthread_mutex_init(&sleeper_mutex, NULL);
   pthread_cond_init(&sleep_threshhold, NULL);
 
-  scheduler(servers, &conclusion);
+  int error_code= EXIT_SUCCESS;
+  try {
+    scheduler(servers, &conclusion);
+  }
+  catch(std::exception& e)
+  {
+    std::cerr << "Died with exception: " << e.what() << std::endl;
+    error_code= EXIT_FAILURE;
+  }
 
   free(opt_servers);
 
@@ -233,7 +241,7 @@ int main(int argc, char *argv[])
   conclusions_print(&conclusion);
   memcached_server_list_free(servers);
 
-  return EXIT_SUCCESS;
+  return error_code;
 }
 
 void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
@@ -305,7 +313,7 @@ void scheduler(memcached_server_st *servers, conclusions_st *conclusion)
 
   pthread_t *threads= new  (std::nothrow) pthread_t[opt_concurrency];
 
-  if (not threads)
+  if (threads == NULL)
   {
     exit(EXIT_FAILURE);
   }
@@ -460,15 +468,33 @@ void options_parse(int argc, char *argv[])
       break;
 
     case OPT_SLAP_CONCURRENCY:
+      errno= 0;
       opt_concurrency= (unsigned int)strtoul(optarg, (char **)NULL, 10);
+      if (errno != 0)
+      {
+        fprintf(stderr, "Invalid value for concurrency: %s\n", optarg);
+        exit(EXIT_FAILURE);
+      }
       break;
 
     case OPT_SLAP_EXECUTE_NUMBER:
+      errno= 0;
       opt_execute_number= (unsigned int)strtoul(optarg, (char **)NULL, 10);
+      if (errno != 0)
+      {
+        fprintf(stderr, "Invalid value for execute: %s\n", optarg);
+        exit(EXIT_FAILURE);
+      }
       break;
 
     case OPT_SLAP_INITIAL_LOAD:
+      errno= 0;
       opt_createial_load= (unsigned int)strtoul(optarg, (char **)NULL, 10);
+      if (errno != 0)
+      {
+        fprintf(stderr, "Invalid value for initial load: %s\n", optarg);
+        exit(EXIT_FAILURE);
+      }
       break;
 
     case OPT_QUIET: