mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
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 ✓
This commit is contained in:
parent
8103c117f1
commit
de76cf80d5
@ -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();
|
||||
|
||||
@ -46,8 +46,8 @@ buffer::buffer(void* p, std::size_t s)
|
||||
: buffer(p, s, nullptr) {
|
||||
}
|
||||
|
||||
buffer::buffer(char const & c)
|
||||
: buffer(const_cast<char*>(&c), 1) {
|
||||
buffer::buffer(char & c)
|
||||
: buffer(&c, 1) {
|
||||
}
|
||||
|
||||
buffer::buffer(buffer&& rhs)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user