serde-json-core icon indicating copy to clipboard operation
serde-json-core copied to clipboard

panic when formatting `de::Error` (with feature `custom-error-messages`)

Open t-moe opened this issue 1 year ago • 0 comments
trafficstars

I'm getting a panic in impl de::Error for Error when the feature custom-error-messages is enabled

https://github.com/rust-embedded-community/serde-json-core/blob/ca6745785c6d315d532170a33bff826377c693c9/src/de/mod.rs#L765-L784

The panic occurs on the line: write!(string, "{:.64}", msg).unwrap() when it tries to format an error message like

unknown variant a1, expected one of b2, b3, b4, b5, b6, b7

The panic occurs because serde uses format_args!

https://github.com/serde-rs/serde/blob/31000e1874ff01362f91e7b53794e402fab4fc78/serde/src/de/mod.rs#L255-L259

  Error::custom(format_args!(
                "unknown variant `{}`, expected {}",
                variant,
                OneOf { names: expected }
            ))

and format_args! cannot be be truncated using specified precision.

See the following simplified reproducer, generates the same panic:

let x = serde_json_core::de::Error::custom(format_args!(
          "unknown variant `{}`, expected {}",
          "a",
          "one of bbbbbbbbbbbbbbbbbbbbbbbbb,ccccccccccccccccccc,ddddddddddddddddddddddddd,eeeeeeeeee"
  ));
  error!("{:?}", x);

t-moe avatar Sep 24 '24 12:09 t-moe