Browse Source

add argument bound checks for command head (+autoformat)

Denys 4 years ago
parent
commit
8e7ce602b8
3 changed files with 16 additions and 6 deletions
  1. 2 1
      cli/include/cmdman.h
  2. 10 3
      cli/src/cmdman.cpp
  3. 4 2
      cli/src/userioman.cpp

+ 2 - 1
cli/include/cmdman.h

@@ -115,7 +115,8 @@ private:
   CmdRet cmdGet(vector<string> args);
   const string descList = "list files available on server";
   CmdRet cmdList(vector<string> args);
-  const string descHead = "request the first four bytes of a file from the server";
+  const string descHead =
+      "request the first four bytes of a file from the server";
   CmdRet cmdHead(vector<string> args);
 
   const string descLogin = "login to the server";

+ 10 - 3
cli/src/cmdman.cpp

@@ -223,8 +223,15 @@ CmdMan::CmdRet CmdMan::cmdHead(vector<string> args) {
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
   Json::Value root;
   root["command"] = "head";
-  root["file"] = args[0];
-  retval.type = send;
+
+  if (args.size() < 1) {
+    retval.type = error;
+    root["accept"] = false;
+    root["error"] = "not enough arguments, at least 1 argument required";
+  } else {
+    root["file"] = args[0];
+    retval.type = send;
+  }
   retval.msg = root;
 
   return retval;
@@ -680,7 +687,7 @@ CmdMan::CmdRet CmdMan::handleHead(Json::Value root) {
     retval.msg = output;
   } else {
     retval.type = print;
-	retval.msg = root;
+    retval.msg = root;
   }
 
   return retval;

+ 4 - 2
cli/src/userioman.cpp

@@ -184,7 +184,9 @@ void UserIoMan::printListdata(Json::Value root) {}
 
 void UserIoMan::printHead(Json::Value root) {
   if (!root["accept"].asBool())
-    std::cout << "Request of the first four bytes failed, server reports: " << root["error"].asString() << std::endl;
+    std::cout << "Request of the first four bytes failed, server reports: "
+              << root["error"].asString() << std::endl;
   else
-    std::cout << "First four bytes of file " << root["file"].asString() << " are: " << root["data"].asString() << std::endl;
+    std::cout << "First four bytes of file " << root["file"].asString()
+              << " are: " << root["data"].asString() << std::endl;
 }