Open nbigaouette opened 3 years ago
you can't do that, offset need to be used with the same origin, aka pointer, and the argument must be "same or after" self
. I think we should update doc on this one.
It's even probably UB to do it.
fn main() {
let input = b"[]";
assert_eq!(0, input.offset(&input));
}
should work correctly. See test here
Yes, looking further into this I realized my mistake. The parser I use with consumed()
is a custom one that returned &[]
in a specific case. Since this empty slice is not a subslice of the input the offset()
call returns bogus indices.
When I got the panic I checked the doc on any possible panic and did not find anything. I agree that this information should probably be part of the offset()
documentation. I would have found and fixed my issue :D
The parser I use with consumed() is a custom one that returned &[] in a specific case.
We should probably also advice to never do that and prefer, input[x..x]
, x
being the index where your parser decide to "valid" the input, x
can be 0..=input.len()
, should work better.
I'm not good in english so I pass my turn on this PR. Need to update at least:
&[]
in parser (or check if such guide already exist ?)That was the fix I made. Instead of returning &[]
(which we should never do) I returned (something like) &input[input.len()..]
. In my case I wanted the "offset" to be at the end of the input.
sure, I'll add some doc for that
Hi all!
Since I adapted my parser to use
consumed()
, I get weird panics coming from nom.I have trouble reproducing it with a MVE. Using a debugger on my test case though revealed the problem. Let me give a description of what happens...
When inside
consumed()
, thei
isb"[]"
(a byte slice containing an opening and closing brackets). After myparser
get called, thematch
goes to theOk
branch. There, bothremaining
andresult
are empty slices (&[]
). Then callinginput.offset(&remaining)
returns an out-of-range index!! Usinginput.slice(..index)
on the following line will thus panic.Here's a runable example that shows the bad offset calculation:
This prints the following:
If this offset is used to index the input, this will panic.
rustc 1.55.0 (c8dfcfe04 2021-09-06)