Skip to content

Fix MAJOR SonarQube bugs: mps_parser signed/unsigned compare and example init#1235

Merged
rapids-bot[bot] merged 3 commits into
release/26.06from
fix/sonar-bugs-mps-parser-and-example-init
May 21, 2026
Merged

Fix MAJOR SonarQube bugs: mps_parser signed/unsigned compare and example init#1235
rapids-bot[bot] merged 3 commits into
release/26.06from
fix/sonar-bugs-mps-parser-and-example-init

Conversation

@rgsl888prabhu
Copy link
Copy Markdown
Collaborator

@rgsl888prabhu rgsl888prabhu commented May 18, 2026

Fix two MAJOR SonarQube bugs on main.

  • cpp:S6214 in the MPS parser: fread() (size_t) was compared to a long bufsize. The earlier bufsize != -1L guard only rules out the ftell error sentinel, not other negatives. Switched the equality to std::cmp_equal (C++20).
  • c:S836 in the C MILP example: objective_value was read by printf unconditionally despite being assigned only inside if (has_primal_solution). Moved that read — and the related solution_values[i] access a few lines down, which had the same problem (latent NULL deref) — inside the guard.

…ple var

mps_parser.cpp (S6214):
  fread() returns size_t; bufsize is long. The earlier
  `mps_parser_expects(bufsize != -1L, ...)` rules out the ftell error
  sentinel but not other negative values, so `fread(...) == bufsize`
  is a signed/unsigned comparison. Switch to `std::cmp_equal` (C++20)
  so the comparison is correct across both signs.

milp_mps_example.c (S836, plus a related NULL-deref):
  `objective_value` was read unconditionally even though it is only
  assigned when `has_primal_solution` is true — undefined behavior
  on INFEASIBLE/ITERATION_LIMIT terminations. The same applies to
  `solution_values[i]` further down: malloc'd inside the if-block but
  indexed unconditionally (NULL deref). Consolidate both reads inside
  the `if (has_primal_solution)` block so the example is well-defined
  for all termination statuses.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@rgsl888prabhu rgsl888prabhu requested review from a team as code owners May 18, 2026 19:03
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 18, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 64af66d5-d0fd-4ecf-84be-3c475fb2b837

📥 Commits

Reviewing files that changed from the base of the PR and between 4f5ea40 and 1a18e19.

📒 Files selected for processing (1)
  • cpp/src/io/mps_parser.cpp

📝 Walkthrough

Walkthrough

Adds a missing <utility> include to the C++ MPS parser and consolidates objective and primal-solution retrieval/printing inside a has_primal_solution check in the MPS example.

Changes

MPS Processing Updates

Layer / File(s) Summary
Parser include addition
cpp/src/io/mps_parser.cpp
Adds the <utility> header to the translation unit.
Example solution output gating
docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c
Objective value printing, allocation, cuOptGetPrimalSolution call and per-variable printing are all executed only when has_primal_solution is true.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • NVIDIA/cuopt#1193: Also modifies cpp/src/io/mps_parser.cpp with changes to the MPS parser implementation.

Suggested labels

bug

Suggested reviewers

  • mlubin
  • Iroy30
🚥 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
Title check ✅ Passed The title accurately summarizes the main changes: fixing two MAJOR SonarQube bugs in the MPS parser (signed/unsigned comparison) and the C example (uninitialized variable usage).
Description check ✅ Passed The description is directly related to the changeset, detailing the two SonarQube bugs being fixed with technical specifics about the problems and solutions implemented.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/sonar-bugs-mps-parser-and-example-init

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

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

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 `@docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c`:
- Around line 127-133: The allocation of solution_values must be checked for
NULL before calling cuOptGetPrimalSolution: after the malloc for solution_values
check if solution_values == NULL, set status to an appropriate memory error
(e.g., CUOPT_MEMORY_ERROR or the project's equivalent), print an error message
indicating allocation failure, and goto DONE; only call
cuOptGetPrimalSolution(solution, solution_values) when the allocation succeeded.
Ensure the change is applied near the existing allocation for solution_values so
the new NULL-check mirrors the other allocation checks in this file.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: a81dd244-99ec-4d16-b0ee-b25e3e766b01

📥 Commits

Reviewing files that changed from the base of the PR and between 6f99a42 and 9ee09c1.

📒 Files selected for processing (2)
  • cpp/src/io/mps_parser.cpp
  • docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c

Comment thread docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c
@rgsl888prabhu rgsl888prabhu self-assigned this May 18, 2026
@rgsl888prabhu rgsl888prabhu added non-breaking Introduces a non-breaking change improvement Improves an existing functionality labels May 18, 2026
@rgsl888prabhu rgsl888prabhu requested review from chris-maes and mlubin and removed request for nguidotti and yuwenchen95 May 18, 2026 19:17
Mirrors the error-checking pattern used elsewhere in this example.
size_t cast on the malloc size avoids signed multiplication overflow.

Thanks to CodeRabbit for the catch.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@mlubin
Copy link
Copy Markdown
Contributor

mlubin commented May 19, 2026

Could we land this after #1120 to reduce conflicts?

@rgsl888prabhu
Copy link
Copy Markdown
Collaborator Author

Yep, we can follow-up.

@rgsl888prabhu rgsl888prabhu added the do not merge Do not merge if this flag is set label May 19, 2026
@rgsl888prabhu rgsl888prabhu changed the base branch from main to release/26.06 May 20, 2026 17:29
…le-init

Conflict in cpp/src/io/mps_parser.cpp resolved by accepting release/26.06's
relocation: file_to_string was moved into cpp/src/io/file_to_string.cpp by
PR #1120 (LP-format reader), where it already carries an equivalent
SonarQube cpp:S6214 fix via static_cast<size_t>(bufsize). Dropped the
duplicate definition from mps_parser.cpp; left file_to_string.cpp untouched.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@rgsl888prabhu rgsl888prabhu removed the do not merge Do not merge if this flag is set label May 21, 2026
@rgsl888prabhu
Copy link
Copy Markdown
Collaborator Author

/merge

@rapids-bot rapids-bot Bot merged commit 7ccf659 into release/26.06 May 21, 2026
192 of 194 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

improvement Improves an existing functionality non-breaking Introduces a non-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants