Переглянути джерело

add argument bound checks for commands which require them

Missingmew 5 роки тому
батько
коміт
d822099049
1 змінених файлів з 82 додано та 52 видалено
  1. 82 52
      cli/src/cmdman.cpp

+ 82 - 52
cli/src/cmdman.cpp

@@ -109,18 +109,25 @@ CmdMan::CmdRet CmdMan::cmdPut(vector<string> args) {
   CmdRet retval;
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
-
-  bool opened = fileman.openPut(args[0]);
-  root["command"] = "put";
-  root["file"] = fileman.getPutName();
-  if (opened) {
-    root["size"] = fileman.getPutSize();
-    root["chunks"] = fileman.getPutChunks();
-    retval.type = send;
-  } else {
-    retval.type = error;
-    root["accept"] = false;
-    root["error"] = "couldnt open local file \"" + args[0] + "\"";
+	
+  if(args.size() < 1) {
+	  retval.type = error;
+	  root["accept"] = false;
+	  root["error"] = "not enough arguments, at least 1 argument required";
+  }
+  else {
+	  bool opened = fileman.openPut(args[0]);
+	  root["command"] = "put";
+	  root["file"] = fileman.getPutName();
+	  if (opened) {
+	    root["size"] = fileman.getPutSize();
+	    root["chunks"] = fileman.getPutChunks();
+	    retval.type = send;
+	  } else {
+	    retval.type = error;
+	    root["accept"] = false;
+	    root["error"] = "couldnt open local file \"" + args[0] + "\"";
+	  }
   }
 
   retval.msg = root;
@@ -149,16 +156,23 @@ CmdMan::CmdRet CmdMan::cmdGet(vector<string> args) {
   CmdRet retval;
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
-
-  bool opened = fileman.openGet(args[0]);
-  root["command"] = "get";
-  root["file"] = fileman.getGetName();
-  if (opened) {
-    retval.type = send;
-  } else {
-    retval.type = error;
-    root["accept"] = false;
-    root["error"] = "local file \"" + args[0] + "\" already exists";
+	
+  if(args.size() < 1) {
+	  retval.type = error;
+	  root["accept"] = false;
+	  root["error"] = "not enough arguments, at least 1 argument required";
+  }
+  else {
+	  bool opened = fileman.openGet(args[0]);
+	  root["command"] = "get";
+	  root["file"] = fileman.getGetName();
+	  if (opened) {
+	    retval.type = send;
+	  } else {
+	    retval.type = error;
+	    root["accept"] = false;
+	    root["error"] = "local file \"" + args[0] + "\" already exists";
+	  }
   }
 
   retval.msg = root;
@@ -258,21 +272,29 @@ CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
   CmdRet retval;
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
-
-  if (loginpossible) {
-    dologin = true;
-    loginpossible = false;
-    root["user"] = args[0];
-    root["pass"] = args[1];
-    root["login"] = true;
-    root["cancel"] = false;
-    retval.type = send;
-  } else {
-    root["command"] = "login";
-    root["error"] = "Login not possible, because you already requested a login "
-                    "or you are logged in";
-    root["accept"] = false;
-    retval.type = error;
+	
+  if(args.size() < 2) {
+	  retval.type = error;
+	    root["command"] = "login";
+	  root["accept"] = false;
+	  root["error"] = "not enough arguments, at least 2 argument required";
+  }
+  else {
+	  if (loginpossible) {
+	    dologin = true;
+	    loginpossible = false;
+	    root["user"] = args[0];
+	    root["pass"] = args[1];
+	    root["login"] = true;
+	    root["cancel"] = false;
+	    retval.type = send;
+	  } else {
+	    root["command"] = "login";
+	    root["error"] = "Login not possible, because you already requested a login "
+			    "or you are logged in";
+	    root["accept"] = false;
+	    retval.type = error;
+	  }
   }
 
   retval.msg = root;
@@ -283,21 +305,29 @@ CmdMan::CmdRet CmdMan::cmdSignup(vector<string> args) {
   CmdRet retval;
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
-
-  if (loginpossible) {
-    dosignup = true;
-    loginpossible = false;
-    root["user"] = args[0];
-    root["pass"] = args[1];
-    root["login"] = false;
-    root["cancel"] = false;
-    retval.type = send;
-  } else {
-    root["command"] = "signup";
-    root["error"] = "Signup not possible, because you already requested a "
-                    "login or you are logged in";
-    root["accept"] = false;
-    retval.type = error;
+	
+  if(args.size() < 2) {
+	  retval.type = error;
+	    root["command"] = "signup";
+	  root["accept"] = false;
+	  root["error"] = "not enough arguments, at least 2 argument required";
+  }
+  else {
+	  if (loginpossible) {
+	    dosignup = true;
+	    loginpossible = false;
+	    root["user"] = args[0];
+	    root["pass"] = args[1];
+	    root["login"] = false;
+	    root["cancel"] = false;
+	    retval.type = send;
+	  } else {
+	    root["command"] = "signup";
+	    root["error"] = "Signup not possible, because you already requested a "
+			    "login or you are logged in";
+	    root["accept"] = false;
+	    retval.type = error;
+	  }
   }
 
   retval.msg = root;