Skip to content

Commit 50feab0

Browse files
committed
Free returned by value instances, not object ones
This was a dangerous bug by as the object remained in a deleted state to be used later. Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent e2d0dce commit 50feab0

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,16 @@ public override bool VisitClassDecl(Class @class)
300300
if (Context.Context.ParserOptions.IsMicrosoftAbi)
301301
vtableIndex = @class.Layout.VFTables.IndexOf(@class.Layout.VFTables.First(
302302
v => v.Layout.Components.Any(c => c.Method == dtor)));
303+
string instance = $"new {typePrinter.IntPtrType}(&{Context.ReturnVarName})";
303304
Context.Before.WriteLine($@"var __vtables = new IntPtr[] {{ {
304305
string.Join(", ", originalClass.Layout.VTablePointers.Select(
305-
x => $" * (IntPtr*) ({ Helpers.InstanceIdentifier} + {x.Offset})"))} }};");
306-
Context.Before.WriteLine($"var __slot = *(IntPtr*) (__vtables[{vtableIndex}] + {i} * sizeof(IntPtr));");
307-
Context.Before.Write($"Marshal.GetDelegateForFunctionPointer<{dtor.FunctionType}>(__slot)({Helpers.InstanceIdentifier}");
306+
x => $"*({typePrinter.IntPtrType}*) ({instance} + {x.Offset})"))} }};");
307+
Context.Before.WriteLine($@"var __slot = *({typePrinter.IntPtrType}*) (__vtables[{
308+
vtableIndex}] + {i} * sizeof({typePrinter.IntPtrType}));");
309+
Context.Before.Write($"Marshal.GetDelegateForFunctionPointer<{dtor.FunctionType}>(__slot)({instance}");
308310
if (dtor.GatherInternalParams(Context.Context.ParserOptions.IsItaniumLikeAbi).Count > 1)
309311
{
310-
Context.Before.WriteLine(", 0");
312+
Context.Before.Write(", 0");
311313
}
312314
Context.Before.WriteLine(");");
313315
}

src/Generator/Generators/CSharp/CSharpSources.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2689,7 +2689,7 @@ public void GenerateMethod(Method method, Class @class)
26892689
}
26902690

26912691
private bool GenerateMethodBody(Class @class, Method method,
2692-
QualifiedType returnType = default(QualifiedType))
2692+
QualifiedType returnType = default)
26932693
{
26942694
var specialization = @class as ClassTemplateSpecialization;
26952695
if (specialization != null)

tests/Common/Common.Tests.cs

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public unsafe void TestCodeGeneration()
5252
{
5353
hasPropertyNamedAsParent.hasPropertyNamedAsParent.GetHashCode();
5454
}
55+
using (Common.FreeFunctionReturnsVirtualDtor)
56+
{
57+
}
5558
EnumWithUnderscores.lOWER_BEFORE_CAPITAL.GetHashCode();
5659
EnumWithUnderscores.UnderscoreAtEnd.GetHashCode();
5760
EnumWithUnderscores.CAPITALS_More.GetHashCode();

tests/Common/Common.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,18 @@ AbstractFoo::~AbstractFoo()
386386
{
387387
}
388388

389+
ImplementsAbstractFoo::ImplementsAbstractFoo()
390+
{
391+
}
392+
393+
ImplementsAbstractFoo::ImplementsAbstractFoo(const ImplementsAbstractFoo& other)
394+
{
395+
}
396+
397+
ImplementsAbstractFoo::~ImplementsAbstractFoo()
398+
{
399+
}
400+
389401
int ImplementsAbstractFoo::pureFunction(typedefInOverride i)
390402
{
391403
return 5;
@@ -1349,6 +1361,11 @@ ReturnByValueWithReturnParam ReturnByValueWithReturnParamFactory::generate()
13491361
return ReturnByValueWithReturnParam();
13501362
}
13511363

1364+
ImplementsAbstractFoo freeFunctionReturnsVirtualDtor()
1365+
{
1366+
return ImplementsAbstractFoo();
1367+
}
1368+
13521369
void integerOverload(int i)
13531370
{
13541371
}

tests/Common/Common.h

+4
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ class DLL_API AbstractFoo
268268
class DLL_API ImplementsAbstractFoo : public AbstractFoo
269269
{
270270
public:
271+
ImplementsAbstractFoo();
272+
ImplementsAbstractFoo(const ImplementsAbstractFoo& other);
273+
~ImplementsAbstractFoo();
271274
typedef int typedefInOverride;
272275
virtual int pureFunction(typedefInOverride i = 0);
273276
virtual int pureFunction1();
@@ -1455,6 +1458,7 @@ template<int N> using TypeAlias = InvokeGenSeq<DerivedTypeAlias<N>>;
14551458
template<int N>
14561459
struct DerivedTypeAlias : TypeAlias<N / 2> {};
14571460

1461+
DLL_API ImplementsAbstractFoo freeFunctionReturnsVirtualDtor();
14581462
DLL_API void integerOverload(int i);
14591463
DLL_API void integerOverload(unsigned int i);
14601464
DLL_API void integerOverload(long i);

0 commit comments

Comments
 (0)