xml.ui: Bug 495415: Safely absolute a hashcode
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Fri, 3 Jun 2016 14:55:48 +0000 (10:55 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Fri, 3 Jun 2016 20:56:37 +0000 (16:56 -0400)
It is possible for a call to hashCode to return
Integer.MIN_VALUE. Take the absolute value of such
a hashcode and you'll still have a negative number.
Since your code is likely to assume that it's a
positive value instead, your results will be unreliable.

Similarly, Integer.MIN_VALUE could be returned from
Random.nextInt() or any object's compareTo method,
and Long.MIN_VALUE could be returned from Random.nextLong().
Calling Math.abs on values returned from these methods
is similarly ill-advised.

Change-Id: I44396a35d3efd046b8a082ebb57c070387d9f90d
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/74533
Reviewed-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Tested-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Reviewed-by: Hudson CI
tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/views/timegraph/XmlPresentationProvider.java

index d83a8b300a47242fdc09cfb2b30737ac5a64c99f..f848f1af17f1f33c98f33ace57775e7cb83fa220 100644 (file)
@@ -212,8 +212,8 @@ public class XmlPresentationProvider extends TimeGraphPresentationProvider {
      * display identically states with the same name.
      */
     private static RGB calcColor(String name) {
-        int hash = name.hashCode();
-        long base = COLOR_SEED[Math.abs(hash) % COLOR_SEED.length];
+        long hash = name.hashCode(); // hashcodes can be Integer.MIN_VALUE.
+        long base = COLOR_SEED[(int) (Math.abs(hash) % COLOR_SEED.length)];
         int x = (int) ((hash & COLOR_MASK) ^ base);
         final int r = (x >> 16) & 0xff;
         final int g = (x >> 8) & 0xff;
This page took 0.024886 seconds and 5 git commands to generate.