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
You have some problems with hygiene of identifiers. Basically, identifiers from inner scope can pollute outer scope and it leads to functionally different logic being executed.
Example is a bit artificial, because I tried to minimize it to show the idea:
This works without panic:
let val = Some(vec![0_usize]);letmut v = vec![1];match val {Some(mut v) => drop(v.pop()),None => {}};
v.pop().unwrap();
This panics:
let val = Some(vec![0_usize]);letmut v = vec![1];cain!{let _ = match val {Some(mut v) => drop(v.pop()),
None => {}};
v.pop().unwrap();
}
Reason it panics is it expands to:
let val = Some(<[_]>::into_vec(box [0_usize]));letmut v = <[_]>::into_vec(box [1]);{match val {Some(mut v) => {let _ = drop(v.pop());
v.pop().unwrap();}None => {let _ = {};
v.pop().unwrap();}}}
and second v.pop() gets executed on inner identifier v except of outer identifier v
This is trivial case which can be solved by "sanitizing" identifiers in arms and "un-sanitizing" them in the scope so they won't get leaked:
match val {Some(mut v_randomized213345) => {let _ = {letmut v = v_randomized213345;drop(v.pop())};
v.pop().unwrap();}None => {let _ = {};
v.pop().unwrap();}}
but there may be other cases which are harder to notice, I'm not sure how can you formally guarantee that code logic and behavior won't get affected.
The text was updated successfully, but these errors were encountered:
Reported by Reddit user u/pinespear in this comment:
The text was updated successfully, but these errors were encountered: