json-rust icon indicating copy to clipboard operation
json-rust copied to clipboard

Update lifetimes of `read_complex_string`

Open vnrst opened this issue 3 years ago • 1 comments

The lifetimes of read_complex_string in parser.rs are weird. It returns a slice, but the lifetime of the slice is not tied to either the lifetime of self or the lifetime of the struct.

fn read_complex_string<'b>(&mut self, start: usize) -> Result<&'b str>

This could cause potential memory errors. Eg :

let sl2;
{
  let a: String = String::from("bftb\"ftbft");
  let sl: &str = &a[0..5];
  
  let mut parser = Parser::new(sl);
  parser.bump();
  sl2 = parser.read_complex_string(0);
}
println!("{:?}", sl2); // Will print garbage because the String has been dropped

This signature makes more sense and passes all tests.

fn read_complex_string(&mut self, start: usize) -> Result<&'_ str>

vnrst avatar Nov 09 '22 04:11 vnrst

Since this project is unmaintained, I forked to https://github.com/rustadopt/jzon-rs where I for example merged this PR and maintain it from now on with the community. It's also available in v0.12.5 on https://crates.io/crates/jzon

gierens avatar Aug 25 '23 17:08 gierens