statiolake / proconio-rs

Apache License 2.0
126 stars 7 forks source link

Possible undefined behavior: dangling reference exists #13

Closed statiolake closed 1 year ago

statiolake commented 4 years ago

https://github.com/statiolake/proconio-rs/blob/1e72f67b3420d61a0ee8cae922e55c5d0db89227/proconio/src/source/line.rs#L53-L54

The first line drops the original string contained in current_context so at this moment tokens is referring to the freed memory. The existence of the dangling reference is considered to be an undefined behavior, this code should be fixed. Store the string in a local variable, assign the refeference first, then assign the context.

Also we can probably use Box::leak() to get &’static str and can remove transmute() s.