From 8b0730f435890afe5ad6a61a1880d963ce8d7705 Mon Sep 17 00:00:00 2001 From: Matthew Khouzam Date: Fri, 3 Jun 2016 10:55:48 -0400 Subject: [PATCH] xml.ui: Bug 495415: Safely absolute a hashcode 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 Reviewed-on: https://git.eclipse.org/r/74533 Reviewed-by: Genevieve Bastien Tested-by: Genevieve Bastien Reviewed-by: Hudson CI --- .../xml/ui/views/timegraph/XmlPresentationProvider.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/views/timegraph/XmlPresentationProvider.java b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/views/timegraph/XmlPresentationProvider.java index d83a8b300a..f848f1af17 100644 --- a/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/views/timegraph/XmlPresentationProvider.java +++ b/tmf/org.eclipse.tracecompass.tmf.analysis.xml.ui/src/org/eclipse/tracecompass/internal/tmf/analysis/xml/ui/views/timegraph/XmlPresentationProvider.java @@ -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; -- 2.34.1