Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add invariant check to InternableString.ExpensiveConvertToString #6798

Merged
merged 3 commits into from
Sep 7, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/StringTools/InternableString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,14 @@ public unsafe string ExpensiveConvertToString()
}
}
}

// The invariant that Length is the sum of span lengths is critical in this unsafe method.
// Violating it may lead to memory corruption and, since this code tends to run under a lock,
// to hangs caused by the lock getting orphaned.
if (destPtr != resultPtr + Length)
{
throw new InvalidOperationException("Length property does not match sum of span lengths");
ladipro marked this conversation as resolved.
Show resolved Hide resolved
}
ladipro marked this conversation as resolved.
Show resolved Hide resolved
}
return result;
}
Expand Down