From b0487421d8bd77ec878001c8e3be3909191fe4ca Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@cs.stanford.edu>
Date: Sun, 25 Nov 2012 10:48:18 -0800
Subject: [PATCH] histogram: Replace assertion failures by errors for bin
 adjustment failure.

Some data can trigger some of these assertions, which isn't acceptable
behavior for users.  This commit avoids the problem by replacing the
assertion failures by warning messages.

Bug #36395.
Reported by Mindaugas.
---
 src/math/histogram.c |   21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/src/math/histogram.c b/src/math/histogram.c
index afc4001..1be89ff 100644
--- a/src/math/histogram.c
+++ b/src/math/histogram.c
@@ -108,7 +108,8 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do
       lower_limit--;
       lower_slack += half_bin_width;
     }
-  assert (lower_limit * half_bin_width <= min);
+  if (lower_limit * half_bin_width > min)
+    return 0;
 
   /* However, the upper bound must be extended regardless, because histogram bins
      span the range [lower, upper). In other words, the upper bound must be
@@ -116,7 +117,8 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do
   */
   upper_limit++;;
   upper_slack += half_bin_width;
-  assert (upper_limit * half_bin_width > max);
+  if (upper_limit * half_bin_width <= max)
+    return 0;
 
   /* The range must be an EVEN number of half bin_widths */
   if ( (upper_limit - lower_limit) % 2)
@@ -159,7 +161,8 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do
 
       if (upper_slack > lower_slack)
         {
-          assert (upper_slack > half_bin_width);
+          if (upper_slack <= half_bin_width)
+            return 0;
 
           /* Adjust the range to the left */
           lower_limit --;
@@ -169,7 +172,8 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do
         }
       else
         {
-          assert (lower_slack >= half_bin_width);
+          if (lower_slack < half_bin_width)
+            return 0;
 
           /* Adjust the range to the right */
           lower_limit ++;
@@ -197,8 +201,8 @@ adjust_bin_ranges (double bin_width, double min, double max, double *adj_min, do
   *adj_min = lower_limit * half_bin_width;
   *adj_max = upper_limit * half_bin_width;
 
-  assert (*adj_max > max);
-  assert (*adj_min <= min);
+  if (*adj_max <= max || *adj_min > min)
+    return 0;
 
   return (upper_limit - lower_limit) / 2.0;
 }
@@ -235,7 +239,10 @@ histogram_create (double bin_width, double min, double max)
 
   /* Can this ever happen? */
   if (bins < 1)
-    bins = 1;
+    {
+      msg (MW, _("Failed to construct histogram bin ranges."));
+      return NULL;
+    }
 
   h = xmalloc (sizeof *h);
 
-- 
1.7.10.4

