Skip to content

Guard degenerate input in DistanceMatrix::operator==, ClusteringGrid::getClusters, File path helpers - #9497

Merged
timosachsenberg merged 3 commits into
OpenMS:developfrom
timosachsenberg:pols-fix-guards-data-ml-sys
Jun 18, 2026
Merged

timosachsenberg merged 3 commits into
OpenMS:developfrom
timosachsenberg:pols-fix-guards-data-ml-sys

Conversation

@timosachsenberg

@timosachsenberg timosachsenberg commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

POLS audit fixes (#9488). Each only changes a previously-undefined path; valid input is unchanged, no reference files touched.

  • DistanceMatrix::operator== — guarded only by OPENMS_PRECONDITION (a no-op in release), then iterated rhs's dimensions while indexing *this → heap OOB read for a smaller left-hand matrix (debug: a throw). Now returns false for differently-sized matrices.
  • ClusteringGrid::getClusters — cells_.find(idx)->second with no end() check → dereferenced map::end() (UB) for a never-populated cell. Now returns an empty list.
  • File::getPathLocations / executableExtensions_ — the default argument constructed a std::string directly from std::getenv("PATH"/"PATHEXT"), which is UB when the variable is unset (getenv returns nullptr). The default is now "" and the function reads the env var internally with a null guard.

Regression tests added (different-size matrices compare unequal; unknown grid cell → empty); all three suites pass locally.

Part of #9488. ABI-safe.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added no-argument helpers for retrieving PATH locations and executable extensions (Windows).
  • Bug Fixes
    • Distance matrix equality comparisons now safely handle matrices of different sizes.
    • Clustering grid operations no longer crash when accessing non-existent cells, returning empty results instead.
    • Improved PATH/PATHEXT handling when environment values are missing.
  • Tests
    • Expanded coverage for distance matrix comparison and clustering grid edge cases, plus PATH-related behavior.

@github-actions

Copy link
Copy Markdown
Contributor

@timosachsenberg The following names might be missing from the AUTHORS file:

  • Timo Sachsenberg

Please consider applying the following patch:

--- AUTHORS	2026-06-12 13:37:08.508136702 +0000
+++ AUTHORS.new	2026-06-12 13:37:13.587384792 +0000
@@ -22,8 +22,8 @@
  - Chirag Dave
  - Chris Bauer
  - Chris Bielow
- - Christie Mathews
  - Christian Ehrlich
+ - Christie Mathews
  - Clemens Groepl
  - Cornelia Friedle
  - Daniel Jameson

@coderabbitai

coderabbitai Bot commented Jun 12, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

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: 213b2098-3a06-40c4-8d57-7ff20d11cd2a

📥 Commits

Reviewing files that changed from the base of the PR and between e4dadea and b0581bc.

📒 Files selected for processing (7)
  • src/openms/include/OpenMS/DATASTRUCTURES/DistanceMatrix.h
  • src/openms/include/OpenMS/SYSTEM/File.h
  • src/openms/source/ML/CLUSTERING/ClusteringGrid.cpp
  • src/openms/source/SYSTEM/File.cpp
  • src/tests/class_tests/openms/source/ClusteringGrid_test.cpp
  • src/tests/class_tests/openms/source/DistanceMatrix_test.cpp
  • src/tests/class_tests/openms/source/File_test.cpp
🚧 Files skipped from review as they are similar to previous changes (5)
  • src/openms/include/OpenMS/DATASTRUCTURES/DistanceMatrix.h
  • src/tests/class_tests/openms/source/ClusteringGrid_test.cpp
  • src/openms/source/ML/CLUSTERING/ClusteringGrid.cpp
  • src/openms/source/SYSTEM/File.cpp
  • src/tests/class_tests/openms/source/DistanceMatrix_test.cpp

📝 Walkthrough

Walkthrough

This PR introduces three independent defensive programming improvements: DistanceMatrix equality comparison safely handles size mismatches, ClusteringGrid::getClusters safely handles missing cells, and File path utilities defensively guard against null environment variables.

Changes

Safety and Robustness Fixes

Layer / File(s) Summary
DistanceMatrix equality size safety
src/openms/include/OpenMS/DATASTRUCTURES/DistanceMatrix.h, src/tests/class_tests/openms/source/DistanceMatrix_test.cpp
operator== now returns false immediately for matrices with different dimensionsize_ values, replacing prior size enforcement. Equal-sized comparison loop unchanged. Test coverage extended to verify cross-size comparisons return false for both operand orders.
ClusteringGrid getClusters cell existence safety
src/openms/source/ML/CLUSTERING/ClusteringGrid.cpp, src/tests/class_tests/openms/source/ClusteringGrid_test.cpp
getClusters now checks that cell_index exists in cells_ before dereferencing; returns empty std::list<int> if missing. Regression test added to verify empty-cell queries return an empty list.
File path environment variable defensive handling
src/openms/include/OpenMS/SYSTEM/File.h, src/openms/source/SYSTEM/File.cpp, src/tests/class_tests/openms/source/File_test.cpp
getPathLocations and executableExtensions_ refactored from single functions with environment variable default arguments to overload sets. Parameterless overloads safely read PATH and PATHEXT via std::getenv with nullptr guards; explicit parameter versions avoid environment lookups. Documentation and test coverage updated to reflect new overload behavior.

Possibly related PRs

  • OpenMS/OpenMS#8305: Adds dimension validation to Matrix::operator== to avoid Eigen3 assertions on mismatched sizes, following the same defensive pattern applied here to DistanceMatrix::operator==.

🐰 Three guards stand strong and tall,
When cells are missing, they don't fall,
Environment's whispers checked with care,
Safe comparisons everywhere!
hop hop

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% 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 specifically summarizes the main changes across three components: DistanceMatrix equality check, ClusteringGrid cell retrieval, and File path helpers, all focused on guarding against degenerate inputs.
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.

@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 current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/tests/class_tests/openms/source/DistanceMatrix_test.cpp`:
- Around line 219-223: The new test block uses tab characters for indentation;
replace those tabs with 2-space indentation in the block containing
DistanceMatrix<double> dm_small(3, 1.0); and the two TEST_EQUAL assertions
(dm==dm_small and dm_small==dm) so the changed C++ code follows the project's
no-tabs / 2-space indentation rule.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fbeab895-c7d2-489c-9efc-7f3d1531e781

📥 Commits

Reviewing files that changed from the base of the PR and between 28de924 and e4dadea.

📒 Files selected for processing (6)
  • src/openms/include/OpenMS/DATASTRUCTURES/DistanceMatrix.h
  • src/openms/include/OpenMS/SYSTEM/File.h
  • src/openms/source/ML/CLUSTERING/ClusteringGrid.cpp
  • src/openms/source/SYSTEM/File.cpp
  • src/tests/class_tests/openms/source/ClusteringGrid_test.cpp
  • src/tests/class_tests/openms/source/DistanceMatrix_test.cpp

timosachsenberg and others added 2 commits June 15, 2026 10:05
…:getClusters, File path helpers

POLS audit (OpenMS#9488). Each only changes a previously-undefined path; valid input unchanged.

- DistanceMatrix::operator==: guarded only by OPENMS_PRECONDITION (no-op in release), then
  iterated rhs's dimensions while indexing *this -> heap OOB read for a smaller left-hand
  matrix (or a debug throw). Now returns false for differently-sized matrices.

- ClusteringGrid::getClusters: did cells_.find(idx)->second with no end() check ->
  dereferenced map::end() (UB) for a never-populated cell. Now returns an empty list.

- File::getPathLocations / executableExtensions_: the default argument constructed a
  std::string directly from std::getenv("PATH"/"PATHEXT"), which is undefined behavior when
  the variable is unset (getenv returns nullptr). The default is now "" and the function
  reads the environment variable internally with a null guard.

Regression tests added for DistanceMatrix (different sizes compare unequal) and
ClusteringGrid (unknown cell -> empty); all three suites pass.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ART_SECTION default arg

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@timosachsenberg
timosachsenberg force-pushed the pols-fix-guards-data-ml-sys branch from 97d9890 to 1a29e6c Compare June 15, 2026 08:09

Copy link
Copy Markdown
Contributor Author

Adversarial review result: I did not find a blocking issue in this change. The File::getPathLocations() split into an environment-reading overload plus explicit-string overload preserves testability while avoiding construction from a null environment pointer, and the ClusteringGrid missing-cell guard is narrowly scoped.

No whitespace errors or source/test comments with tracker-style issue references stood out in this diff.

I did not build or run the tests locally.

@timosachsenberg
timosachsenberg merged commit 3a374f1 into OpenMS:develop Jun 18, 2026
38 checks passed
timosachsenberg added a commit that referenced this pull request Jun 19, 2026
Add entries for all substantive non-test PRs merged since the last
changelog update:

- #9623 ThermoRawFile full mzML metadata extraction + polarity fix
- #9602 New FileInfo library class usable from C++ and pyOpenMS
- #9521 IMAGING multi-region support (add missing PR reference)
- #9620 Deisotoper::isToleranceSupported + ProSE/SSE crash fix on low-res data
- #9610 DistanceMatrix::setValue cached-minimum guard fix
- #9497 DistanceMatrix::operator==, ClusteringGrid, File path guard fixes
- #9583 LogStreamBuf thread-safety fix
- #9514 CI Windows migration to Visual Studio 2026


Claude-Session: https://claude.ai/code/session_01YLg6pWyjyTxpWAU6mgrDzC

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