]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
crushtester: avoid divide by zero
authorSage Weil <sage@inktank.com>
Fri, 28 Sep 2012 13:56:20 +0000 (06:56 -0700)
committerSage Weil <sage@inktank.com>
Fri, 28 Sep 2012 20:18:04 +0000 (13:18 -0700)
CID 716904: Division or modulo by zero (DIVIDE_BY_ZERO)
At (4): In expression "(float)weight[i] / (float)total_weight", division by expression "total_weight" which may be zero has undefined behavior.

At (8): On this path, function call "this->crush->get_max_devices()" has return value of 0
CID 716905: Division or modulo by zero (DIVIDE_BY_ZERO)
At (9): In expression "lrand48() % this->crush->get_max_devices()" modulo by expression "this->crush->get_max_devices()" which may be zero has undefined behavior.

Signed-off-by: Sage Weil <sage@inktank.com>
src/crush/CrushTester.cc

index a125735cd69fbcee61be952c8a8155b325b2e250..6acde9f03b912ea06c64c194e6a53ccfd4904274 100644 (file)
@@ -242,10 +242,15 @@ int CrushTester::random_placement(int ruleno, vector<int>& out, int maxout, vect
   for (unsigned i = 0; i < weight.size(); i++)
     total_weight += weight[i];
 
+  if (total_weight == 0 ||
+      crush.get_max_devices() == 0)
+    return -EINVAL;
+
   // compute each device's proportional weight
   vector<float> proportional_weights( weight.size() );
-  for (unsigned i = 0; i < weight.size(); i++)
+  for (unsigned i = 0; i < weight.size(); i++) {
     proportional_weights[i] = (float) weight[i] / (float) total_weight;
+  }
 
   // determine the real maximum number of devices to return
   int devices_requested = min(maxout, get_maximum_affected_by_rule(ruleno));