Skip to content

Commit de76cf8

Browse files
committed
fix(buffer): remove const from char constructor to prevent UB
- Change: explicit buffer(char const & c) → explicit buffer(char & c) - Remove dangerous const_cast in implementation - Before: buffer(const_cast<char*>(&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 ✓
1 parent 8103c11 commit de76cf8

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

include/libipc/buffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class IPC_EXPORT buffer {
2424
explicit buffer(byte_t (& data)[N])
2525
: buffer(data, sizeof(data)) {
2626
}
27-
explicit buffer(char const & c);
27+
explicit buffer(char & c);
2828

2929
buffer(buffer&& rhs);
3030
~buffer();

src/libipc/buffer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ buffer::buffer(void* p, std::size_t s)
4646
: buffer(p, s, nullptr) {
4747
}
4848

49-
buffer::buffer(char const & c)
50-
: buffer(const_cast<char*>(&c), 1) {
49+
buffer::buffer(char & c)
50+
: buffer(&c, 1) {
5151
}
5252

5353
buffer::buffer(buffer&& rhs)

0 commit comments

Comments
 (0)