avitex / rust-dangerous

Rust library for safely and explicitly parsing untrusted data
MIT License
51 stars 4 forks source link

Fix take_consumed footgun #12

Closed avitex closed 3 years ago

avitex commented 3 years ago

Currently failing:

use dangerous::{Error, Invalid, Reader};

fn parse<'i, E: Error<'i>>(r: &mut Reader<'i, E>) -> Result<(), E> {
    let consumed = r.try_take_consumed(|r| {
        // We take a exact length of input
        r.consume(b"blah")
    })?;
    // This produces a `RetryRequirement` when it should be fatal?
    consumed.read_all(|r| r.consume(b"blah1"))
}

let input = dangerous::input(b"blah");
let result: Result<_, Invalid> = input.read_all(parse);

assert_eq!(result, Err(Invalid::fatal()));

This can be fixed with: consumed.bound(), but this is a footgun I don't like. What is written is a bug, but with the trust in RetryRequirement would cause a infinite loop of asking for more input rather than throwing an error. This would make finding the bug impossible in logs unless a max retry was used.

avitex commented 3 years ago

Fixed in https://github.com/avitex/rust-dangerous/commit/8bf55b049b095f48978310ee82fb38c93f65b0ec