statiolake/proconio-rs

Possible undefined behavior: dangling reference exists

Closed this issue · 0 comments

self.current_context = line.into_boxed_str();
self.tokens = unsafe { std::mem::transmute::<_, &'static str>(&*self.current_context) }

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.