Skip to content

Reject sample and ancestor IDs equal to num_nodes in link_ancestors (fixes #3484) - #3492

Closed
asystemoffields wants to merge 2 commits into
tskit-dev:mainfrom
asystemoffields:link-ancestors-node-bounds
Closed

asystemoffields wants to merge 2 commits into
tskit-dev:mainfrom
asystemoffields:link-ancestors-node-bounds

Conversation

@asystemoffields

Copy link
Copy Markdown

Description

Fixes #3484

ancestor_mapper_init_samples and ancestor_mapper_init_ancestors validated node IDs with > nodes.num_rows instead of >= nodes.num_rows, so a sample or ancestor ID equal to the number of nodes passed the check and was then used to index is_sample, is_ancestor and the ancestor map arrays, all allocated with exactly num_nodes entries. A standalone C program calling tsk_table_collection_link_ancestors with such an ID on a three-node table segfaults; through the Python API, tables.link_ancestors(samples=[0, 1], ancestors=[num_nodes]) on a two-sample msprime tree sequence returned normally.

  • c/tskit/tables.c: >= in both checks, so these IDs return TSK_ERR_NODE_OUT_OF_BOUNDS like the other node bounds checks in the file.
  • c/tests/test_tables.c: test_link_ancestors_input_errors gains the one-past-the-end sample and ancestor cases. On the current code these two assertions happen to pass on the seven-node test table (the out-of-bounds accesses land in allocator padding and the run completes), so the C test documents the behaviour rather than catching the regression; the Python test below does fail without the fix.
  • python/tests/test_python_c.py: two cases added to test_link_ancestors_bad_args (fail on main, pass with the fix). test_link_ancestors in the same file asked for ancestor 3 of a tree sequence with nodes 0–2 and only passed because of this bug; it now asks for the root, node 2.
  • Changelog entries in c/CHANGELOG.rst and python/CHANGELOG.rst; the :pr: numbers will be filled in once this PR exists.

Testing here: ninja -C build test_tables && ./build/test_tables (CUnit built from source locally): 34208 asserts, 0 failures; pytest python/tests/test_python_c.py python/tests/test_topology.py python/tests/test_highlevel.py python/tests/test_immutable_table_collection.py -k link_ancestors: 110 passed, 6 skipped; clang-format reports no changes.

PR Checklist:

  • Tests that fully cover new/changed functionality.
  • Documentation including tutorial content if appropriate. (no documented behaviour changes)
  • Changelogs, if there are API changes.

Prepared with Claude Code (Claude Fable 5.1) and submitted on the account owner's authorisation; the runs above were done as described.

🤖 Generated with Claude Code

asystemoffields and others added 2 commits October 18, 2026 19:05
ancestor_mapper_init_samples and ancestor_mapper_init_ancestors checked
IDs with `> num_rows` instead of `>= num_rows`, so an ID equal to the
number of nodes passed validation and was then used to index is_sample,
is_ancestor and the ancestor map arrays, all allocated with exactly
num_nodes entries. A standalone call with such an ID segfaults.

Use `>=`, add the one-past-the-end case to the C and Python error tests,
and fix test_link_ancestors in test_python_c.py, which asked for ancestor
3 of a tree sequence with nodes 0-2 and only passed because of the bug.

Fixes tskit-dev#3484

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@hyanwong

Copy link
Copy Markdown
Member

I think this is the same as #3489, which is waiting to be merged. But thanks for the extra spot @asystemoffields

@hyanwong

Copy link
Copy Markdown
Member

Closed as dupe of #3489

@hyanwong hyanwong closed this Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Codex security warning: link_ancestors accepts one-past node IDs and writes out of bounds

2 participants