From 0361715810d198bac52a3c7d9a33f0783026269e Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Thu, 11 Jul 2019 01:11:27 +0300 Subject: [PATCH] Expose a bug when passing a const char* without copying it Signed-off-by: Dimitar Dobrev --- tests/CSharp/CSharp.Tests.cs | 6 ++++++ tests/CSharp/CSharp.cpp | 18 ++++++++++++++++++ tests/CSharp/CSharp.h | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/tests/CSharp/CSharp.Tests.cs b/tests/CSharp/CSharp.Tests.cs index c45c99e6..68e52b3f 100644 --- a/tests/CSharp/CSharp.Tests.cs +++ b/tests/CSharp/CSharp.Tests.cs @@ -1283,6 +1283,12 @@ public unsafe class CSharpTests : GeneratorTestFixture [Test] public void TakeRefToPointerToObject() { + const string test = "test"; + using (Latin1 l = new Latin1(test)) + { + Assert.That(l.Size, Is.EqualTo(test.Length)); + Assert.That(l.Data, Is.EqualTo(test)); + } using (Foo foo = new Foo { A = 25 }) { Foo returnedFoo = CSharp.CSharp.TakeReturnReferenceToPointer(foo); diff --git a/tests/CSharp/CSharp.cpp b/tests/CSharp/CSharp.cpp index 35611676..07c5a367 100644 --- a/tests/CSharp/CSharp.cpp +++ b/tests/CSharp/CSharp.cpp @@ -35,6 +35,24 @@ Foo::Foo(const Foo& other) : A(other.A), P(other.P), { } +Latin1::Latin1(const char *s) : m_size(s ? int(strlen(s)) : 0), m_data(s) +{ +} + +Latin1::~Latin1() +{ +} + +int Latin1::size() const +{ + return m_size; +} + +const char* Latin1::data() const +{ + return m_data; +} + Foo::~Foo() { } diff --git a/tests/CSharp/CSharp.h b/tests/CSharp/CSharp.h index 5e697e47..fe1cf423 100644 --- a/tests/CSharp/CSharp.h +++ b/tests/CSharp/CSharp.h @@ -8,6 +8,18 @@ #include "AnotherUnit.h" #include "CSharpTemplates.h" +class DLL_API Latin1 +{ +public: + explicit Latin1(const char *s); + ~Latin1(); + int size() const; + const char* data() const; +private: + int m_size; + const char *m_data; +}; + class DLL_API Foo { public: