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
* 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;