Avoid ArgumentOutOfRangeException in ExpressionHelper.CheckForString (#1649)

This commit is contained in:
Joe Hull 2022-01-21 09:07:24 -08:00 коммит произвёл GitHub
Родитель 81351632d5
Коммит 1edd5ee5ee
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 32 добавлений и 2 удалений

Просмотреть файл

@ -427,8 +427,17 @@ namespace CppSharp.Internal
if (typeInSignature is CILType managed && managed.Type == typeof(string))
{
result = result[result.IndexOf("\"")..];
return true;
// 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 false;
}
}
return false;

Просмотреть файл

@ -934,6 +934,15 @@ public unsafe class CSharpTests
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]
public void TestPointerPassedAsItsSecondaryBase()
{

Просмотреть файл

@ -1096,6 +1096,18 @@ public:
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);
class DLL_API TestString