Skip to content

Commit 31ba3f4

Browse files
mrc0mmandbluca
authored andcommitted
macro: terminate the temporary VA_ARGS_FOREACH() array with a sentinel
So gcc-14 doesn't complain we're out of bounds on the last iteration: [2092/2414] Compiling C object test-macro.p/src_test_test-macro.c.o In file included from ../src/basic/list.h:209, from ../src/basic/log.h:10, from ../src/test/test-macro.c:5: ../src/test/test-macro.c: In function ‘test_FOREACH_VA_ARGS’: ../src/basic/macro.h:395:90: warning: array subscript 1 is outside array bounds of ‘uint8_t[1]’ {aka ‘unsigned char[1]’} [-Warray-bounds=] 395 | ((long)(_current_ - _entries_) < (long)ELEMENTSOF(_entries_)) && ({ entry = *_current_; true; }); \ ../src/basic/macro.h:392:9: note: in expansion of macro ‘_VA_ARGS_FOREACH’ 392 | _VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), ##__VA_ARGS__) | ^~~~~~~~~~~~~~~~ ../src/test/test-macro.c:322:9: note: in expansion of macro ‘VA_ARGS_FOREACH’ 322 | VA_ARGS_FOREACH(u8, 0) { | ^~~~~~~~~~~~~~~ ../src/fundamental/macro-fundamental.h:163:37: note: at offset 1 into object ‘__unique_prefix__entries_181’ of size 1 163 | #define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq)) | ^~~~~~~~~~~~~~~~ ../src/basic/macro.h:394:28: note: in definition of macro ‘_VA_ARGS_FOREACH’ 394 | for (typeof(entry) _entries_[] = { __VA_ARGS__ }, *_current_ = _entries_; \ | ^~~~~~~~~ ../src/fundamental/macro-fundamental.h:109:27: note: in expansion of macro ‘XCONCATENATE’ 109 | #define CONCATENATE(x, y) XCONCATENATE(x, y) | ^~~~~~~~~~~~ ../src/fundamental/macro-fundamental.h:163:25: note: in expansion of macro ‘CONCATENATE’ 163 | #define UNIQ_T(x, uniq) CONCATENATE(__unique_prefix_, CONCATENATE(x, uniq)) | ^~~~~~~~~~~ ../src/basic/macro.h:392:33: note: in expansion of macro ‘UNIQ_T’ 392 | _VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), ##__VA_ARGS__) | ^~~~~~ ../src/test/test-macro.c:322:9: note: in expansion of macro ‘VA_ARGS_FOREACH’ 322 | VA_ARGS_FOREACH(u8, 0) { | ^~~~~~~~~~~~~~~ (cherry picked from commit dc571cc) (cherry picked from commit 0ddd788) (cherry picked from commit d44fe47) (cherry picked from commit 6a8d6e2)
1 parent c288a3a commit 31ba3f4

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/basic/macro.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,10 +442,10 @@ assert_cc(sizeof(dummy_t) == 0);
442442
/* Iterate through each variadic arg. All must be the same type as 'entry' or must be implicitly
443443
* convertable. The iteration variable 'entry' must already be defined. */
444444
#define VA_ARGS_FOREACH(entry, ...) \
445-
_VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), ##__VA_ARGS__)
446-
#define _VA_ARGS_FOREACH(entry, _entries_, _current_, ...) \
447-
for (typeof(entry) _entries_[] = { __VA_ARGS__ }, *_current_ = _entries_; \
448-
((long)(_current_ - _entries_) < (long)ELEMENTSOF(_entries_)) && ({ entry = *_current_; true; }); \
445+
_VA_ARGS_FOREACH(entry, UNIQ_T(_entries_, UNIQ), UNIQ_T(_current_, UNIQ), UNIQ_T(_va_sentinel_, UNIQ), ##__VA_ARGS__)
446+
#define _VA_ARGS_FOREACH(entry, _entries_, _current_, _va_sentinel_, ...) \
447+
for (typeof(entry) _va_sentinel_[1] = {}, _entries_[] = { __VA_ARGS__ __VA_OPT__(,) _va_sentinel_[0] }, *_current_ = _entries_; \
448+
((long)(_current_ - _entries_) < (long)(ELEMENTSOF(_entries_) - 1)) && ({ entry = *_current_; true; }); \
449449
_current_++)
450450

451451
#include "log.h"

0 commit comments

Comments
 (0)