Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
t-moe opened this issue Sep 24, 2024 · 0 comments
Open

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

t-moe opened this issue Sep 24, 2024 · 0 comments

Comments

@t-moe
Copy link

t-moe commented Sep 24, 2024

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

impl de::Error for Error {
#[cfg_attr(not(feature = "custom-error-messages"), allow(unused_variables))]
fn custom<T>(msg: T) -> Self
where
T: fmt::Display,
{
#[cfg(not(feature = "custom-error-messages"))]
{
Error::CustomError
}
#[cfg(feature = "custom-error-messages")]
{
use core::fmt::Write;
let mut string = heapless::String::new();
write!(string, "{:.64}", msg).unwrap();
Error::CustomErrorWithMessage(string)
}
}
}

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);
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

No branches or pull requests

1 participant