8000 Double free in Array.h · Issue #300 · ogdf/ogdf · GitHub
[go: up one dir, main page]

Skip to content

Double free in Array.h #300

Open

Description

@gctwnl

Note: this report has been produced by Claude Code, but I have estimated this is most likely not slop. I am running an experiment in how capable LLM Code is in a real situation of building a professional app (to be open sourced if it succeeds), see Anthropic/OpenAI may be spending more than $1000 for every $100 you pay them for teh first article that came out of that. The behaviour and fix are confirmed though.

Title:
Array::grow shrinking to size 0 calls realloc(p, 0): InsufficientMemoryException, then a double free, with the MSVC runtime (and glibc)

OGDF version
foxglove-202510 (tag). The same code is on master as of 2026-09-13.

Where observed
Windows 10 Version 22H2 (build 19045), x64, Visual Studio 2026 (MSVC toolset 14.51), OGDF built
as a static library against the dynamic runtime (/MD), release configuration, OGDF_DEBUG off.
Not observed on macOS: Apple's realloc(p, 0) frees the block and returns a new minimum-size block,
so the code path below never throws there.

Summary
When an ogdf::Array of a trivially copyable element type shrinks to zero elements,
Array<E, INDEX>::expandArray calls expandArrayHelper(sOld, 0), which calls realloc(m_pStart, 0).
The behaviour of realloc with size 0 is implementation-defined in C17 (and undefined in C23).
The MSVC runtime frees the block and returns nullptr; glibc does the same. expandArrayHelper
treats the nullptr as an allocation failure and throws InsufficientMemoryException, but
m_pStart still points at the block realloc has just freed. When the array is destroyed, whether
during the unwinding of that exception or later, Array::deconstruct frees the block a second
time. The MSVC heap detects the double free and terminates the process with
STATUS_HEAP_CORRUPTION (0xC0000374); no catch handler is reached.

The non-trivially-copyable expandArrayHelper has the same exposure through malloc(0), which
the C standard also allows to return nullptr (the MSVC runtime, glibc and macOS return a
non-null pointer, so it does not show in practice there).

Minimal reproducer
The array is deliberately leaked after the exception, so the program reports the defect
instead of crashing on the second free.

    #include <ogdf/basic/Array.h>

    #include <cstdio>
    #include <cstdlib>

    int main() {
        void* block = std::malloc(16);
        void* reallocated = std::realloc(block, 0);
        std::printf("realloc(p, 0) returned %s\n",
                reallocated == nullptr ? "nullptr (the block was freed)" : "a non-null block");
        std::free(reallocated);

        auto* array = new ogdf::Array<int*>(1);
        try {
            array->grow(-1);
            std::printf("Array<int*>(1).grow(-1): no exception, size %d\n", array->size());
            delete array;
            std::printf("the emptied array was destroyed without error\n");
        } catch (const ogdf::InsufficientMemoryException&) {
            std::printf("Array<int*>(1).grow(-1): InsufficientMemoryException thrown; the array still "
                        "points at the freed block, so destroying it would free that block a second time\n");
        }
        return 0;
    }

Built with: cl -EHsc -std:c++17 -MD -O2 -DNDEBUG (OGDF include directories) reproducer.cpp OGDF.lib

Output with foxglove-202510 as released:
realloc(p, 0) returned nullptr (the block was freed)
Array<int*>(1).grow(-1): InsufficientMemoryException thrown; the array still points at the freed block, so destroying it would free that block a second time

Output with the fix below:
realloc(p, 0) returned nullptr (the block was freed)
Array<int*>(1).grow(-1): no exception, size 0
the emptied array was destroyed without error

How it shows up in a layout
A clustered SugiyamaLayout::call (on ClusterGraphAttributes) terminated the application. The
crash dump's main thread, OGDF part, innermost first:

ntdll!RtlReportFatalFailure                     (0xC0000374, heap corruption: block not busy, double free)
ntdll!RtlFreeHeap
ucrtbase!_free_base
ogdf::Array<ogdf::LHTreeNode *,int>::{dtor}
ogdf::ENGLayer::~ENGLayer
ogdf::Array<ogdf::ENGLayer,int>::deconstruct
ogdf::ExtendedNestingGraph::~ExtendedNestingGraph   (stack unwinding)
  ... C++ exception dispatch ...
VCRUNTIME140!_CxxThrowException                 (InsufficientMemoryException)
ogdf::Array<ogdf::LHTreeNode *,int>::expandArrayHelper
ogdf::Array<ogdf::LHTreeNode *,int>::expandArray    (add = -1)
ogdf::Array<ogdf::LHTreeNode *,int>::grow           (sOld = 1)
ogdf::LHTreeNode::removeAuxChildren                 (i = 1: the only child was an AuxNode)
ogdf::ENGLayer::removeAuxNodes
ogdf::ExtendedNestingGraph::removeAuxNodes

56A6
ogdf::ExtendedNestingGraph::removeTopBottomEdges
ogdf::SugiyamaLayout::call

LHTreeNode::removeAuxChildren deletes the auxiliary children and calls m_child.grow(add, nullptr)
with add = j - i. For a compound node whose only child is an auxiliary node, that is grow(-1) on
a one-element array, which shrinks it to zero elements and takes the path above. The
ExtendedNestingGraph is a local of SugiyamaLayout::call, so its destructor runs while the
exception unwinds and performs the second free.

The code involved (include/ogdf/basic/Array.h)

    void expandArrayHelper(INDEX sOld, INDEX sNew) {          // trivially copyable E
        E* p = static_cast<E*>(realloc(m_pStart, sNew * sizeof(E)));
        if (p == nullptr) {
            OGDF_THROW(InsufficientMemoryException);
        }
        m_pStart = p;
    }

    void Array<E, INDEX>::expandArray(INDEX add) {
        INDEX sOld = size(), sNew = sOld + add;
        if (m_pStart != nullptr) {
            expandArrayHelper(sOld, sNew);
        } else { ... malloc ... }
        m_vpStart = m_pStart - m_low;
        m_pStop = m_pStart + sNew;
        m_high += add;
    }

Proposed fix
In expandArray, release the block when the array shrinks to no elements, leaving the same empty
state Array::construct uses for an empty index range. This covers both helpers.

    --- a/include/ogdf/basic/Array.h
    +++ b/include/ogdf/basic/Array.h
    @@ -810,6 +810,17 @@
     void Array<E, INDEX>::expandArray(INDEX add) {
     	INDEX sOld = size(), sNew = sOld + add;

    +	// Shrinking to no elements releases the block. realloc(p, 0) and malloc(0) may return
    +	// nullptr (the MSVC runtime and glibc free the block and return nullptr for realloc), which
    +	// the helpers would report as an allocation failure while m_pStart still points at the freed
    +	// block, so that the destructor frees it a second time.
    +	if (sNew == 0) {
    +		deconstruct();
    +		m_pStart = m_vpStart = m_pStop = nullptr;
    +		m_high += add;
    +		return;
    +	}
    +
     	// expand allocated memory block
     	if (m_pStart != nullptr) {
     		expandArrayHelper(sOld, sNew);

With this change the reproducer runs as shown above.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0