From f09b4b1d00ae5d64cb27446d5c4528f93a1d6dd0 Mon Sep 17 00:00:00 2001 From: Robin Getz Date: Sun, 6 Sep 2026 00:51:32 -0400 Subject: [PATCH] ft4/ft8: Prevent negative container indexing Shift the sentinel value from the index (-1) to the best score (positive infinity). This ensures the index is always a valid container index, so there is no possibility of later accessing data using an index of -1. Reported by cppcheck. Signed-off-by: Robin Getz --- ft8/ft4.cpp | 13 ++++++------- ft8/ft8.cpp | 15 +++++++-------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/ft8/ft4.cpp b/ft8/ft4.cpp index 880b8369b..08c952eff 100644 --- a/ft8/ft4.cpp +++ b/ft8/ft4.cpp @@ -1495,15 +1495,14 @@ std::vector> FT4::soft_c2m(const FFTEngine::ffts_t &c103) con // choose the phase that has the lowest total distance to other // phases. like median but avoids -pi..pi wrap-around. - int n = v.size(); - int best = -1; - float best_score = 0; - for (int i = 0; i < n; i++) - + const size_t n = v.size(); + size_t best = 0; + float best_score = std::numeric_limits::infinity(); + for (size_t i = 0; i < n; i++) { float score = 0; - for (int j = 0; j < n; j++) + for (size_t j = 0; j < n; j++) { if (i == j) { continue; @@ -1518,7 +1517,7 @@ std::vector> FT4::soft_c2m(const FFTEngine::ffts_t &c103) con score += d; } - if (best == -1 || score < best_score) + if (score < best_score) { best = i; best_score = score; diff --git a/ft8/ft8.cpp b/ft8/ft8.cpp index d08d72696..28d91b273 100644 --- a/ft8/ft8.cpp +++ b/ft8/ft8.cpp @@ -1562,7 +1562,7 @@ std::vector> FT8::soft_c2m(const FFTEngine::ffts_t &c79) cons { m79[si].resize(8); float mx = -std::numeric_limits::infinity(); - float mx_phase; + float mx_phase = 0; for (int bi = 0; bi < 8; bi++) { @@ -1602,15 +1602,14 @@ std::vector> FT8::soft_c2m(const FFTEngine::ffts_t &c79) cons // choose the phase that has the lowest total distance to other // phases. like median but avoids -pi..pi wrap-around. - int n = v.size(); - int best = -1; - float best_score = 0; - for (int i = 0; i < n; i++) - + const size_t n = v.size(); + size_t best = 0; + float best_score = std::numeric_limits::infinity(); + for (size_t i = 0; i < n; i++) { float score = 0; - for (int j = 0; j < n; j++) + for (size_t j = 0; j < n; j++) { if (i == j) { continue; @@ -1625,7 +1624,7 @@ std::vector> FT8::soft_c2m(const FFTEngine::ffts_t &c79) cons score += d; } - if (best == -1 || score < best_score) + if (score < best_score) { best = i; best_score = score;