Fix MAJOR SonarQube bugs: mps_parser signed/unsigned compare and example init#1235
Fix MAJOR SonarQube bugs: mps_parser signed/unsigned compare and example init#1235rgsl888prabhu wants to merge 2 commits into
Conversation
…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>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR hardens MPS file handling across parser and example code. The C++ parser now uses safe integer comparison ( ChangesMPS Processing Improvements
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
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
📒 Files selected for processing (2)
cpp/src/io/mps_parser.cppdocs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c
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>
Fix two MAJOR SonarQube bugs on
main.cpp:S6214in the MPS parser:fread()(size_t) was compared to along bufsize. The earlierbufsize != -1Lguard only rules out the ftell error sentinel, not other negatives. Switched the equality tostd::cmp_equal(C++20).c:S836in the C MILP example:objective_valuewas read byprintfunconditionally despite being assigned only insideif (has_primal_solution). Moved that read — and the relatedsolution_values[i]access a few lines down, which had the same problem (latent NULL deref) — inside the guard.