[flang][runtime] Fixed memory leak in Assign().
authorSlava Zakharin <szakharin@nvidia.com>
Mon, 15 May 2023 16:52:14 +0000 (09:52 -0700)
committerSlava Zakharin <szakharin@nvidia.com>
Mon, 15 May 2023 17:48:46 +0000 (10:48 -0700)
The temporary descriptor must be either Pointer or Allocatable,
otherwise its memory will not be freed.

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D150534

flang/runtime/assign.cpp

index 9e10e2f..f75fe94 100644 (file)
@@ -294,6 +294,10 @@ static void Assign(
       StaticDescriptor<maxRank, true, 16> staticDesc;
       Descriptor &newFrom{staticDesc.descriptor()};
       std::memcpy(&newFrom, &from, descBytes);
+      // Pretend the temporary descriptor is for an ALLOCATABLE
+      // entity, otherwise, the Deallocate() below will not
+      // free the descriptor memory.
+      newFrom.raw().attribute = CFI_attribute_allocatable;
       auto stat{ReturnError(terminator, newFrom.Allocate())};
       if (stat == StatOk) {
         char *toAt{newFrom.OffsetElement()};