question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Possible Int overflow in AggregateEventTag

See original GitHub issue

Lagom Version

1.4.5, 1.5.0, Master

API (Scala / Java / Neither / Both)

Both Scala and Java API.

Expected Behavior

AggregateEventTag.selectShard should always return positive value.

Actual Behavior

AggregateEventTag.selectShard can return a negative value, if entityId.hashCode == Int.MinValue. Then, if numShards is e.g. 6, the the result will be -2.

AggregateEventTag.scala:78

def selectShard(numShards: Int, entityId: String): Int = {
    Math.abs(entityId.hashCode) % numShards
}

Here Math.abs(entityId.hashCode) % numShards will reduce to Int.MinValue % numShards, if entityId.hashCode equals Int.MinValue.

As there is no guarantee that String.hashCode will not return Int.MinValue, it is possible, albeit very unlikely, that the tag shard tag name generation will return an invalid tag name.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
octonatocommented, Jun 25, 2019

I think you mean:

if (shard < 0) 0 else shard

Could also be: Math.abs(shard)

2reactions
ignasi35commented, Jun 25, 2019

I think the solution is:

def selectShard(numShards: Int, entityId: String): Int = {
    val shard = Math.abs(entityId.hashCode) % numShards
    if (shard == Int.MinValue) 0 else shard
}

Which hardcodes the mapping of Int.MinValue to 0 and maintains all other mappings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How does Java handle integer underflows and overflows and ...
If it overflows, it goes back to the minimum value and continues from there. If it underflows, it goes back to the maximum...
Read more >
Domain Modelling with Akka Persistence Typed
In Akka Typed, unlike Akka classic and Lagom Persistence, it's not possible to return an exception to the caller. All communication between the...
Read more >
Check for Integer Overflow - GeeksforGeeks
Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum...
Read more >
java.util.concurrent.ConcurrentHashMap Scala Example
Utils def apply( execId: String, host: String, port: Int, topologyInfo: ... AggregateEventTag import com.lightbend.lagom.scaladsl.persistence.
Read more >
Is Integer Overflow and Underflow possible in any scenerio in ...
Well, yes. If you use assembly or the unchecked keyword you can get an overflow/underflow. Check this simple code I created:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found