r11127 - in libprelude/trunk/src: . idmef-wrappers-gen

[email protected]
Newsgroups gmane.comp.security.ids.prelude.cvs
Message-ID <[email protected]>
Author: yoann
Date: 2009-04-08 15:09:27 +0200 (Wed, 08 Apr 2009)
New Revision: 11127

Modified:
   libprelude/trunk/src/idmef-tree-wrap.c
   libprelude/trunk/src/idmef-wrappers-gen/GenerateIDMEFTreeWrapC.pm
Log:
Fix float comparison issue reported by Steve Grubb <[email protected]>.

The fix introduce a new float_compare() function which subtract the
integer representations of two floats to check how close they are.
If the difference is zero, they are identical. If the difference is
one, they are adjacent floats. In general, if the difference is n
then there are n-1 floats between them.

Modified: libprelude/trunk/src/idmef-tree-wrap.c
===================================================================
--- libprelude/trunk/src/idmef-tree-wrap.c	2009-04-08 13:09:21 UTC (rev 11126)
+++ libprelude/trunk/src/idmef-tree-wrap.c	2009-04-08 13:09:27 UTC (rev 11127)
@@ -86,7 +86,43 @@
 
 #define idmef_data_copy idmef_data_copy_dup
 
+#ifndef ABS
+# define ABS(x) (((x) < 0) ? -(x) : (x))
+#endif
 
+
+/*
+ * If we subtract the integer representations of two floats then that
+ * will tell us how close they are. If the difference is zero, they are
+ * identical. If the difference is one, they are adjacent floats.
+ * In general, if the difference is n then there are n-1 floats between
+ * them.
+ *
+ * http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
+ */
+static int float_compare(float a, float b)
+{
+        union {
+                float fval;
+                int32_t ival;
+        } au, bu;
+
+        au.fval = a;
+        bu.fval = b;
+
+        /* Make aInt lexicographically ordered as a twos-complement int */
+        if ( au.ival < 0 )
+                au.ival = 0x80000000 - au.ival;
+
+        /* Make bInt lexicographically ordered as a twos-complement int */
+        if ( bu.ival < 0 )
+                bu.ival = 0x80000000 - bu.ival;
+
+        return (ABS(au.ival - bu.ival) <= 0) ? 0 : -1;
+}
+
+
+
 static int prelude_string_copy(const prelude_string_t *src, prelude_string_t *dst)
 {
         prelude_return_val_if_fail(src, prelude_error(PRELUDE_ERROR_ASSERTION));
@@ -17607,8 +17643,7 @@
         if ( obj1->rating != obj2->rating )
                 return -1;
 
-        if ( obj1->confidence != obj2->confidence )
-                return -1;
+        ret = float_compare(obj1->confidence, obj2->confidence);
 
         return ret;
 }

Modified: libprelude/trunk/src/idmef-wrappers-gen/GenerateIDMEFTreeWrapC.pm
===================================================================
--- libprelude/trunk/src/idmef-wrappers-gen/GenerateIDMEFTreeWrapC.pm	2009-04-08 13:09:21 UTC (rev 11126)
+++ libprelude/trunk/src/idmef-wrappers-gen/GenerateIDMEFTreeWrapC.pm	2009-04-08 13:09:27 UTC (rev 11127)
@@ -118,7 +118,43 @@
 
 #define idmef_data_copy idmef_data_copy_dup
 
+#ifndef ABS
+# define ABS(x) (((x) < 0) ? -(x) : (x))
+#endif
 
+
+/*
+ * If we subtract the integer representations of two floats then that
+ * will tell us how close they are. If the difference is zero, they are
+ * identical. If the difference is one, they are adjacent floats.
+ * In general, if the difference is n then there are n-1 floats between
+ * them.
+ *
+ * http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
+ */
+static int float_compare(float a, float b)
+{
+        union {
+                float fval;
+                int32_t ival;
+        } au, bu;
+
+        au.fval = a;
+        bu.fval = b;
+
+        /* Make aInt lexicographically ordered as a twos-complement int */
+        if ( au.ival < 0 )
+                au.ival = 0x80000000 - au.ival;
+
+        /* Make bInt lexicographically ordered as a twos-complement int */
+        if ( bu.ival < 0 )
+                bu.ival = 0x80000000 - bu.ival;
+
+        return (ABS(au.ival - bu.ival) <= 0) ? 0 : -1;
+}
+
+
+
 static int prelude_string_copy(const prelude_string_t *src, prelude_string_t *dst)
 {
         prelude_return_val_if_fail(src, prelude_error(PRELUDE_ERROR_ASSERTION));
@@ -969,10 +1005,16 @@
                 return -1;
 ");
             } else {
-                $self->output("
+		if ( $field->{typename} eq "float" ) {
+			$self->output("
+        ret = float_compare(obj1->$field->{name}, obj2->$field->{name});
+");
+		} else {
+                	$self->output("
         if ( obj1->$field->{name} != obj2->$field->{name} )
                 return -1;
 ");
+		}
             }
         }
     }

_______________________________________________
Prelude-cvslog site list
[email protected]
http://lists.prelude-ids.org/mailman/listinfo/prelude-cvslog
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.