]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common/config: detect overflow of float values 4889/head
authorKefu Chai <kchai@redhat.com>
Wed, 29 Apr 2015 10:28:18 +0000 (03:28 -0700)
committerAbhishek Lekshmanan <abhishek.lekshmanan@ril.com>
Sun, 7 Jun 2015 09:02:50 +0000 (14:32 +0530)
Signed-off-by: Kefu Chai <kchai@redhat.com>
(cherry picked from commit 1ff409ef8d022a1a84d034bd3db976c4d769e993)

src/common/config.cc
src/test/daemon_config.cc

index f2460c636e27962d75f9fbc65329c3eb0e18bec8..5e923e6fa658ca143675c8851f61a5f8b0490ab4 100644 (file)
@@ -894,12 +894,22 @@ int md_config_t::set_val_raw(const char *val, const config_option *opt)
     case OPT_STR:
       *(std::string*)opt->conf_ptr(this) = val ? val : "";
       return 0;
-    case OPT_FLOAT:
-      *(float*)opt->conf_ptr(this) = atof(val);
+    case OPT_FLOAT: {
+      std::string err;
+      float f = strict_strtof(val, &err);
+      if (!err.empty())
+       return -EINVAL;
+      *(float*)opt->conf_ptr(this) = f;
       return 0;
-    case OPT_DOUBLE:
-      *(double*)opt->conf_ptr(this) = atof(val);
+    }
+    case OPT_DOUBLE: {
+      std::string err;
+      double f = strict_strtod(val, &err);
+      if (!err.empty())
+       return -EINVAL;
+      *(double*)opt->conf_ptr(this) = f;
       return 0;
+    }
     case OPT_BOOL:
       if (strcasecmp(val, "false") == 0)
        *(bool*)opt->conf_ptr(this) = false;
index 2968b3514a2faf756a9d54db75d8e2b595d59bc4..6d32e150ccc49b97a34a385cd5ff8f3144bc2b61 100644 (file)
@@ -360,6 +360,25 @@ TEST(DaemonConfig, InvalidIntegers) {
   }
 }
 
+TEST(DaemonConfig, InvalidFloats) {
+  {
+    double bad_value = 2 * (double)std::numeric_limits<float>::max();
+    string str = boost::lexical_cast<string>(-bad_value);
+    int ret = g_ceph_context->_conf->set_val("log_stop_at_utilization", str);
+    ASSERT_EQ(ret, -EINVAL);
+  }
+  {
+    double bad_value = 2 * (double)std::numeric_limits<float>::max();
+    string str = boost::lexical_cast<string>(bad_value);
+    int ret = g_ceph_context->_conf->set_val("log_stop_at_utilization", str);
+    ASSERT_EQ(ret, -EINVAL);
+  }
+  {
+    int ret = g_ceph_context->_conf->set_val("log_stop_at_utilization", "not a float");
+    ASSERT_EQ(ret, -EINVAL);
+  }
+}
+
 /*
  * Local Variables:
  * compile-command: "cd .. ; make unittest_daemon_config && ./unittest_daemon_config"