bretzay commented on PR #908:
URL:
https://github.com/apache/arrow-rs-object-store/pull/908#issuecomment-6962291624
Thanks for taking on RUSTSEC-2026-0542. Getting humantime out of the tree is
overdue.
I ran this PR's `parse_duration` against `humantime` 2.4.0 on 1,516 inputs:
every humantime unit spelling, fractions on each unit, 400 random multi-term
sequences, whitespace variants and error cases. On 246 of them humantime
returns a value and this PR returns an error. A config that loads today with
`timeout = "0.5d"` would fail after the upgrade. The refusals fall into a few
groups.
| Input | humantime 2.4.0 | this PR |
|---|---|---|
| `1M` (humantime's month, case-sensitive) | 2630016s | error |
| `1y 1M` | 34187616s | error |
| `0.5d`, `1.0d` | 43200s, 86400s | error |
| `1.5months` | 3945024s | error |
| `2 weeks 1.5d` | 1339200s | error |
| `1.5h 30m` (fraction before another unit) | 7200s | error |
| `1s 1h`, `30m 1h` (units out of order) | 3601s, 5400s | error |
| `1h 1h` (repeated unit) | 7200s | error |
| `100000000y` (beyond jiff's span limits) | 3155760000000000s | error |
Fractional hours and smaller still work (`1.5h`, `1.5m`). The new test says
its expected values "match what `humantime` 2.x returned", which is true for
the cases listed there, but not for the inputs above. In the other direction
the PR accepts `1mo`, `1h, 30m` and `+5s`, which does no harm, and 40 days now
formats as `960h` instead of `1month 9days 13h 26m 24s`, which still parses
back.
I see two ways to settle it.
**Keep humantime's grammar.** The patch below replaces the jiff parser with
a small hand-written one that uses humantime's unit table and exact integer
arithmetic. jiff stays for formatting. On the same 1,516 inputs it returns
humantime's value, to the nanosecond, wherever humantime returns one, with one
exception. Whitespace inside a number is refused (`1 0s` was 10s in humantime,
`1 .5s` was 1.5s), and I doubt anyone relies on that. It is also more lenient
in two places. It accepts fractions humantime refused because they did not land
on a whole second (`0.1M` gives 263001.6s, `0.0001h` gives 360ms, as this PR
already does), and a padded `"0 "`, as this PR already does. All 2,006
formatted durations I tried parse back to themselves. The cost is size, about
40 lines in place of 24, which is more than I'd like for a dependency swap. The
patch also adds the regressed inputs to your test.
<details>
<summary>Patch on efa9e59 (applies with <code>git apply</code>)</summary>
```diff
diff --git a/src/config.rs b/src/config.rs
index 7fe180f..31ef346 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -19,8 +19,7 @@ use std::str::FromStr;
use std::time::Duration;
use http::header::HeaderValue;
-use jiff::fmt::friendly::{SpanParser, SpanPrinter};
-use jiff::{SignedDuration, Span};
+use jiff::fmt::friendly::SpanPrinter;
use crate::{Error, Result};
@@ -95,34 +94,52 @@ impl Parse for Duration {
}
}
-/// Parses a duration in jiff's "friendly" format, e.g. `60s`, `1h 30m` or
`2 days`
+/// Parses a duration such as `60s`, `1h 30m`, `1.5d` or `1y 1M`
///
-/// Calendar units are converted with the fixed lengths previously used by
`humantime`:
-/// a day is 24 hours, a week 7 days, a month 30.44 days and a year 365.25
days.
+/// Accepts the units and lengths of `humantime`, which this replaced. A
month
+/// (`M`) is 30.44 days, a year 365.25 days, and every unit takes a
fraction.
fn parse_duration(v: &str) -> Option<Duration> {
- const PARSER: SpanParser = SpanParser::new();
- const DAY: u64 = 86_400;
+ const NS: u128 = 1_000_000_000;
+ fn split(s: &str, f: fn(char) -> bool) -> (&str, &str) {
+ s.split_at(s.find(|c| !f(c)).unwrap_or(s.len()))
+ }
- let v = v.trim();
- if v == "0" {
+ let mut rest = v.trim();
+ if rest == "0" {
return Some(Duration::ZERO);
}
- if let Ok(d) = PARSER.parse_unsigned_duration(v) {
- return Some(d);
- }
-
- // Spans with calendar units (days and above) cannot be parsed into a
duration directly
- let span: Span = PARSER.parse_span(v).ok()?;
- if span.is_negative() {
- return None;
+ let mut total = 0u128;
+ loop {
+ let (int, tail) = split(rest, |c| c.is_ascii_digit());
+ let (frac, tail) = match tail.strip_prefix('.') {
+ Some(tail) => split(tail, |c| c.is_ascii_digit()),
+ None => ("0", tail),
+ };
+ let (unit, tail) = split(tail.trim_start(), char::is_alphabetic);
+ let unit = match unit {
+ "nanos" | "nsec" | "ns" => 1,
+ "usec" | "us" | "µs" => 1_000,
+ "millis" | "msec" | "ms" => 1_000_000,
+ "seconds" | "second" | "secs" | "sec" | "s" => NS,
+ "minutes" | "minute" | "min" | "mins" | "m" => 60 * NS,
+ "hours" | "hour" | "hr" | "hrs" | "h" => 3_600 * NS,
+ "days" | "day" | "d" => 86_400 * NS,
+ "weeks" | "week" | "wk" | "wks" | "w" => 604_800 * NS,
+ "months" | "month" | "M" => 2_630_016 * NS,
+ "years" | "year" | "yr" | "yrs" | "y" => 31_557_600 * NS,
+ _ => return None,
+ };
+ // value = int.frac * unit, exact to the nanosecond or refused
+ let den = 10u128.checked_pow(frac.len() as u32)?;
+ let num = u128::from(int.parse::<u64>().ok()?).checked_mul(den)?;
+ let value = num.checked_add(frac.parse().ok()?)?.checked_mul(unit)?;
+ total = total.checked_add(Some(value).filter(|v| v % den == 0)? /
den)?;
+ rest = tail.trim_start();
+ if rest.is_empty() {
+ let secs = u64::try_from(total / NS).ok()?;
+ return Some(Duration::new(secs, (total % NS) as u32));
+ }
}
- let calendar_secs = (span.get_years() as u64 * 31_557_600)
- + (span.get_months() as u64 * 2_630_016)
- + (span.get_weeks() as u64 * 7 * DAY)
- + (span.get_days() as u64 * DAY);
- let time = span.years(0).months(0).weeks(0).days(0);
- let time =
Duration::try_from(SignedDuration::try_from(time).ok()?).ok()?;
- time.checked_add(Duration::from_secs(calendar_secs))
}
impl Parse for usize {
@@ -201,12 +218,21 @@ mod tests {
Duration::new(38_995_999, 8_000_000),
),
("18446744073709551615s", Duration::from_secs(u64::MAX)),
+ ("1M", Duration::from_secs(2_630_016)),
+ ("1y 1M", Duration::from_secs(34_187_616)),
+ ("0.5d", Duration::from_secs(43_200)),
+ ("1.5months", Duration::from_secs(3_945_024)),
+ ("2 weeks 1.5d", Duration::from_secs(1_339_200)),
+ ("1.5h 30m", Duration::from_secs(7_200)),
+ ("1s 1h", Duration::from_secs(3_601)),
];
for (input, expected) in cases {
assert_eq!(Duration::parse(input).unwrap(), expected,
"{input}");
}
- for input in ["", "5", "s", "-5s", "-1d", "5 S", "PT5S", "1d -1h"] {
+ for input in [
+ "", "5", "s", "-5s", "-1d", "5 S", "PT5S", "1d -1h", "1.s",
"1.5ns",
+ ] {
assert!(Duration::parse(input).is_err(), "{input}");
}
}
```
</details>
**Or document the narrowing.** If you'd rather keep jiff's grammar, list the
forms that stop parsing under "Are there any user-facing changes?" (`M`,
fractional days, weeks, months and years, units out of order or repeated, a
fraction followed by another unit), and reword the test comment so it does not
claim parity with humantime. Since the changelog is generated from labels, a
maintainer may also want `api-change` on the PR so the change shows up there.
With the patch applied on efa9e59, `cargo test --lib --features cloud-base
config::` passes, `cargo fmt --all -- --check` is clean, and `cargo clippy --
-D warnings` is clean with `--all-targets --features aws,azure,gcp,http`, with
`--features aws` and with `--no-default-features --features
aws-base,azure-base,gcp-base,http-base`. `cargo clippy --features cloud-base --
-D warnings` on its own fails on dead code elsewhere in the crate, before and
after the patch. CI does not run that combination.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]