Skip to content

fix: heap overflow when copying an undirected graph into a CSR - #670

Merged
Becheler merged 3 commits into
boostorg:developfrom
Becheler:fix/csr-copy-from-undirected
Oct 28, 2026
Merged

Becheler merged 3 commits into
boostorg:developfrom
Becheler:fix/csr-copy-from-undirected

Conversation

@Becheler

@Becheler Becheler commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

fixes a memory bug that happens when copying an undirected graph into a CSR graph.

Motivation

The CSR copy code tries to detect an undirected source so it can reserve room for twice as many edges, since an undirected edge shows up from both ends. The check compares against undirectedS, but an undirected graph reports undirected_tag, so the check never matches. The CSR therefore gets room for E edges and then writes 2E into it. That is a heap overflow that can crash or silently corrupt memory, and it has been there since the 2009 fix for ticket #3357.

What this PR does

Instead of guessing from the graph's type, the CSR counts the edges it will actually copy by adding up out_degree over all vertices. That is always the exact size, self-loops included.

note: num_edges on a filtered_graph returns the unfiltered edge count, so summing out_degree over every vertex is the only way to size the CSR to exactly the edges the copy will write, at a cost of one extra pass that is no larger than the copy itself. Copying a 1M-vertex, 16M-edge graph into a CSR took 57.3 ms with the fix versus 56.7 ms on develop, which is inside noise rnage (something like ~1%), while an alternative that skips the counting pass to grow the array while copying took ~3x longer (179 ms).

  1. First commit: expand test coverage to guard regressions (expect green CI)
  2. Add test to hilight bug (expect red CI)
  3. Fix bug (expect green CI)

Before submitting

  • I searched for an existing PR or issue covering the same change.
  • This PR targets the develop branch.
  • My contribution is licensed under the Boost Software License 1.0.

Kind of change

Tick exactly one. Each kind is reviewed differently, so mixing them in one PR makes the diff difficult to review (why).

  • Reformat (layout only, no token added or removed)
  • Refactor (no observable behavior change)
  • Behavior change (observable to users of the library)
  • No code change (documentation, build, CI, or tooling)

Change category

  • Bug fix
  • New algorithm or API addition
  • Deprecation or removal
  • Performance
  • Tests
  • Documentation
  • Build, CI, or tooling
  • Other (specify below)

@Becheler Becheler self-assigned this Oct 26, 2026
@Becheler Becheler added test Adding, fixing or modernizing unit tests fix labels Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Boost dependency footprint vs develop (auto-generated).
PR run 40339466575 vs develop run 39949899700 (62ed6e2d67).

Header-inclusion weights (graph files pulling each direct dependency in):

Dependency develop PR Δ
range 8 9 +1

Transitive Boost modules: 47 → 47 (0)

@Becheler
Becheler force-pushed the fix/csr-copy-from-undirected branch from fc8e4e6 to f6084ed Compare October 26, 2026 12:23
@Becheler Becheler changed the title test: add coverage fix: heap overflow when copying an undirected graph into a CSR Oct 26, 2026
@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Compiler-warning counts vs develop (auto-generated).
PR run 40339466228 vs develop run 39949899676 (62ed6e2d67).

Job Baseline After Delta
macos (clang, 14) 256 256 0
macos (clang, 17) 255 255 0
macos (clang, 20) 255 255 0
ubuntu (clang-19, 14) 256 256 0
ubuntu (clang-19, 17) 255 255 0
ubuntu (clang-19, 20) 255 255 0
ubuntu (clang-19, 23) 255 255 0
ubuntu (gcc-14, 14) 352 352 0
ubuntu (gcc-14, 17) 348 348 0
ubuntu (gcc-14, 20) 348 348 0
ubuntu (gcc-14, 23) 348 348 0
windows_msvc_14_3 (msvc-14.3) 961 961 0

@Becheler
Becheler force-pushed the fix/csr-copy-from-undirected branch from 7d5fbe0 to 62ed6e2 Compare October 26, 2026 14:25
@Becheler
Becheler marked this pull request as ready for review October 27, 2026 11:38
@Becheler Becheler added data structure and removed test Adding, fixing or modernizing unit tests labels Oct 27, 2026
@Becheler
Becheler merged commit 08d01bd into boostorg:develop Oct 28, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant