sanpii/todo-txt

Can parsing a todo line fail?

Closed this issue · 5 comments

The error type of parser::task (and thus of Task::from_str) is ():

pub fn task(line: &str) -> Result<Task, ()> { ... }

Does this mean that parsing a task can never fail?

  • If so, would it be an option to change it to Void from the void crate (and maybe ! once it is stable ?)
  • If not, what could cause a task parsing to fail, and could a more explicit type be used ?

Does this mean that parsing a task can never fail?

The parsing could fail, but I don’t know exactly when 🤔 Probably with a non-UTF8 string.

If not, what could cause a task parsing to fail, and could a more explicit type be used ?

I can return the nom error, it’s more explicit but absolutly not user readable 😕

Probably with a non-UTF8 string.

Well, as the input of the parser is a &str, this cannot happen. Do you think there may be other error cases?

I ran this fuzz until 30 minutes without failure:

#![no_main]

#[macro_use]
extern crate libfuzzer_sys;
extern crate todo_txt;

fuzz_target!(|data: &[u8]| {
    if let Ok(s) = ::std::str::from_utf8(data) {
        ::todo_txt::parser::task(s)
            .unwrap();
    }
});

You’re right, the parser probably can’t fail.

If it really can't fail, then why use Result<Task, Void> instead of just Task ?

The task function could just return Task if it can't fail, indeed. <Task as FromStr>::from_str must return a Result as imposed by the FromStr trait on the other hand.