From 4d2cc3649c060f1d4a8bc39b300d92f6668bdb71 Mon Sep 17 00:00:00 2001 From: indigodarkwolf Date: Thu, 28 Dec 2023 18:09:05 -0600 Subject: [PATCH] Fixing some unsafe snprintf usage. --- src/ieee.cpp | 3 +-- src/overlay/overlay.cpp | 4 ++++ src/overlay/psg_overlay.cpp | 1 + 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ieee.cpp b/src/ieee.cpp index 1f867ef..670b248 100644 --- a/src/ieee.cpp +++ b/src/ieee.cpp @@ -678,8 +678,7 @@ static void set_activity(bool active) static void set_error(int e, int t, int s) { - snprintf(error, sizeof(error), "%02x,%s,%02d,%02d\r", e, error_string(e), t, s); - error_len = static_cast(strlen(error)); + error_len = snprintf(error, sizeof(error), "%02x,%s,%02d,%02d\r", e, error_string(e), t, s); error_pos = 0; uint8_t cbdos_flags = get_kernal_cbdos_flags(); if (e < 0x10 || e == 0x73) { diff --git a/src/overlay/overlay.cpp b/src/overlay/overlay.cpp index ce2b75a..2b32d09 100644 --- a/src/overlay/overlay.cpp +++ b/src/overlay/overlay.cpp @@ -206,11 +206,13 @@ static void draw_debugger_cpu_status() ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled)); char stack_line[256]; snprintf(stack_line, sizeof(stack_line), "$%02X:$%04X", bank, pc); + stack_line[255] = '\0'; pushed = ImGui::Selectable(stack_line, false, 0, ImGui::CalcTextSize(stack_line)); ImGui::PopStyleColor(); } else { char stack_line[256]; snprintf(stack_line, sizeof(stack_line), "$%02X:$%04X: %s", bank, pc, label); + stack_line[255] = '\0'; pushed = ImGui::Selectable(stack_line, false, 0, ImGui::CalcTextSize(stack_line)); } } else { @@ -218,11 +220,13 @@ static void draw_debugger_cpu_status() ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled)); char stack_line[256]; snprintf(stack_line, sizeof(stack_line), "$%04X", pc); + stack_line[255] = '\0'; pushed = ImGui::Selectable(stack_line, false, 0, ImGui::CalcTextSize(stack_line)); ImGui::PopStyleColor(); } else { char stack_line[256]; snprintf(stack_line, sizeof(stack_line), "$%04X: %s", pc, label); + stack_line[255] = '\0'; pushed = ImGui::Selectable(stack_line, false, 0, ImGui::CalcTextSize(stack_line)); } } diff --git a/src/overlay/psg_overlay.cpp b/src/overlay/psg_overlay.cpp index a07364b..b0369eb 100644 --- a/src/overlay/psg_overlay.cpp +++ b/src/overlay/psg_overlay.cpp @@ -314,6 +314,7 @@ void draw_debugger_vera_psg() char rate_txt[15]; float rate_hz = rate <= 128 ? (float)SAMPLERATE * rate / 128 : 0; snprintf(rate_txt, 15, "%d (%.0f Hz)", rate, rate_hz); + rate_txt[14] = '\0'; ImGui::SetNextItemWidth(avail / 2 - 48); if (ImGui::SliderInt("Rate", &rate_i, 0, 128, rate_txt, ImGuiSliderFlags_AlwaysClamp)) { pcm_write_rate(rate_i);