Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an exception if size-in-bytes values are out of Long range #173

Merged
merged 2 commits into from
Dec 27, 2014

Conversation

havocp
Copy link
Collaborator

@havocp havocp commented Jun 23, 2014

Fixes #170

Discussion on #160 suggests that we may want to only print to stderr or something (and clamp the value to Long.MAX_VALUE) rather than throwing an exception.... not sure how I feel about that.

private static boolean isValidLong(BigInteger v) {
BigInteger max = BigInteger.valueOf(Long.MAX_VALUE);
BigInteger min = BigInteger.valueOf(Long.MIN_VALUE);
return max.compareTo(v) >= 0 && min.compareTo(v) <= 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheaper:

v.bitLength <= 63

 - use bitLength to see if we overflow Long
 - use BigInteger and BigDecimal to parse, instead of parseLong/parseDouble
@havocp
Copy link
Collaborator Author

havocp commented Jun 25, 2014

Good suggestions, fixed.

@havocp
Copy link
Collaborator Author

havocp commented Dec 27, 2014

Going to merge this but it will only be in a 1.3.x release not a 1.2.x so I think throwing is ok.

havocp added a commit that referenced this pull request Dec 27, 2014
Throw an exception if size-in-bytes values are out of Long range
@havocp havocp merged commit 197be59 into master Dec 27, 2014
@havocp havocp deleted the wip/havocp-out-of-range-bytes branch December 27, 2014 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

getBytes abbreviations exceed Long storage but no Exception is thrown
2 participants