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

8273369: Computing micros between two instants unexpectedly overflows for some cases #5396

Closed
wants to merge 6 commits into from

Conversation

@naotoj
Copy link
Member

@naotoj naotoj commented Sep 7, 2021

Please review the fix to the issue. Avoiding overflow by not calling nanosUntil() directly, which will overflow beyond Long.MAX_VALUE difference in nano unit.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8273369: Computing micros between two instants unexpectedly overflows for some cases

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/5396/head:pull/5396
$ git checkout pull/5396

Update a local copy of the PR:
$ git checkout pull/5396
$ git pull https://git.openjdk.java.net/jdk pull/5396/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 5396

View PR using the GUI difftool:
$ git pr show -t 5396

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/5396.diff

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented Sep 7, 2021

👋 Welcome back naoto! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

Loading

@openjdk openjdk bot added the rfr label Sep 7, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Sep 7, 2021

@naotoj The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

Loading

@openjdk openjdk bot added the core-libs label Sep 7, 2021
@mlbridge
Copy link

@mlbridge mlbridge bot commented Sep 7, 2021

Loading

Copy link
Contributor

@LanceAndersen LanceAndersen left a comment

Looks OK to me

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Sep 7, 2021

@naotoj This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8273369: Computing micros between two instants unexpectedly overflows for some cases

Reviewed-by: lancea, rriggs, joehw

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 83 new commits pushed to the master branch:

  • 4d5e1ed: 8272375: Improve phrasing of synthesized descriptions in JavaFX docs
  • 267c61a: 8273361: InfoOptsTest is failing in tier1
  • f7e9f56: 8273359: CI: ciInstanceKlass::get_canonical_holder() doesn't respect instance size
  • d7efd0e: 8273450: Fix the copyright header of SVML files
  • e5f298a: 8273329: Remove redundant null check from String.getBytes(String charsetName)
  • cb112af: 8269373: some tests in jdk/tools/launcher/ fails on localized Windows platform
  • 7e662e7: 8272413: Incorrect num of element count calculation for vector cast
  • f2f8136: 8265443: IGV: disambiguate groups by emiting additional properties
  • 59c9f75: 8273375: Remove redundant 'new String' calls after concatenation in java.desktop
  • faa942c: 8273000: Remove WeakReference-based class initialisation barrier implementation
  • ... and 73 more: https://git.openjdk.java.net/jdk/compare/18a731a3e4ec85f0f3e8d6ff619b539c0daaf320...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

Loading

@openjdk openjdk bot added the ready label Sep 7, 2021
private long microsUntil(Instant end) {
long secsDiff = Math.subtractExact(end.seconds, seconds);
long totalMicros = Math.multiplyExact(secsDiff, NANOS_PER_SECOND / 1000);
return Math.addExact(totalMicros, (end.nanos - nanos) / 1000);
Copy link
Contributor

@RogerRiggs RogerRiggs Sep 7, 2021

Choose a reason for hiding this comment

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

Can you define NANOS_PER_MICRO, the others conversions use predefined constants.

Loading

Copy link
Member Author

@naotoj naotoj Sep 7, 2021

Choose a reason for hiding this comment

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

Defined it as a private field in Instant.

Loading

LanceAndersen
Copy link
Contributor

LanceAndersen commented on bbd6abc Sep 7, 2021

Choose a reason for hiding this comment

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

I thought about suggesting that, but given it was only used by your change decided otherwise. Glad that you decided to though :-)

Loading

@@ -96,4 +98,11 @@ public void test_epochMillis(String name, long millis) {
assertEquals(millis, m, name);
}

@Test
public void test_microsUntil() {
Copy link
Member

@JoeWang-Java JoeWang-Java Sep 8, 2021

Choose a reason for hiding this comment

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

A comment about the test might be helpful.

Loading

Copy link
Member Author

@naotoj naotoj Sep 8, 2021

Choose a reason for hiding this comment

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

Indeed. Added some comments to the test.

Loading

Copy link
Contributor

@jodastephen jodastephen left a comment

This change looks fine, but I think you also need a millisUntil private method to fix the identical overflow problem with millis (which might as well be fixed now).

Loading

@naotoj
Copy link
Member Author

@naotoj naotoj commented Sep 8, 2021

This change looks fine, but I think you also need a millisUntil private method to fix the identical overflow problem with millis (which might as well be fixed now).

until() for millis simply subtracts its toEpochMilli() from the end Instant's, so I am not sure that would cause the same false ArithmeticException. Can you please elaborate more?

Loading

@jodastephen
Copy link
Contributor

@jodastephen jodastephen commented Sep 8, 2021

toEpochMilli() overflows at large/small values. Thus any attempt to calculate the difference between two very large instants would fail.

Loading

@naotoj
Copy link
Member Author

@naotoj naotoj commented Sep 8, 2021

toEpochMilli() overflows at large/small values. Thus any attempt to calculate the difference between two very large instants would fail.

Thanks. Fixed.

Loading

@naotoj
Copy link
Member Author

@naotoj naotoj commented Sep 10, 2021

/integrate

Loading

@openjdk
Copy link

@openjdk openjdk bot commented Sep 10, 2021

Going to push as commit 81d2ace.
Since your change was applied there have been 122 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

Loading

@openjdk openjdk bot closed this Sep 10, 2021
@openjdk openjdk bot added integrated and removed ready rfr labels Sep 10, 2021
@openjdk
Copy link

@openjdk openjdk bot commented Sep 10, 2021

@naotoj Pushed as commit 81d2ace.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
5 participants