Safety question

I'm working on a parser. The parser takes an input in [u8] and spits out offsets. E.g. imagine it works like following:

    "String value"
 // ^            ^
 // |            |
 // 0            14
 // The `^` denotes spans, so they would effectively be 

Seeing how all my separators are always ASCII and the spans return will always return the position of said chars, do I need to make following trait unsafe?

   trait ParserSearcher {
       fn find_quote(&[u8]) -> [usize];
   }

  //  typical usage
  let search = ParserSearcher::find_quote(bytes);
  let quoted = unsafe {
      bytes.get_unchecked(search[0], search[1])
  }

In theory, it could be used to extract invalid points in a byte array?

I ask because it's similar but unsure if same as Rust Searcher trait.

Yes, the trait needs to be unsafe if you expect unsafe code can rely on find_quote's correctness.

Though in this particular case, I think find_quote can return &[u8] directly if the only usage of the index is indexing the original buffer.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.