Skip to content

Libs: (bug) string.Join assumes the input is immutable #134515

Closed

Description

@EgorBo

I don't think this is a security issue as it involves a data race + the API doesn't give such guarantees, but it looks like we should make it more durable?

string sep = new string(',', 4000);
string big = new string('x', 4000);
string?[] values = new string?[2];

ThreadPool.QueueUserWorkItem(_ =>
{
    while (true)
    {
        values[0] = big;
        values[0] = null;
    }
});

for (long i = 0; ; i++)
{
    _ = string.Join(sep, values);
}

Result:

Fatal error.

Description

string.Join(string separator, string?[] values) (and the other overloads that reach JoinCore) makes two passes. The first sums the lengths and allocates the result. The second copies. The second pass is meant to survive concurrent mutation of values (the code comment says "We range check again to avoid buffer overflows if this happens"). It re-checks each value's length against the remaining space, but then writes the separator through Unsafe.Add with no check. If another thread swaps an element from null or short to a longer string between the passes, the separator is written past the end of the newly allocated string, corrupting the GC heap. The final "copied the wrong amount, retry with a defensive copy" fallback runs only after the overflow. Only ordinary safe APIs are involved.

Activity

  1. added
    untriaged
    on Sep 23, 2026
  2. dotnet-policy-service commented on Sep 23, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-runtime
    See info in area-owners.md if you want to be subscribed.

  3. KalleOlaviNiemitalo commented on Sep 23, 2026

    @KalleOlaviNiemitalo

    Seems to have been caused by dotnet/coreclr#7942, between .NET Core 1.1 and 2.0. Before that, String.Join(String separator, String[] value, int startIndex, int count) used UnSafeCharBuffer.AppendString(string) for both separators and array elements, and that method threw IndexOutOfRangeException if the buffer filled up too early.

  4. GrabYourPitchforks commented on Sep 23, 2026

    @GrabYourPitchforks
    Member

    Agree: not a security issue. Mutating an input while an API is interpreting it is clearly operating outside the boundaries of what any API would ever reasonably be expected to support without explicit documentation to the contrary.

    Whether you want to make a behavioral change to be more robust in this scenario is entirely up to you.

  5. added a commit that references this issue on Sep 23, 2026
    4c25eb6
  6. removed
    untriaged
    on Sep 23, 2026
  7. locked and limited conversation to collaborators on Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions