Skip to content

Passing %: to strtime::format panics instead of Erring #410

@cakebaker

Description

@cakebaker

An issue found by our fuzzer: if the incorrect format string %: is passed to strtime::format(), the function panics instead of returning an Err.

A simple example to reproduce the issue:

use jiff::{civil::date, fmt::strtime};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let zdt = date(2024, 7, 15)
        .at(16, 24, 59, 0)
        .in_tz("America/New_York")?;
    let _ = strtime::format("%:", &zdt)?;

    Ok(())
}

And the panic:

$ RUST_BACKTRACE=1 cargo run -q

thread 'main' panicked at /home/dho/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.15/src/fmt/strtime/format.rs:170:9:
index out of bounds: the len is 0 but the index is 0
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/29483883eed69d5fb4db01964cdf2af4d86e9cb2/library/std/src/panicking.rs:697:5
   1: core::panicking::panic_fmt
             at /rustc/29483883eed69d5fb4db01964cdf2af4d86e9cb2/library/core/src/panicking.rs:75:14
   2: core::panicking::panic_bounds_check
             at /rustc/29483883eed69d5fb4db01964cdf2af4d86e9cb2/library/core/src/panicking.rs:280:5
   3: jiff::fmt::strtime::format::Formatter<W,L>::f
             at /home/dho/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.15/src/fmt/strtime/format.rs:170:9
   4: jiff::fmt::strtime::format::Formatter<W,L>::format_one
             at /home/dho/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.15/src/fmt/strtime/format.rs:66:20
   5: jiff::fmt::strtime::format::Formatter<W,L>::format
             at /home/dho/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.15/src/fmt/strtime/format.rs:48:36
   6: jiff::fmt::strtime::BrokenDownTime::format_with_config
             at /home/dho/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.15/src/fmt/strtime/mod.rs:1153:19
   7: jiff::fmt::strtime::BrokenDownTime::format
             at /home/dho/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.15/src/fmt/strtime/mod.rs:1100:14
   8: jiff::fmt::strtime::format
             at /home/dho/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/jiff-0.2.15/src/fmt/strtime/mod.rs:436:22
   9: hello_world::main
             at ./src/main.rs:7:13
  10: core::ops::function::FnOnce::call_once
             at /home/dho/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions