Browse Source

Avoid ArgumentOutOfRangeException in ExpressionHelper.CheckForString (#1649)

pull/1650/head
Joe Hull 3 years ago committed by GitHub
parent
commit
1edd5ee5ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      src/Generator/Passes/ExpressionHelper.cs
  2. 9
      tests/CSharp/CSharp.Tests.cs
  3. 12
      tests/CSharp/CSharp.h

11
src/Generator/Passes/ExpressionHelper.cs

@ -427,9 +427,18 @@ namespace CppSharp.Internal
if (typeInSignature is CILType managed && managed.Type == typeof(string)) if (typeInSignature is CILType managed && managed.Type == typeof(string))
{ {
result = result[result.IndexOf("\"")..]; // It is possible that "result" is not a string literal. See, for
// example, the test case MoreVariablesWithInitializer in CSharp.h.
// Test for presence of a quote first to avoid
// ArgumentOutOfRangeException.
var initialQuoteIndex = result.IndexOf("\"");
if (initialQuoteIndex >= 0)
{
result = result[initialQuoteIndex..];
return true; return true;
} }
return false;
}
} }
return false; return false;
} }

9
tests/CSharp/CSharp.Tests.cs

@ -934,6 +934,15 @@ public unsafe class CSharpTests
Assert.That(VariablesWithInitializer.BoolArray, Is.EqualTo(new[] { false, true })); Assert.That(VariablesWithInitializer.BoolArray, Is.EqualTo(new[] { false, true }));
} }
[Test]
public void TestIndirectVariableInitializer()
{
// The actual test is that the generator doesn't throw when generating
// IndependentStringVariable. If we're running the test, we must have
// generated CSharp.cs without crashing the generator.
Assert.That(MoreVariablesWithInitializer.DependentStringVariable, Is.EqualTo(MoreVariablesWithInitializer.IndependentStringVariable));
}
[Test] [Test]
public void TestPointerPassedAsItsSecondaryBase() public void TestPointerPassedAsItsSecondaryBase()
{ {

12
tests/CSharp/CSharp.h

@ -1096,6 +1096,18 @@ public:
static constexpr float FloatArray[2] { 0.5020f, 0.6020f }; static constexpr float FloatArray[2] { 0.5020f, 0.6020f };
}; };
// Try to precipitate an ArgumentOutOfRangeException in ExpressionHelper.CheckForString.
//
// Note that uncommenting DLL_API below results in different behavior in
// ExpressionHelper.PrintExpression. In particular, ExpressionHelper.CheckForString is not
// called and no ArgumentOutOfRangeException is generated.
#define STRING_INITIALIZER "The quick brown fox"
struct /*DLL_API*/ MoreVariablesWithInitializer
{
static constexpr const char* IndependentStringVariable = STRING_INITIALIZER;
static constexpr const char* DependentStringVariable = IndependentStringVariable;
};
typedef void (*ALLCAPS_UNDERSCORES)(int i); typedef void (*ALLCAPS_UNDERSCORES)(int i);
class DLL_API TestString class DLL_API TestString

Loading…
Cancel
Save