diff liboctave/oct-inttypes.h @ 8193:5fd507839b76

remove the int->real conversion code vulnerability to compiler optimization
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 08 Oct 2008 08:01:46 +0200
parents 9cb73236e552
children cf59d542f33e
line wrap: on
line diff
--- a/liboctave/oct-inttypes.h
+++ b/liboctave/oct-inttypes.h
@@ -267,11 +267,12 @@
   static S 
   compute_threshold (S val, T orig_val)
     { 
-      if (static_cast <T> (val) != orig_val)
-        return val;
-      else
-        // Next number away from zero.
-        return val * (static_cast<S> (1.0) + std::numeric_limits<S>::epsilon ()); 
+      val = xround (val); // Fool optimizations (maybe redundant)
+      // If val is even, but orig_val is odd, we're one unit off.
+      if (orig_val % 2 && val / 2 == xround (val / 2))
+        // TODO: is this always correct?
+        val *= (static_cast<S>(1) - (std::numeric_limits<S>::epsilon () / 2)); 
+      return val;
     }
   
 public:
@@ -288,12 +289,12 @@
           fnan = true;
           return static_cast<T> (0);
         }
-      else if (value <= thmin)
+      else if (value < thmin)
         {
-          octave_int_base<T>::ftrunc = true;
+          ftrunc = true;
           return min_val ();
         }
-      else if (value >= thmax)
+      else if (value > thmax)
         {
           ftrunc = true;
           return max_val ();