Skip to content

pantry expiring panics on a large --days value #428

Closed

Description

@dubadub

cook pantry expiring --days N panics for large N instead of reporting an error or saturating.

Reproduction

cook pantry expiring -d 4294967295

Cause

src/pantry.rs:

let today = Local::now().date_naive();
let threshold_date = today + chrono::Duration::days(args.days as i64);

--days is a u32, and chrono's Add<Duration> for NaiveDate panics on out-of-range results rather than returning an error. Any value large enough to push the date past NaiveDate's representable range aborts the process.

Impact

Low severity — it needs an absurd --days value, so it is unlikely to be hit accidentally. But a panic is never the right response to a command-line argument, and the message a user sees is a Rust backtrace rather than anything actionable.

Suggested direction

Use checked_add_signed and either saturate at NaiveDate::MAX (every item is "expiring" within a window that large, which is arguably the right answer) or reject the value with a clear message.

Noticed while extracting the pantry queries into a library crate; that branch saturates, since a library must not panic on a caller-supplied number — a panic crossing a NAPI boundary into JavaScript is considerably worse than one in a CLI.

Activity

  1. added
    help wanted
    on Aug 13, 2026
  2. dubadub commented on Aug 14, 2026

    @dubadub
    MemberAuthor

    Fixed. Core saturates at NaiveDate::MAX (from #434); #444 fixed the copy in the server handler (fd36745), which was more exposed than the CLI path since days comes straight off the query string — an out-of-range value took down the request handler rather than a one-shot process.

    $ cook pantry expiring -d 4294967295
    Items Expiring Within 4294967295 Days:
      • rice - 2027-01-01 (expires in 141 days) [larder]
    exit=0
    

    stats.rs uses a literal 7-day window, which cannot run off the end of the calendar, so it is left as it is.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions