Skip to content

fix: segfault in FeatureLinkerUnlabeledKD with charge_merging=Any - #8944

Merged
timosachsenberg merged 2 commits into
developfrom
fix/feature-linker-kd-charge-any-segfault
Mar 20, 2026
Merged

timosachsenberg merged 2 commits into
developfrom
fix/feature-linker-kd-charge-any-segfault

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Fixes segfault when using -algorithm:link:charge_merging Any in FeatureLinkerUnlabeledKD
  • Root cause: FeatureDistance functor rejected charge-mismatched pairs that the KD candidate filter intentionally allowed, leaving best_index at SIZE_MAX → out-of-bounds vector access

Root Cause

The KD algorithm has two stages: a candidate filter (lines 369-387) that decides which features enter the pool, and a distance functor (FeatureDistance) that ranks them. When charge_merging="Any", the candidate filter allows all charge states through. But FeatureDistance had ignore_charge=false (hardcoded default — the param was removed from the KD algorithm's parameter set at construction). This caused it to return infinity for non-zero charge mismatches (e.g., charge 2 vs charge 3).

If all candidates from a given map had a different non-zero charge than the center feature, every distance was infinity, best_index stayed at numeric_limits<Size>::max(), and kd_data.feature(SIZE_MAX) segfaulted.

Fix

Set ignore_charge=true on FeatureDistance when charge_merging="Any". This is correct because the KD filter already handles charge filtering — FeatureDistance should only compute distances for the candidates that passed.

With_charge_zero is not affected because FeatureDistance's default charge logic (allow pairing when one side is charge 0) exactly matches what the KD filter allows through.

Test plan

  • All 18 existing FeatureLinkerUnlabeledKD tests pass (including tests 6+7 which use charge_merging=Any)
  • Reference output for charge_merging=Any tests is identical (no behavior change for existing test data)
  • CI passes

Closes #8927

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Prevented a crash during feature grouping when the "Any" charge-merging option is used, ensuring charge-mismatched pairs are handled safely.
  • Tests
    • Added a regression test and associated input/output fixtures to validate grouping behavior with "Any" charge merging.

When charge_merging="Any", the KD candidate filter allows features with
any charge into the pool. However, the FeatureDistance functor (which
ranks candidates) had ignore_charge=false, causing it to return infinity
for non-zero charge mismatches. If all candidates from a map had a
different non-zero charge than the center, best_index stayed at SIZE_MAX,
leading to an out-of-bounds access in addConsensusFeature_().

Fix: set ignore_charge=true on FeatureDistance when charge_merging="Any"
so the distance functor does not contradict the candidate filter.

Closes #8927

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: defbaef1-2fbb-4552-a856-319ecc10904d

📥 Commits

Reviewing files that changed from the base of the PR and between e69d0c3 and bdf2a6a.

📒 Files selected for processing (4)
  • src/tests/topp/CMakeLists.txt
  • src/tests/topp/FeatureLinkerUnlabeledKD_8_output.consensusXML
  • src/tests/topp/FeatureLinkerUnlabeledKD_charge_any_input1.featureXML
  • src/tests/topp/FeatureLinkerUnlabeledKD_charge_any_input2.featureXML
✅ Files skipped from review due to trivial changes (2)
  • src/tests/topp/FeatureLinkerUnlabeledKD_8_output.consensusXML
  • src/tests/topp/FeatureLinkerUnlabeledKD_charge_any_input2.featureXML

📝 Walkthrough

Walkthrough

A conditional was added in FeatureGroupingAlgorithmKD to set the distance parameter ignore_charge = true when link:charge_merging is "Any", so charge-mismatched pairs are not rejected during candidate selection.

Changes

Cohort / File(s) Summary
Core change
src/openms/source/ANALYSIS/MAPMATCHING/FeatureGroupingAlgorithmKD.cpp
Set ignore_charge to true in the distance configuration when link:charge_merging == "Any", ensuring FeatureDistance does not reject charge-mismatched pairs during grouping.
Tests - harness
src/tests/topp/CMakeLists.txt
Added a regression test invocation and validation target for TOPP_FeatureLinkerUnlabeledKD with charge_merging Any (new dependency and comment referencing issue #8927).
Tests - fixtures
src/tests/topp/FeatureLinkerUnlabeledKD_8_output.consensusXML, src/tests/topp/FeatureLinkerUnlabeledKD_charge_any_input1.featureXML, src/tests/topp/FeatureLinkerUnlabeledKD_charge_any_input2.featureXML
Added expected output consensusXML and two featureXML inputs exercising charge_merging=Any to validate grouping behavior and prevent the regression that caused a segfault.

Poem

🐰 A hop, a tweak, a tiny code mend,
Charges now ignored so pairs can blend.
No segfault to fear, no crash in sight,
Features unite in the soft twilight.
Hooray — a rabbit's patch, snug and right!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the segmentation fault fix for FeatureLinkerUnlabeledKD with charge_merging=Any, directly matching the main change.
Linked Issues check ✅ Passed The PR implements the fix for issue #8927 by setting ignore_charge=true in FeatureDistance when charge_merging=Any and adds regression tests to prevent segfaults.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the segfault issue: the algorithm fix, regression test configuration, and test fixture files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/feature-linker-kd-charge-any-segfault
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/openms/source/ANALYSIS/MAPMATCHING/FeatureGroupingAlgorithmKD.cpp`:
- Around line 132-137: When link:adduct_merging is "Any" the distance functor
must not reject adduct-mismatched pairs; update the same block that checks
param_.getValue("link:charge_merging") to also check
param_.getValue("link:adduct_merging") and call
distance_params.setValue("ignore_adduct", "true") when it equals "Any" (this
mirrors the existing ignore_charge behavior used with FeatureDistance and
prevents best_index from remaining max and being dereferenced); modify the code
near where distance_params is configured (the block that touches ignore_charge)
so both ignore_charge and ignore_adduct are set appropriately.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 55849e72-5787-4266-8ac2-319f75eb8295

📥 Commits

Reviewing files that changed from the base of the PR and between 0bcae36 and e69d0c3.

📒 Files selected for processing (1)
  • src/openms/source/ANALYSIS/MAPMATCHING/FeatureGroupingAlgorithmKD.cpp

Comment on lines +132 to +137
// charge_merging="Any" allows all charge states into the candidate pool,
// so FeatureDistance must not reject charge-mismatched pairs (segfault: #8927)
if (param_.getValue("link:charge_merging").toString() == "Any")
{
distance_params.setValue("ignore_charge", "true");
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Also set ignore_adduct for link:adduct_merging="Any" to avoid the same crash pattern.

The charge-side fix is correct, but the adduct-side mismatch remains: merge_adduct == "Any" keeps all candidates (Line 432), while FeatureDistance still rejects adduct-mismatched pairs with infinity when ignore_adduct is false (FeatureDistance.cpp:124-205). If a map contributes only such candidates, best_index can stay numeric_limits<Size>::max() (Line 453) and later be dereferenced.

💡 Proposed fix
@@
     if (param_.getValue("link:charge_merging").toString() == "Any")
     {
       distance_params.setValue("ignore_charge", "true");
     }
+    if (param_.getValue("link:adduct_merging").toString() == "Any")
+    {
+      distance_params.setValue("ignore_adduct", "true");
+    }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/openms/source/ANALYSIS/MAPMATCHING/FeatureGroupingAlgorithmKD.cpp` around
lines 132 - 137, When link:adduct_merging is "Any" the distance functor must not
reject adduct-mismatched pairs; update the same block that checks
param_.getValue("link:charge_merging") to also check
param_.getValue("link:adduct_merging") and call
distance_params.setValue("ignore_adduct", "true") when it equals "Any" (this
mirrors the existing ignore_charge behavior used with FeatureDistance and
prevents best_index from remaining max and being dereferenced); modify the code
near where distance_params is configured (the block that touches ignore_charge)
so both ignore_charge and ignore_adduct are set appropriately.

Two minimal featureXML inputs with different non-zero charges (2 vs 3)
at similar RT/mz. Without the fix, this segfaults because FeatureDistance
returns infinity for all candidates, leaving best_index at SIZE_MAX.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment on lines +132 to +136
// charge_merging="Any" allows all charge states into the candidate pool,
// so FeatureDistance must not reject charge-mismatched pairs (segfault: #8927)
if (param_.getValue("link:charge_merging").toString() == "Any")
{
distance_params.setValue("ignore_charge", "true");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while this limits the number of cases where a segfault occurs, it probably does not prevent all possible cases.

Should the full fix add a test for infinity, or if any features could be matched at all?! (not sure what the underlying actual segfaulting call is).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it segfaults if there are just different charges. e.g. f1 has z=2 and f2 has z=3
previously it got infinity because distance did not ignore charge

@timosachsenberg

Copy link
Copy Markdown
Contributor Author

will merge as it is definitely more correct now and I don't have time to investigate further

@timosachsenberg
timosachsenberg merged commit 9d15bee into develop Mar 20, 2026
41 checks passed
@timosachsenberg
timosachsenberg deleted the fix/feature-linker-kd-charge-any-segfault branch March 20, 2026 08:05
github-actions Bot pushed a commit that referenced this pull request Mar 21, 2026
Add missing changelog entries for:
- BREAKING: Qt string API removal from String/DataValue (#8940)
- BREAKING: Qt6::Core removed from libOpenMS PUBLIC CMake deps (#8965)
- BREAKING: Transitive includes trimmed from KERNEL/METADATA headers (#8946)
- Fix: segfault in FeatureLinkerUnlabeledKD with charge_merging=Any (#8944)

Co-authored-by: Copilot <[email protected]>
timosachsenberg pushed a commit that referenced this pull request Mar 21, 2026
Add missing changelog entries for:
- BREAKING: Qt string API removal from String/DataValue (#8940)
- BREAKING: Qt6::Core removed from libOpenMS PUBLIC CMake deps (#8965)
- BREAKING: Transitive includes trimmed from KERNEL/METADATA headers (#8946)
- Fix: segfault in FeatureLinkerUnlabeledKD with charge_merging=Any (#8944)

Co-authored-by: GitHub Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
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.

Segmentation fault when using FeatureLinkerUnlabeledKD with -algorithm:link:charge_merging=Any

2 participants