]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mon: check for int parsing errors in osdmon
authorSage Weil <sage@inktank.com>
Fri, 24 Aug 2012 23:02:02 +0000 (16:02 -0700)
committerSage Weil <sage@inktank.com>
Fri, 24 Aug 2012 23:02:02 +0000 (16:02 -0700)
Signed-off-by: Sage Weil <sage@inktank.com>
src/mon/Monitor.cc
src/mon/Monitor.h
src/mon/OSDMonitor.cc

index f08f5b659d25614d8a225afa693b717f7f205378..f73de532ad8dd32a2e64e7b71497aefe3d116a01 100644 (file)
@@ -90,6 +90,24 @@ CompatSet get_ceph_mon_feature_compat_set()
                   ceph_mon_feature_incompat);
 }
 
+long parse_pos_long(const char *s, ostream *pss)
+{
+  char *e = 0;
+  long r = strtol(s, &e, 10);
+  if (e == s || e == 0) {
+    if (pss)
+      *pss << "unable to parse positive integer '" << s << "'";
+    return -1;
+  }
+  if (r < 0 || r == LONG_MAX || r == LONG_MIN) {
+    if (pss)
+      *pss << "unable to parse positive integer '" << s << "'";
+    return -1;
+  }
+  return r;
+}
+
+
 Monitor::Monitor(CephContext* cct_, string nm, MonitorStore *s, Messenger *m, MonMap *map) :
   Dispatcher(cct_),
   name(nm),
index a87b726e4c2c0c0ca00cc44c254c2294ababf814..9e92de6be7f06b51a417f9b2d923bf0ca60ef709 100644 (file)
@@ -443,5 +443,7 @@ private:
 
 #define CEPH_MON_FEATURE_INCOMPAT_BASE CompatSet::Feature (1, "initial feature set (~v.18)")
 
+long parse_pos_long(const char *s, ostream *pss = NULL);
+
 
 #endif
index a94a159336ae2ba87c47d119c0551fbcf074e6c5..88590c254a1739bde52a4761c6b9c2156018753e 100644 (file)
@@ -1536,9 +1536,14 @@ bool OSDMonitor::preprocess_command(MMonCommand *m)
          break;
        else if (ceph_argparse_witharg(args, i, &val, "-f", "--format", (char*)NULL))
          format = val;
-       else if (!epoch)
-         epoch = atoi(*i++);
-       else
+       else if (!epoch) {
+         long l = parse_pos_long(*i++, &ss);
+         if (l < 0) {
+           r = -EINVAL;
+           goto out;
+         }
+         epoch = l;
+       } else
          i++;
       }
 
@@ -1839,10 +1844,14 @@ bool OSDMonitor::prepare_command(MMonCommand *m)
     else if (m->cmd.size() >= 6 && m->cmd[1] == "crush" && m->cmd[2] == "set") {
       do {
        // osd crush set <id> <name> <weight> [<loc1> [<loc2> ...]]
-       int id = atoi(m->cmd[3].c_str());
+       int id = parse_pos_long(m->cmd[3].c_str(), &ss);
+       if (id < 0) {
+         err = -EINVAL;
+         goto out;
+       }
        if (!osdmap.exists(id)) {
          err = -ENOENT;
-         ss << "osd." << id << " does not exist.  create it before updating the crush map";
+         ss << "osd." << m->cmd[3] << " does not exist.  create it before updating the crush map";
          goto out;
        }
 
@@ -2004,7 +2013,11 @@ bool OSDMonitor::prepare_command(MMonCommand *m)
       } while (false);
     }
     else if (m->cmd[1] == "setmaxosd" && m->cmd.size() > 2) {
-      int newmax = atoi(m->cmd[2].c_str());
+      int newmax = parse_pos_long(m->cmd[2].c_str(), &ss);
+      if (newmax < 0) {
+       err = -EINVAL;
+       goto out;
+      }
       if (newmax < osdmap.crush->get_max_devices()) {
        err = -ERANGE;
        ss << "cannot set max_osd to " << newmax << " which is < crush max_devices "
@@ -2536,7 +2549,11 @@ bool OSDMonitor::prepare_command(MMonCommand *m)
             (m->cmd[1] == "reweight-by-utilization")) {
       int oload = 120;
       if (m->cmd.size() > 2) {
-       oload = atoi(m->cmd[2].c_str());
+       oload = parse_pos_long(m->cmd[2].c_str(), &ss);
+       if (oload < 0) {
+         err = -EINVAL;
+         goto out;
+       }
       }
       string out_str;
       err = reweight_by_utilization(oload, out_str);
@@ -2553,7 +2570,12 @@ bool OSDMonitor::prepare_command(MMonCommand *m)
       }
     }
     else if (m->cmd.size() == 3 && m->cmd[1] == "thrash") {
-      thrash_map = atoi(m->cmd[2].c_str());
+      long l = parse_pos_long(m->cmd[2].c_str(), &ss);
+      if (l < 0) {
+       err = -EINVAL;
+       goto out;
+      }
+      thrash_map = l;
       ss << "will thrash map for " << thrash_map << " epochs";
       ret = thrash();
       err = 0;