Skip to content

Commit e6f390e

Browse files
committed
Add launder for static array range assertions in high version GCC
1 parent 334bcb3 commit e6f390e

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

ecosystem/redis.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,16 @@ class _RedisClient {
183183
size_t _sum(T x, const Ts&...xs) {
184184
return x + _sum(xs...);
185185
}
186-
char* ibuf() { return _xbuf; }
187-
char* obuf() { return _xbuf + _bufsize; }
186+
// _xbuf is a GCC zero-length flex-array; the actual storage lives in the
187+
// derived class __RedisClient<BUF_SIZE>::_buf. Under -D_FORTIFY_SOURCE>=1,
188+
// __builtin_object_size(_xbuf, *) yields 0, causing libc fortified wrappers
189+
// (__snprintf_chk / __memcpy_chk / __memmove_chk) to falsely report
190+
// buffer overflow. Launder the returned pointer through an inline-asm
191+
// "+r" constraint so the optimizer/FORTIFY can no longer associate it
192+
// with the original [0]-sized array, eliminating both the compile-time
193+
// -Wstringop-overflow warning and the runtime buffer-overflow abort.
194+
char* ibuf() { char* p = _xbuf; __asm__("" : "+r"(p)); return p; }
195+
char* obuf() { char* p = _xbuf + _bufsize; __asm__("" : "+r"(p)); return p; }
188196
std::string_view __getstring(size_t length);
189197
std::string_view __getline();
190198

0 commit comments

Comments
 (0)