From 7ccf6594ae3697d53e5afaf77e01a745a68b4dc5 Mon Sep 17 00:00:00 2001 From: Ramakrishnap <42624703+rgsl888prabhu@users.noreply.github.com> Date: Fri, 22 May 2026 03:19:51 +0530 Subject: [PATCH] Fix MAJOR SonarQube bugs: mps_parser signed/unsigned compare and example init (#1235) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. Authors: - Ramakrishnap (https://github.com/rgsl888prabhu) Approvers: - Trevor McKay (https://github.com/tmckayus) - Rajesh Gandham (https://github.com/rg20) URL: https://github.com/NVIDIA/cuopt/pull/1235 --- cpp/src/io/mps_parser.cpp | 1 + .../lp-qp-milp/examples/milp_mps_example.c | 31 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/cpp/src/io/mps_parser.cpp b/cpp/src/io/mps_parser.cpp index 31bc9455ef..535938b09c 100644 --- a/cpp/src/io/mps_parser.cpp +++ b/cpp/src/io/mps_parser.cpp @@ -20,6 +20,7 @@ #include #include #include +#include namespace { using cuopt::linear_programming::io::error_type_t; diff --git a/docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c b/docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c index c61a29bd95..82109492a7 100644 --- a/docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c +++ b/docs/cuopt/source/cuopt-c/lp-qp-milp/examples/milp_mps_example.c @@ -119,21 +119,28 @@ cuopt_int_t solve_mps_file(const char* filename) termination_status_to_string(termination_status), termination_status); printf("Solve time: %f seconds\n", time); - printf("Objective value: %f\n", objective_value); - // Get and print solution variables if (has_primal_solution) { - solution_values = (cuopt_float_t*)malloc(num_variables * sizeof(cuopt_float_t)); - status = cuOptGetPrimalSolution(solution, solution_values); - if (status != CUOPT_SUCCESS) { - printf("Error getting solution values: %d\n", status); - goto DONE; - } - } + printf("Objective value: %f\n", objective_value); + + // Get and print solution variables + solution_values = + (cuopt_float_t*)malloc((size_t)num_variables * sizeof(cuopt_float_t)); + if (solution_values == NULL) { + printf("Error allocating solution buffer\n"); + status = CUOPT_OUT_OF_MEMORY; + goto DONE; + } + status = cuOptGetPrimalSolution(solution, solution_values); + if (status != CUOPT_SUCCESS) { + printf("Error getting solution values: %d\n", status); + goto DONE; + } - printf("\nSolution: \n"); - for (cuopt_int_t i = 0; i < num_variables; i++) { - printf("x%d = %f\n", i + 1, solution_values[i]); + printf("\nSolution: \n"); + for (cuopt_int_t i = 0; i < num_variables; i++) { + printf("x%d = %f\n", i + 1, solution_values[i]); + } } DONE: