Skip to content

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

Open
rgsl888prabhu wants to merge 2 commits into
mainfrom
fix/sonar-bugs-mps-parser-and-example-init
Open

Fix MAJOR SonarQube bugs: mps_parser signed/unsigned compare and example init#1235
rgsl888prabhu wants to merge 2 commits into
mainfrom
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: 488b0153-fc00-4e48-b709-a816b6aaf166

📥 Commits

Reviewing files that changed from the base of the PR and between 9ee09c1 and 4f5ea40.

📒 Files selected for processing (1)
  • docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c

📝 Walkthrough

Walkthrough

This PR hardens MPS file handling across parser and example code. The C++ parser now uses safe integer comparison (std::cmp_equal) when validating fread byte counts, and the example code ensures solution output (objective value, allocation, retrieval, and per-variable printing) occurs only when a primal solution is available.

Changes

MPS Processing Improvements

Layer / File(s) Summary
Parser I/O safe integer comparison
cpp/src/io/mps_parser.cpp
<utility> header added; fread validation check replaced with std::cmp_equal(fread(...), bufsize) for safer type comparison.
Example solution output conditional
docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c
Objective value print, solution allocation, cuOptGetPrimalSolution call, error handling, and per-variable output loop all consolidated inside has_primal_solution condition.

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

non-breaking, improvement

Suggested reviewers

  • aliceb-nv
  • tmckayus
🚥 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 clearly summarizes the main changes: fixing two SonarQube bugs in the mps_parser and C example files, which aligns with the changeset.
Description check ✅ Passed The description is directly related to the changeset, detailing the specific SonarQube bugs fixed and the technical rationale for the changes made.
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>
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.

1 participant