]> www.ginac.de Git - ginac.git/commitdiff
Fix relational::compare_same_type.
authorOleg Finkelshteyn <olegfink@gmail.com>
Tue, 21 Dec 2021 09:02:29 +0000 (10:02 +0100)
committerRichard Kreckel <kreckel@ginac.de>
Tue, 21 Dec 2021 09:02:29 +0000 (10:02 +0100)
A hash collision was needed to elicit this bug because basic::compare
returns early without calling compare_same_type if hashes differ.

Reported by Feng Feng <f.feng@outlook.com>.

check/exam_relational.cpp
ginac/relational.cpp

index 3b652b05ac09c8e34c011d8c54c484c02a840b3d..aeff144bfbf5046158bf0a00be25bff107a04ad1 100644 (file)
@@ -116,6 +116,21 @@ static unsigned exam_relational_arith()
        return result;
 }
 
+// Comparisons should maintain ordering invariants
+static unsigned exam_relational_order()
+{
+       unsigned result = 0;
+       numeric i = 1ll<<32, j = i+1;
+       symbol a;
+       relational x = i==a, y = j==a;
+       if (x.compare(y) != -y.compare(x)) {
+               clog << "comparison should be antisymmetric." << endl;
+               result += 1;
+       }
+
+       return result;
+}
+
 unsigned exam_relational()
 {
        unsigned result = 0;
@@ -125,6 +140,7 @@ unsigned exam_relational()
        result += exam_relational_elementary(); cout << '.' << flush;
        result += exam_relational_possymbol(); cout << '.' << flush;
        result += exam_relational_arith(); cout << '.' << flush;
+       result += exam_relational_order(); cout << '.' << flush;
 
        return result;
 }
index 599a26352de202f949eddd9e8aac1fdee0cc6289..dbc541a31c88e3377ff253161bcfdbd1effa9b05 100644 (file)
@@ -223,8 +223,8 @@ int relational::compare_same_type(const basic & other) const
                                return (o < oth.o) ? -1 : 1;
                        break;
        }
-       const int lcmpval = lh.compare(oth.rh);
-       return (lcmpval!=0) ? lcmpval : rh.compare(oth.lh);
+       const int lcmpval = lh.compare(oth.lh);
+       return (lcmpval!=0) ? lcmpval : rh.compare(oth.rh);
 }
 
 bool relational::match_same_type(const basic & other) const