mirror of
https://github.com/morgan9e/systemd
synced 2026-04-14 00:14:32 +09:00
macro: add macro that simplifies going backwards through an array via pointers
Inspired by #22797, let's avoid some UB when iterating through arrays.
This commit is contained in:
committed by
Yu Watanabe
parent
4053d11006
commit
50996f04ad
@@ -463,4 +463,20 @@ typedef struct {
|
||||
|
||||
assert_cc(sizeof(dummy_t) == 0);
|
||||
|
||||
/* A little helper for subtracting 1 off a pointer in a safe UB-free way. This is intended to be used for for
|
||||
* loops that count down from a high pointer until some base. A naive loop would implement this like this:
|
||||
*
|
||||
* for (p = end-1; p >= base; p--) …
|
||||
*
|
||||
* But this is not safe because p before the base is UB in C. With this macro the loop becomes this instead:
|
||||
*
|
||||
* for (p = PTR_SUB1(end, base); p; p = PTR_SUB1(p, base)) …
|
||||
*
|
||||
* And is free from UB! */
|
||||
#define PTR_SUB1(p, base) \
|
||||
({ \
|
||||
typeof(p) _q = (p); \
|
||||
_q && _q > (base) ? &_q[-1] : NULL; \
|
||||
})
|
||||
|
||||
#include "log.h"
|
||||
|
||||
Reference in New Issue
Block a user