Skip to content

Conversation

stepancheg
Copy link
Contributor

@stepancheg stepancheg commented Sep 14, 2025

Add a test for regression #146228, and turns out this test fails detects error when std is compiled with integer overflow checks.

Original regression was reverted in #146473.

First attempt to fix was in #146247; test and some code is copied from there, thanks @eval-exec

r? @RalfJung

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Sep 14, 2025
@stepancheg stepancheg changed the title Repro duration_since regression from issue 146228 Test for (reverted now) duration_since regression Sep 14, 2025
@rust-log-analyzer

This comment has been minimized.

@stepancheg
Copy link
Contributor Author

Seems like the bug was there before constify of SystemTime, but did not manifest in debug builds. I'll try to fix.

@stepancheg
Copy link
Contributor Author

If anyone is around, not do I compile std with debug assertions including integer overflow? I'm running

./x test library/std --no-doc

and such assertions are not triggered.

@workingjubilee
Copy link
Member

put this in your bootstrap.toml

rust.debug-assertions-std = true

from

rust/bootstrap.example.toml

Lines 603 to 604 in 52618eb

# Defaults to rust.debug-assertions value
#rust.debug-assertions-std = rust.debug-assertions (boolean)

@stepancheg
Copy link
Contributor Author

stepancheg commented Sep 14, 2025

@workingjubilee this seems to enable debug_assert!, but does not trigger integer overflow checks.

@stepancheg
Copy link
Contributor Author

Found it.

rust.overflow-checks = true

@rustbot rustbot added O-hermit Operating System: Hermit O-unix Operating system: Unix-like labels Sep 14, 2025
@stepancheg stepancheg changed the title Test for (reverted now) duration_since regression Fix duration_since panic on unix when std is built with integer overflow checks Sep 14, 2025
@workingjubilee
Copy link
Member

Ah, yes, down a few lines then.

@RalfJung
Copy link
Member

r? libs
since this actually changes the implementation

@rustbot rustbot assigned tgross35 and unassigned RalfJung Sep 15, 2025
@rustbot

This comment has been minimized.

@stepancheg
Copy link
Contributor Author

Ping @tgross35.

} else {
(
(self.tv_sec - other.tv_sec - 1) as u64,
(self.tv_sec - 1).wrapping_sub(other.tv_sec) as u64,
Copy link
Member

Choose a reason for hiding this comment

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

I had to think for a few seconds before I realised why the - 1 couldn't panic (since other.tv_nsec is larger than self.tv_nsec, but other smaller than self, other.tv_sec must be smaller than self.tv_sec, meaning that there is a value smaller than self.tv_sec, making the subtraction work). Could you perhaps add a comment to explain it? Or use wrapping_sub for the decrement as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a few assertions, so that sequence of assertion will explain why it does not underflow.

Or use wrapping_sub for the decrement as well?

That would not be right, because if there is a bug here, instead of debug assertion, we may quietly get wrong result.

// is outside of the `if`-`else`, not duplicated in both branches
//
// Ideally this code could be rearranged such that it more
// directly expresses the lower-cost behavior we want from it.
Copy link
Contributor

@tgross35 tgross35 Sep 19, 2025

Choose a reason for hiding this comment

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

The comment above here needs to be updated, considering it specifically calls out self.tv_sec - 1 - other.tv_sec vs. self.tv_sec - other.tv_sec - 1. I don't think it's all relevant anymore, given https://rust.godbolt.org/z/1Ex913qr5

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the comment because:

  • you asked to update it
  • I don't understand it
  • this does not seem like performance critical code to do

@tgross35
Copy link
Contributor

Ping @tgross35.

Hey, this was open for only three days :) I don't think this is high priority?

@rustbot

This comment has been minimized.

@stepancheg
Copy link
Contributor Author

stepancheg commented Sep 19, 2025

@tgross35

I don't think this is high priority?

The bugfix is not high priority, but I wanted to add a couple PRs on top, and if each one takes weeks to review, I will lose context and/or switch to some other work (my primary work is far from opensource).

Sorry for the ping. Is there expected time to a comment from the reviewer for a PR in rust repo so next time I'll avoid pinging the maintainers too early?

@RalfJung
Copy link
Member

The comment that the bot posts for new contributors says

They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Copy link
Contributor

@tgross35 tgross35 left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this fix, two small requests then r=me.

The bugfix is not high priority, but I wanted to add a couple PRs on top, and if each one takes weeks to review, I will lose context and/or switch to some other work (my primary work is far from opensource).

Sorry for the ping. Is there expected time to a comment from the reviewer for a PR in rust repo so next time I'll avoid pinging the maintainers too early?

No worries, we just usually expect a couple of weeks. If you have a small change, it's fine to ask on Zulip if there's anyone willing to take a quick look at something.

I think stacking PRs is pretty common to keep future work unblocked.

View changes since this review

@rustbot
Copy link
Collaborator

rustbot commented Sep 24, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@stepancheg
Copy link
Contributor Author

Applied suggestions.

Added helper function sub_ge_to_unsigned with debug assertion and explanations how to subtract i64 to u64.

And slight offtopic here, that is the function I needed a few times: shouldn't it it added to core? Something like:

impl i64 {
  // if self >= other { Ok(self - other) } else { Err(other - self) }
  fn sub_to_unsigned(self, other: i64) -> Result<u64, u64> {
    if self >= other {
      Ok(self.wrapping_sub(other).cast_unsigned())
    } else {
      Err(other.wrapping_sub(self).cast_unsigned())
    }
  }

  fn strict_sub_to_unsigned(self: i64, other: i64) -> u64 {
    // panic if ...
  }
}

This function would be somewhat similar to SystemTime::duration_since.

@tgross35
Copy link
Contributor

Looks great, thanks!

@stepancheg
Copy link
Contributor Author

... last minute typo in comment fix.

@tgross35
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Sep 24, 2025

📌 Commit 92859e9 has been approved by tgross35

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 24, 2025
@tgross35
Copy link
Contributor

@bors rollup

bors added a commit that referenced this pull request Sep 24, 2025
Rollup of 7 pull requests

Successful merges:

 - #146556 (Fix duration_since panic on unix when std is built with integer overflow checks)
 - #146679 (Clarify Display for error should not include source)
 - #146753 (Improve the pretty print of UnstableFeature clause)
 - #146894 (Improve derive suggestion of const param)
 - #146950 (core: simplify `CStr::default()`)
 - #146958 (Fix infinite recursion in Path::eq with String)
 - #146971 (fix ICE in writeback due to bound regions)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Sep 25, 2025
Rollup of 7 pull requests

Successful merges:

 - #146556 (Fix duration_since panic on unix when std is built with integer overflow checks)
 - #146679 (Clarify Display for error should not include source)
 - #146753 (Improve the pretty print of UnstableFeature clause)
 - #146894 (Improve derive suggestion of const param)
 - #146950 (core: simplify `CStr::default()`)
 - #146958 (Fix infinite recursion in Path::eq with String)
 - #146971 (fix ICE in writeback due to bound regions)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8073b6f into rust-lang:master Sep 25, 2025
10 checks passed
@rustbot rustbot added this to the 1.92.0 milestone Sep 25, 2025
rust-timer added a commit that referenced this pull request Sep 25, 2025
Rollup merge of #146556 - stepancheg:repro-146228, r=tgross35

Fix duration_since panic on unix when std is built with integer overflow checks

Add a test for regression #146228, and turns out this test fails detects error when std is compiled with integer overflow checks.

Original regression was reverted in #146473.

First attempt to fix was in #146247; test and some code is copied from there, thanks `@eval-exec`

r? `@RalfJung`
github-actions bot pushed a commit to rust-lang/rust-analyzer that referenced this pull request Sep 29, 2025
Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#146556 (Fix duration_since panic on unix when std is built with integer overflow checks)
 - rust-lang/rust#146679 (Clarify Display for error should not include source)
 - rust-lang/rust#146753 (Improve the pretty print of UnstableFeature clause)
 - rust-lang/rust#146894 (Improve derive suggestion of const param)
 - rust-lang/rust#146950 (core: simplify `CStr::default()`)
 - rust-lang/rust#146958 (Fix infinite recursion in Path::eq with String)
 - rust-lang/rust#146971 (fix ICE in writeback due to bound regions)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-hermit Operating System: Hermit O-unix Operating system: Unix-like S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants