From de76cf80d5fb162cac104e507afc541028a9a696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E5=A4=B4=E4=BA=91?= Date: Sun, 30 Nov 2025 05:00:57 +0000 Subject: [PATCH] fix(buffer): remove const from char constructor to prevent UB MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Change: explicit buffer(char const & c) → explicit buffer(char & c) - Remove dangerous const_cast in implementation - Before: buffer(const_cast(&c), 1) [UB if c is truly const] - After: buffer(&c, 1) [safe, requires non-const char] - Prevents undefined behavior from modifying compile-time constants - Constructor now correctly requires mutable char reference - Aligns with buffer's mutable data semantics The previous implementation with const_cast could lead to: - Modifying string literals (undefined behavior) - Modifying const variables (undefined behavior) - Runtime crashes or data corruption Example of prevented misuse: buffer buf('X'); // Now: compile error ✓ char c = 'X'; buffer buf(c); // Now: works correctly ✓ const char cc = 'Y'; buffer buf(cc); // Now: compile error ✓ --- include/libipc/buffer.h | 2 +- src/libipc/buffer.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/libipc/buffer.h b/include/libipc/buffer.h index 9d787d5..51ae59b 100755 --- a/include/libipc/buffer.h +++ b/include/libipc/buffer.h @@ -24,7 +24,7 @@ public: explicit buffer(byte_t (& data)[N]) : buffer(data, sizeof(data)) { } - explicit buffer(char const & c); + explicit buffer(char & c); buffer(buffer&& rhs); ~buffer(); diff --git a/src/libipc/buffer.cpp b/src/libipc/buffer.cpp index 084b815..78ba8a1 100755 --- a/src/libipc/buffer.cpp +++ b/src/libipc/buffer.cpp @@ -46,8 +46,8 @@ buffer::buffer(void* p, std::size_t s) : buffer(p, s, nullptr) { } -buffer::buffer(char const & c) - : buffer(const_cast(&c), 1) { +buffer::buffer(char & c) + : buffer(&c, 1) { } buffer::buffer(buffer&& rhs)