Browse Source

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
pull/1017/head
Dimitar Dobrev 8 years ago committed by GitHub
parent
commit
8cc67420d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 9
      tests/Common/Common.Tests.cs

9
src/Generator/Generators/CSharp/CSharpSources.cs

@ -2743,6 +2743,15 @@ namespace CppSharp.Generators.CSharp @@ -2743,6 +2743,15 @@ namespace CppSharp.Generators.CSharp
}
}
// special validation when constructing std::string as it cannot take null as a value
if (method != null && method.OriginalName == "basic_string" &&
method.TranslationUnit.IsSystemHeader)
{
WriteLine($"if (ReferenceEquals({method.Parameters[0].Name}, null))");
WriteLineIndent($@"throw new global::System.ArgumentNullException({
method.Parameters[0].Name}, ""The underlying std::string cannot take null."");");
}
if (needsReturn && !originalFunction.HasIndirectReturnTypeParameter)
Write("var {0} = ", Helpers.ReturnIdentifier);

9
tests/Common/Common.Tests.cs

@ -765,6 +765,15 @@ This is a very long string. This is a very long string. This is a very long stri @@ -765,6 +765,15 @@ This is a very long string. This is a very long string. This is a very long stri
}
}
[Test, Platform(Exclude = "Win")]
public void TestNullStdString()
{
using (var hasStdString = new HasStdString())
{
Assert.That(() => hasStdString.TestStdString(null), Throws.ArgumentNullException);
}
}
private class CustomDerivedFromVirtual : AbstractWithVirtualDtor
{
public override void Abstract()

Loading…
Cancel
Save