Uploaded image for project: 'Byteman'
  1. Byteman
  2. BYTEMAN-116

ComparisonExpression fails to handle null values correctly

XMLWordPrintable

      In Byteman rules the comparison operations <, <=, ==, >=, > and != are valid on all object types which implement interface Comparable. They are implemented by calling method compareTo.
      At present, the code for "x op y" generates a null pointer exception when x is null because it always executes" x.compareTo". It would be better if Byteman detected null values and handled them differently. So, if either value is null:

      • for an ordering comparison the test should always return false i.e. null is removed from the sort, rather like NaN for floats/doubles.
      • for an == comparison the test should return true if both are null and false if only one is null
      • for a != comparison the test should return false if both are null and true if only one is null

      Also, for == and != comparisons where neither operand is null making a call to equals would be better than a call to compareTo as the generated bytecode is simpler. Amongst other things this has the desirable and desired consequence that e.g. String comparisons via == or != proceed via equals. This masks the ability to test for object identity directly but note that this is still available by assigning (one or both of) the operands to local variables of type Object and then comparing. e.g. in the following rule

      RULE foo
      CLASS bar
      METHOD baz
      BIND str : String = ...
      obj : Object = str + "1"
      IF TRUE
      DO traceln("str == obj ==> " + (str == obj))
      ENDRULE

      the comparison takes a String and an Object operand, which means the common base type for both is Object.Since Object does not implement Comparable the comparison proceeds via == rather than equals.

            rhn-engineering-adinn Andrew Dinn
            rhn-engineering-adinn Andrew Dinn
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: