String::retain allows safely creating invalid (non-utf8) strings when abusing panic #78498
Labels
C-bug
Category: This is a bug.
I-unsound
Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
P-high
High priority
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
While
String::retain
executes it may temporarily leave theString
in an inconsistent state, in particular it may contain invalid utf8. This is safe because it restores this invariant before returning, but the caller may skip this by panicing inside the closure and catching the unwind it outside. This allows to createString
s that are not utf8, breaking the library invariant without usingunsafe
.For example the following will panic at the final assertion, while I would expect it to never fail when
s
has typeString
:The text was updated successfully, but these errors were encountered: