Fix: bt2: erroneous integer comparison of Field and Value
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Tue, 25 Jun 2019 21:44:32 +0000 (17:44 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Thu, 27 Jun 2019 20:54:53 +0000 (16:54 -0400)
commit7bb4180f2758d78af3f4d539f6b3e4e1fa60335f
tree81d873601b1b35e13eaa54e8e7de29cf990906f5
parent2b4ea20edfcf8ff93be7fbb98dfde5b4ba574b8f
Fix: bt2: erroneous integer comparison of Field and Value

Issue
=====
The `{Unsigned, Signed}IntegerValue` class inherit its __eq__() method
from the `_NumericValue` class. This method converts the `other` object
to a `complex` number to make the comparison as generic as possible. The
issue is that the large complex number comparison is unreliable as it's
using float comparison[1]. The timestamps we are dealing with will often
be going beyond the limit of exact comparison possible with the IEEE 754
double-precision[2].

I encountered this issue while comparing a
`bt2.value.SignedIntegerValue` to a large Python Integer representing a
timestamp in nanosecond.

Here is a showcase of the issue reproduced without the `bt2` bindings:
  In [1]: 42 == complex(42)
  Out[1]: True

  In [2]: 1561489497327275497 == complex(1561489497327275497)
  Out[2]: False

The same issue is reproducible with the `_{Unsigned,
Signed}IntegerField`.

Solution
========
Implement the __eq__() and __lt__() methods in the `_IntegralValue`
class and but still use `_NumericValue`'s methods if the `other` object
is not an instance of `numbers.Integral`.

Implement the __eq__() and __lt__() methods in the `_IntegralField`
class and but still use `_NumericField`'s methods if the `other` object
is not an instance of `numbers.Integral`.

Drawback
========
None.

Note
====
I added test cases to cover large integer comparison for both Integer Value and
integer Field classes.

[1]: https://stackoverflow.com/questions/5595425/what-is-the-best-way-to-compare-floats-for-almost-equality-in-python
[2]: https://en.wikipedia.org/wiki/Double-precision_floating-point_format

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I8448bd42704ad1a2d215bece8534c839b9468f25
Reviewed-on: https://review.lttng.org/c/babeltrace/+/1538
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
src/bindings/python/bt2/bt2/field.py
src/bindings/python/bt2/bt2/value.py
tests/bindings/python/bt2/test_field.py
tests/bindings/python/bt2/test_value.py
This page took 0.025702 seconds and 4 git commands to generate.