Skip to content

uucore: avoid huge divisors for tiny hex durations - #15482

Merged
sylvestre merged 1 commit into
uutils:mainfrom
limadog9:fix/duration-tiny-hex-12226
Oct 19, 2026
Merged

sylvestre merged 1 commit into
uutils:mainfrom
limadog9:fix/duration-tiny-hex-12226

Conversation

@limadog9

Copy link
Copy Markdown
Contributor

Tiny hexadecimal durations with many fractional zeros spent seconds constructing a huge denominator. Detect values below one nanosecond before that division, accounting for binary exponents and unit suffixes. Add parser and sleep regressions.

Validation: 55 sleep integration tests and 84 parser tests pass.

Closes #12226

construct_extended_big_decimal(digits, negative, base, scale, exponent.unwrap_or_default());
let exponent = exponent.unwrap_or_default();

// A duration smaller than one nanosecond rounds up to one nanosecond.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could you please make this comment shorter? two lines should be enough

let ebd_result = if target == ParseTarget::Duration
&& base == Base::Hexadecimal
&& !digits.is_zero()
&& BigInt::from(digits.bits()) + &exponent - BigInt::from(scale) * 4 <= BigInt::from(-30)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please put -30 in a named const with a short comment (2^-30 s < 1ns)

&& !digits.is_zero()
&& BigInt::from(digits.bits()) + &exponent - BigInt::from(scale) * 4 <= BigInt::from(-30)
{
if negative {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

returning NotNumeric for a valid number feels like a hack
could we return make_error(false, negative) and reject a negative underflow in parse_time instead?

@sylvestre

Copy link
Copy Markdown
Contributor

@limadog9 our time isn't infinite, please pause the contributions a bit, it is impossible to handle such a large volume

@limadog9

Copy link
Copy Markdown
Contributor Author

Will do, thanks

@limadog9
limadog9 force-pushed the fix/duration-tiny-hex-12226 branch from 0705a5b to 9792f38 Compare October 18, 2026 22:37
//! Utilities for parsing numbers in various formats

// 2^-30 seconds < 1 ns.
const SUBNANOSECOND_BINARY_EXPONENT: i32 = -30;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could you please move it after the use block? constants usually live there

Comment thread tests/by-util/test_sleep.rs Outdated
}

#[test]
#[cfg(target_os = "linux")]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why only linux?

Tiny hexadecimal durations with many fractional zeros spent seconds
constructing a huge denominator. Detect values below one nanosecond
before that division, accounting for binary exponents and unit suffixes.
Add parser and sleep regressions.

Validation: 55 sleep integration tests and 84 parser tests pass.

Closes uutils#12226
@limadog9
limadog9 force-pushed the fix/duration-tiny-hex-12226 branch from 9792f38 to 384b157 Compare October 19, 2026 10:06
@limadog9

Copy link
Copy Markdown
Contributor Author

Moved the constant and enabled the regression test across platforms.

@sylvestre
sylvestre merged commit 0bf0a66 into uutils:main Oct 19, 2026
190 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

uucore/parse_time: parsing a very small number takes a lot of time

2 participants