Pārlūkot izejas kodu

Don't allow empty string for --rpc-secret option

Tatsuhiro Tsujikawa 11 gadi atpakaļ
vecāks
revīzija
1a24020e63
3 mainītis faili ar 16 papildinājumiem un 4 dzēšanām
  1. 4 3
      src/OptionHandlerFactory.cc
  2. 10 1
      src/OptionHandlerImpl.cc
  3. 2 0
      src/OptionHandlerImpl.h

+ 4 - 3
src/OptionHandlerFactory.cc

@@ -875,11 +875,12 @@ std::vector<OptionHandler*> OptionHandlerFactory::createOptionHandlers()
     handlers.push_back(op);
   }
   {
-    OptionHandler* op(new DefaultOptionHandler
-                      (PREF_RPC_SECRET,
-                       TEXT_RPC_SECRET));
+    DefaultOptionHandler* op(new DefaultOptionHandler
+                             (PREF_RPC_SECRET,
+                              TEXT_RPC_SECRET));
     op->addTag(TAG_RPC);
     op->setEraseAfterParse(true);
+    op->setAllowEmpty(false);
     handlers.push_back(op);
   }
   {

+ 10 - 1
src/OptionHandlerImpl.cc

@@ -287,7 +287,8 @@ DefaultOptionHandler::DefaultOptionHandler
  char shortName)
   : AbstractOptionHandler(pref, description, defaultValue, argType,
                           shortName),
-    possibleValuesString_(possibleValuesString)
+    possibleValuesString_(possibleValuesString),
+    allowEmpty_(true)
 {}
 
 DefaultOptionHandler::~DefaultOptionHandler() {}
@@ -295,6 +296,9 @@ DefaultOptionHandler::~DefaultOptionHandler() {}
 void DefaultOptionHandler::parseArg(Option& option, const std::string& optarg)
   const
 {
+  if(!allowEmpty_ && optarg.empty()) {
+    throw DL_ABORT_EX("Empty string is not allowed");
+  }
   option.put(pref_, optarg);
 }
 
@@ -303,6 +307,11 @@ std::string DefaultOptionHandler::createPossibleValuesString() const
   return possibleValuesString_;
 }
 
+void DefaultOptionHandler::setAllowEmpty(bool allow)
+{
+  allowEmpty_ = allow;
+}
+
 CumulativeOptionHandler::CumulativeOptionHandler
 (PrefPtr pref,
  const char* description,

+ 2 - 0
src/OptionHandlerImpl.h

@@ -127,6 +127,7 @@ public:
 class DefaultOptionHandler : public AbstractOptionHandler {
 private:
   std::string possibleValuesString_;
+  bool allowEmpty_;
 public:
   DefaultOptionHandler(PrefPtr pref,
                        const char* description = NO_DESCRIPTION,
@@ -138,6 +139,7 @@ public:
   virtual void parseArg(Option& option, const std::string& optarg) const
     CXX11_OVERRIDE;
   virtual std::string createPossibleValuesString() const CXX11_OVERRIDE;
+  void setAllowEmpty(bool allow);
 };
 
 class CumulativeOptionHandler : public AbstractOptionHandler {