Skip to content

fix(SwathMapMassCorrection): remove leaked *new(SpectrumPtr) fallback - #9386

Merged
timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:fix/swath-mass-correction-spectrumptr-leak
May 20, 2026
Merged

timosachsenberg merged 1 commit into
OpenMS:developfrom
timosachsenberg:fix/swath-mass-correction-spectrumptr-leak

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • *new(OpenSwath::SpectrumPtr) in SwathMapMassCorrection.cpp heap-allocated a default-constructed std::shared_ptr<Spectrum>, dereferenced the raw pointer, copied the null shared_ptr, and never deleted the heap object — a small leak per empty-fetch fallback in a per-peptide loop.
  • The same construct also yielded a null shared_ptr that the downstream DIAHelpers::integrateWindow / sp->getDriftTimeArray() calls would dereference (precondition violation in debug, null deref in release). The fallback wasn't actually safe — it just deferred the crash.
  • Three call sites in this file were affected (introduced in Refactor/osw calibration #8743, Dec 2023). Note that the MS1 branch added later (line 578) already used the correct OpenSwath::SpectrumPtr() idiom — this PR brings the older sites in line.

Changes

  • correctMZ: when fetchSpectrumSwath returns empty, continue to the next transition group — mirrors the existing used_maps.empty() guard a few lines above.
  • correctIM: replace *new(OpenSwath::SpectrumPtr) with a default-constructed OpenSwath::SpectrumPtr() for both sp_ms1 and sp_ms2, and extend the two existing getDriftTimeArray() == nullptr guards to also check for a null shared_ptr.

Test plan

  • cmake --build OpenMS-build --target SwathMapMassCorrection_test — builds clean
  • ctest --test-dir OpenMS-build -R SwathMapMassCorrection_test — passes (0.40s)
  • CI green on full OpenSWATH test suite

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed incorrect handling of missing spectrum data in SWATH-MS analysis operations, improving stability during spectrum processing.

Review Change Stack

The `*new(OpenSwath::SpectrumPtr)` fallback heap-allocated a default-
constructed shared_ptr, dereferenced it, and never freed it — leaking
one shared_ptr per empty-fetch fallback and producing a null pointer
that downstream `integrateWindow` / `getDriftTimeArray()` would
dereference (precondition violation in debug, crash in release).

* correctMZ: skip the iteration via `continue` when fetchSpectrumSwath
  returns empty — mirrors the existing `used_maps.empty()` guard above.
* correctIM: replace the fallback with `OpenSwath::SpectrumPtr()` and
  null-check sp_ms1 / sp_ms2 alongside the existing getDriftTimeArray()
  checks.

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

coderabbitai Bot commented May 20, 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: 14089973-c9dc-4847-9347-fb7e51e23778

📥 Commits

Reviewing files that changed from the base of the PR and between d9875ca and 6b94293.

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

📝 Walkthrough

Walkthrough

SwathMapMassCorrection.cpp now safely handles cases where spectrum fetches return empty results. The correctIM method assigns empty SpectrumPtr() objects instead of dereferencing newly allocated pointers, and adds explicit null checks before accessing drift-time arrays. The correctMZ method skips transition groups with empty spectrum arrays and assigns spectrum references directly.

Changes

Spectrum Null/Empty Safety Improvements

Layer / File(s) Summary
correctIM null-safe spectrum handling
src/openms/source/ANALYSIS/OPENSWATH/SwathMapMassCorrection.cpp
Spectrum fetch results use empty SpectrumPtr() when unavailable, with added null checks for sp_ms2 and sp_ms1 before calling getDriftTimeArray() to prevent unsafe dereference.
correctMZ empty spectrum array handling
src/openms/source/ANALYSIS/OPENSWATH/SwathMapMassCorrection.cpp
Empty results from fetchSpectrumSwath are skipped via early-continue, and sp is assigned directly from spArr[0] without unsafe pointer construction fallback.

Possibly Related PRs

  • OpenMS/OpenMS#8308: Adjusts SwathMapMassCorrection.cpp to handle missing MS1-related data paths, overlapping in empty spectrum handling within the same correction logic.

Suggested Reviewers

  • jcharkow
  • cbielow

Poem

🐰 A spectrum's dance, now safe and sound,
Empty results no longer crash our ground,
Null checks bloom where pointers used to loom,
Swift corrections chase away the gloom! ✨

🚥 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 clearly and concisely describes the main fix: removing a memory leak caused by the *new(SpectrumPtr) fallback pattern in SwathMapMassCorrection.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@timosachsenberg
timosachsenberg merged commit 4834d21 into OpenMS:develop May 20, 2026
41 checks passed
timosachsenberg pushed a commit that referenced this pull request May 21, 2026
- FeatureFinderMetaboIdent: adduct support via optional TSV column (#9382)
- BrukerTimsImagingFile: new Bruker .d MALDI imaging loader (#9383)
- BrukerTimsFile: frame_id/RT range filter in Config (#9387)
- Fix: SwathMapMassCorrection null-pointer and memory leak (#9386)

Co-authored-by: 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.

1 participant