From 4c215aa5bbbdb50b644d469b5dd533f4a2f1f537 Mon Sep 17 00:00:00 2001 From: bicarus-dev <202771338+bicarus-dev@users.noreply.github.com> Date: Sat, 15 Mar 2025 22:54:13 -0700 Subject: [PATCH] Fix last character being truncated in clipboard::copy_text due to off-by-one error (#262) ## Link to GitHub Issue, if one exists n/a, user reported ## Description of change When copying text to clipboard, the last character was being dropped due to incorrect string buffer size calculation. Also, clean up callers to just pass std:string by reference instead of going back and forth between C-style strings. Lastly, fix `git archive` in `build_all.sh` which broke after github migration. ## Compiling Yes ## Testing Tested all three callers - IIDX cam hook, scard reader in Cards tab, and patch manager datecode. --- src/spice2x/build_all.sh | 2 +- src/spice2x/misc/clipboard.cpp | 6 +++--- src/spice2x/misc/clipboard.h | 2 +- src/spice2x/overlay/windows/camera_control.cpp | 2 +- src/spice2x/overlay/windows/config.cpp | 2 +- src/spice2x/overlay/windows/patch_manager.cpp | 2 +- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/spice2x/build_all.sh b/src/spice2x/build_all.sh index a4169d6..eb0c915 100644 --- a/src/spice2x/build_all.sh +++ b/src/spice2x/build_all.sh @@ -189,7 +189,7 @@ mkdir -p ${OUTDIR}/src if ((INCLUDE_SRC > 0)) then echo "Generating source file archive..." - git archive --format tar.gz --prefix=spice2x/ HEAD > ${OUTDIR}/src/spice2x-${GIT_BRANCH}.tar.gz 2>/dev/null || \ + git archive --format tar.gz --prefix=spice2x/ HEAD ./ > ${OUTDIR}/src/spice2x-${GIT_BRANCH}.tar.gz 2>/dev/null || \ echo "WARNING: Couldn't get git to create the archive. Is this a git repository?" fi diff --git a/src/spice2x/misc/clipboard.cpp b/src/spice2x/misc/clipboard.cpp index 4f264dd..bd08573 100644 --- a/src/spice2x/misc/clipboard.cpp +++ b/src/spice2x/misc/clipboard.cpp @@ -142,14 +142,14 @@ namespace clipboard { handle.detach(); } - void copy_text(const std::string str) { + void copy_text(const std::string& str) { if (!OpenClipboard(nullptr)) { log_warning("clipboard", "Failed to open clipboard"); return; } - HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, str.length()); - memcpy(GlobalLock(mem), str.c_str(), str.length()); + HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, str.length() + 1); + memcpy(GlobalLock(mem), str.c_str(), str.length() + 1); GlobalUnlock(mem); EmptyClipboard(); diff --git a/src/spice2x/misc/clipboard.h b/src/spice2x/misc/clipboard.h index 5e38fd3..96a9d60 100644 --- a/src/spice2x/misc/clipboard.h +++ b/src/spice2x/misc/clipboard.h @@ -4,6 +4,6 @@ namespace clipboard { void copy_image(const std::filesystem::path path); - void copy_text(const std::string str); + void copy_text(const std::string& str); const std::string paste_text(); } diff --git a/src/spice2x/overlay/windows/camera_control.cpp b/src/spice2x/overlay/windows/camera_control.cpp index 69fcd1e..fef3b46 100644 --- a/src/spice2x/overlay/windows/camera_control.cpp +++ b/src/spice2x/overlay/windows/camera_control.cpp @@ -74,7 +74,7 @@ namespace overlay::windows { ImGui::SameLine(); if (ImGui::Button("Copy")) { const auto s = selectedCamera->GetFriendlyName() + "\n" + selectedCamera->GetSymLink(); - clipboard::copy_text(s.c_str()); + clipboard::copy_text(s); } // Render parameters diff --git a/src/spice2x/overlay/windows/config.cpp b/src/spice2x/overlay/windows/config.cpp index 54fec5e..948f474 100644 --- a/src/spice2x/overlay/windows/config.cpp +++ b/src/spice2x/overlay/windows/config.cpp @@ -2524,7 +2524,7 @@ namespace overlay::windows { ); ImGui::SameLine(); if (ImGui::Button("Copy")) { - clipboard::copy_text(card_str.c_str()); + clipboard::copy_text(card_str); } ImGui::SameLine(); if (ImGui::Button("Clear")) { diff --git a/src/spice2x/overlay/windows/patch_manager.cpp b/src/spice2x/overlay/windows/patch_manager.cpp index 949ae25..35391f1 100644 --- a/src/spice2x/overlay/windows/patch_manager.cpp +++ b/src/spice2x/overlay/windows/patch_manager.cpp @@ -295,7 +295,7 @@ namespace overlay::windows { ImGui::Text("%s", avs::game::get_identifier().c_str()); ImGui::SameLine(); if (ImGui::Button("Copy")) { - clipboard::copy_text(identifiers.c_str()); + clipboard::copy_text(identifiers); } ImGui::AlignTextToFramePadding();