You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Refactor from 0.7.1 to 0.10.0 causing unrecoverable crash #36
The checked_shl() method is available, but it may have perf implications.
Hm, I don't quite understand how that could help here, can you elaborate?
IMHO a check comparing index against the length of chars is necessary before doing chars[index] to prevent this bug from happening - this should be possible using a simple condition and reacting with some kind of error when it is triggered. Or maybe just constrain the loop with an upper bound/switch to a while loop (as done in the function wrapping trans) so it may not run indefinitely. This should hopefully only have a negligible performance impact.
Oh wow, that's quite the impact. Okay, I think I understand a bit better now. I'm not totally up to speed has to how the polyline encoding/decoding works, I will probably do some research on it.
I was thinking something like this - it's basically the current code but the loop {} has been changed to a while {}. What do you think?
I don't have rust environment set up currently, so I wrote this out of thin air:
fntrans(chars:&[u8],mutindex:usize) -> Result<(i64,usize),String>{letmut at_index;letmut shift = 0;letmut result = 0;letmut byte;letmut valid = False;while index < chars.len(){
at_index = chars[index];
byte = (at_index asu64).saturating_sub(63);
index += 1;
result |= (byte &0x1f) << shift;
shift += 5;if byte < 0x20{
valid = True;break;}}if not valid {// this would mean that we have terminated above loop// but not because of the original condition on "byte < 0x20",// so this should be treated as an errorErr( ? )}let coordinate_change = if(result &1) > 0{
!(result >> 1)}else{
result >> 1}asi64;Ok((coordinate_change, index))}
fntrans(chars:&[u8],mutindex:usize) -> Result<(i64,usize),String>{letmut at_index;letmut shift = 0;letmut result = 0;letmut byte;letmut valid = false;while index < chars.len(){
at_index = chars[index];
byte = (at_index asu64).saturating_sub(63);
index += 1;
result |= (byte &0x1f) << shift;
shift += 5;if byte < 0x20{
valid = true;break;}}if !valid {// this would mean that we have terminated above loop// but not because of the original condition on "byte < 0x20",// so this should be treated as an errorreturnErr("Couldn't shift".to_string());}let coordinate_change = if(result &1) > 0{
!(result >> 1)}else{
result >> 1}asi64;Ok((coordinate_change, index))}
The problem is this line: result |= (byte & 0x1f) << shift;
While I'm aware that this does not fix the actual error per-se, it should at least handle the binary crashing violently, right? I understand that it would be desirable to also know the exact character that is invalid though.
Currently, the main thing thats bugging me is that certain input to the pypolyline.cutil.decode_polyline function causes the rust binary to crash which then also kills the python interpreter, so there is no way of handling this problem in some way.
This is not much different than the "solution" I proposed earlier, but might further clarify the situation:
Both of your solutions work with release optimisations, and have negligible perf impact. But they don't work with test optimisations (running cargo test) presumably due to overflow checks, so we need to figure out a way around that.
There's no need to worry about the pypolyline crash – once we're able to correctly return a Result from trans the error will bubble up to the wrapper and pypolyline will do the right thing.
Ah, okay! So if I read you correctly, cargo detects a possible overflow on byte due to the repeated ORing? Then I also understand better why you'd rather fix the root of the problem. I'll try to think of a solution as well.
fntrans(chars:&[u8],mutindex:usize) -> Result<(i64,usize),String>{letmut at_index;letmut shift = 0;letmut result = 0;letmut byte;loop{
at_index = chars[index];
byte = (at_index asu64).saturating_sub(63);
index += 1;// Protect against overflow by checking if any bits would be moved out of the u64if((byte &0x1f) >> (64 - shift)) != 0{Err(format!("Couldn't decode character at index {}", index - 1))}
result |= (byte &0x1f) << shift;
shift += 5;if byte < 0x20{break;}}let coordinate_change = if(result &1) > 0{
!(result >> 1)}else{
result >> 1}asi64;Ok((coordinate_change, index))}
Edit:
I haven't fully read up on the logic of polyline decoding, but probably we just need to check if (64 - shift) > 5, e.g. if another shift by 5 is still possible without overflow?
loop{
at_index = chars[index];
byte = (at_index asu64).saturating_sub(63);
result |= (byte &0x1f) << shift;if byte < 0x20{break;}
index += 1;
shift += 5;if shift > (64 - 5){Err(format!("Couldn't decode character at index {}", index - 1))}}
Activity
urschrei commentedon Apr 24, 2024
@purew I believe the perf changes you made have this side effect. Can you take a look?
urschrei commentedon Apr 24, 2024
The
checked_shl()method is available, but it may have perf implications.mattiZed commentedon Apr 24, 2024
Hm, I don't quite understand how that could help here, can you elaborate?
IMHO a check comparing
indexagainst the length ofcharsis necessary before doingchars[index]to prevent this bug from happening - this should be possible using a simple condition and reacting with some kind of error when it is triggered. Or maybe just constrain the loop with an upper bound/switch to a while loop (as done in the function wrappingtrans) so it may not run indefinitely. This should hopefully only have a negligible performance impact.urschrei commentedon Apr 24, 2024
Sure.
However, the performance impact of this change is huge, so it's not feasible:
Please post some code demonstrating your idea so we can test and benchmark it!
mattiZed commentedon Apr 24, 2024
Oh wow, that's quite the impact. Okay, I think I understand a bit better now. I'm not totally up to speed has to how the polyline encoding/decoding works, I will probably do some research on it.
I was thinking something like this - it's basically the current code but the
loop {}has been changed to awhile {}. What do you think?I don't have rust environment set up currently, so I wrote this out of thin air:
Edit: slightly refactored.
urschrei commentedon Apr 24, 2024
That doesn't catch the error, unfortunately:
The problem is this line:
result |= (byte & 0x1f) << shift;mattiZed commentedon Apr 24, 2024
While I'm aware that this does not fix the actual error per-se, it should at least handle the binary crashing violently, right? I understand that it would be desirable to also know the exact character that is invalid though.
Currently, the main thing thats bugging me is that certain input to the
pypolyline.cutil.decode_polylinefunction causes the rust binary to crash which then also kills the python interpreter, so there is no way of handling this problem in some way.This is not much different than the "solution" I proposed earlier, but might further clarify the situation:
urschrei commentedon Apr 24, 2024
Both of your solutions work with release optimisations, and have negligible perf impact. But they don't work with test optimisations (running
cargo test) presumably due to overflow checks, so we need to figure out a way around that.There's no need to worry about the
pypolylinecrash – once we're able to correctly return aResultfromtransthe error will bubble up to the wrapper andpypolylinewill do the right thing.mattiZed commentedon Apr 24, 2024
Ah, okay! So if I read you correctly, cargo detects a possible overflow on
bytedue to the repeatedORing? Then I also understand better why you'd rather fix the root of the problem. I'll try to think of a solution as well.mattiZed commentedon Apr 24, 2024
What about something like this?
Edit:
I haven't fully read up on the logic of polyline decoding, but probably we just need to check if
(64 - shift) > 5, e.g. if another shift by 5 is still possible without overflow?I have prepared a PR: #37
urschrei commentedon May 13, 2024
This has been fairly comprehensively addressed by #42, #43, #45, #47.