Skip to content

Commit 8cc6742

Browse files
authored
Fixed a crash when passing null as an std::string on Unix. (#1012)
Fixed a crash when passing null as an std::string on Unix
1 parent b67e9bc commit 8cc6742

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,6 +2743,15 @@ public void GenerateFunctionCall(string functionName, List<Parameter> parameters
27432743
}
27442744
}
27452745

2746+
// special validation when constructing std::string as it cannot take null as a value
2747+
if (method != null && method.OriginalName == "basic_string" &&
2748+
method.TranslationUnit.IsSystemHeader)
2749+
{
2750+
WriteLine($"if (ReferenceEquals({method.Parameters[0].Name}, null))");
2751+
WriteLineIndent($@"throw new global::System.ArgumentNullException({
2752+
method.Parameters[0].Name}, ""The underlying std::string cannot take null."");");
2753+
}
2754+
27462755
if (needsReturn && !originalFunction.HasIndirectReturnTypeParameter)
27472756
Write("var {0} = ", Helpers.ReturnIdentifier);
27482757

tests/Common/Common.Tests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,15 @@ This is a very long string. This is a very long string. This is a very long stri
765765
}
766766
}
767767

768+
[Test, Platform(Exclude = "Win")]
769+
public void TestNullStdString()
770+
{
771+
using (var hasStdString = new HasStdString())
772+
{
773+
Assert.That(() => hasStdString.TestStdString(null), Throws.ArgumentNullException);
774+
}
775+
}
776+
768777
private class CustomDerivedFromVirtual : AbstractWithVirtualDtor
769778
{
770779
public override void Abstract()

0 commit comments

Comments
 (0)