Fix unsafe memory access.
This does fix PR26908.
Please mention the bug in the commit message (you might want to mention it was a use-after-free problem too).
This patch was just to demonstrate what I want to see so it lacked a commit
message. However, if you are okay with this, I'll write a description for
it and submit.
LGTM. Thanks for improving it!