diff --git a/src/libraries/System.Private.CoreLib/src/System/String.cs b/src/libraries/System.Private.CoreLib/src/System/String.cs index 45f4ac3e8f380a..4995a972ecd8e8 100644 --- a/src/libraries/System.Private.CoreLib/src/System/String.cs +++ b/src/libraries/System.Private.CoreLib/src/System/String.cs @@ -322,6 +322,10 @@ private static unsafe string Ctor(ReadOnlySpan value) public static string Create(int length, TState state, SpanAction action) where TState : allows ref struct { + // To support interop scenarios, the underlying buffer is guaranteed to be at least 1 greater than represented by the span parameter of the action callback. + // This additional index represents the null-terminator and, if written, that is the only value supported. + // Writing any value other than the null-terminator corrupts the string and is considered undefined behavior. + if (action is null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.action);