Quellcode durchsuchen

fixed handling argument count

Denys vor 5 Jahren
Ursprung
Commit
32725d6fcf
2 geänderte Dateien mit 19 neuen und 19 gelöschten Zeilen
  1. 9 4
      cli/src/main.cpp
  2. 10 15
      daemon/src/main.cpp

+ 9 - 4
cli/src/main.cpp

@@ -39,14 +39,14 @@ CMD commands[]{{CMD_HELP, "help", "show help"},
 
 COMMANDTYPES getCommand(char *str, unsigned int isshort) {
   COMMANDTYPES ret = CMD_UNKNOWN;
-  char temp[11] = {0};
+  char temp[COMMANDLEN + 1] = {0};
   if (strlen(str) > COMMANDLEN)
     return ret;
   if (isshort)
-    temp[0] = tolower(str[0]);
+    temp[0] = std::tolower(str[0]);
   else
-    for (int i = 0; i < 10; i++)
-      temp[i] = tolower(str[i]);
+    for (int i = 0; i < COMMANDLEN; i++)
+      temp[i] = std::tolower(str[i]);
 
   for (int i = 0; i < sizeofarr(commands); i++) {
     if (isshort) {
@@ -88,6 +88,11 @@ int main(int argc, char **argv) {
     std::cout << desc;
     exit(1);
   }
+  if (argc < 3) {
+    std::printf("not enough arguments\n");
+    show_help(argv[0]);
+    exit(1);
+  }
 
   // have enough valid arguments
   switch (cmd) {

+ 10 - 15
daemon/src/main.cpp

@@ -19,31 +19,26 @@ private:
 
 public:
   typedef boost::shared_ptr<con_handler> pointer;
-  con_handler(
-      basic_socket_acceptor<ip::tcp>::executor_type
-          &io_service)
+  con_handler(basic_socket_acceptor<ip::tcp>::executor_type &io_service)
       : sock(io_service) {}
 
   static pointer
-  create(basic_socket_acceptor<ip::tcp>::executor_type
-             &io_service) {
+  create(basic_socket_acceptor<ip::tcp>::executor_type &io_service) {
     return pointer(new con_handler(io_service));
   }
 
   tcp::socket &socket() { return sock; }
 
   void start() {
-    sock.async_read_some(
-        buffer(data, max_length),
-        boost::bind(&con_handler::handle_read, shared_from_this(),
-                    placeholders::error,
-                    placeholders::bytes_transferred));
+    sock.async_read_some(buffer(data, max_length),
+                         boost::bind(&con_handler::handle_read,
+                                     shared_from_this(), placeholders::error,
+                                     placeholders::bytes_transferred));
 
-    sock.async_write_some(
-        buffer(message, max_length),
-        boost::bind(&con_handler::handle_write, shared_from_this(),
-                    placeholders::error,
-                    placeholders::bytes_transferred));
+    sock.async_write_some(buffer(message, max_length),
+                          boost::bind(&con_handler::handle_write,
+                                      shared_from_this(), placeholders::error,
+                                      placeholders::bytes_transferred));
   }
 
   void handle_read(const boost::system::error_code &err,