json icon indicating copy to clipboard operation
json copied to clipboard

Wrong positions for `InvalidUnicodeCodePoint` caused by `from_utf8`

Open samuelcolvin opened this issue 2 years ago • 0 comments

Given the following example:

fn main() {
    let json_vec = vec![34, 92, 34, 206, 44, 163, 34];
    let error = serde_json::from_slice::<serde_json::Value>(&json_vec).unwrap_err();
    dbg!(error.to_string());
}

Serde JSON gives the error "invalid unicode code point at line 1 column 7", but I would argue that the error is at column 4, not 7.

I think this is caused by one of the *from_utf8 calls, e.g. this:

        let raw = match String::from_utf8(raw) {
            Ok(raw) => raw,
            Err(_) => return error(self, ErrorCode::InvalidUnicodeCodePoint),
        };

Instead of using the position of the end of of the string, Serde JSON should use start + e.valid_up_to() + 1, similar to this code.

samuelcolvin avatar Nov 02 '23 16:11 samuelcolvin