From 68afb3efcf123f0a18119a02bfa18691e0e80253 Mon Sep 17 00:00:00 2001 From: Andrew Clayton Date: Wed, 15 Jun 2022 19:59:57 +0100 Subject: Sptr: avoided potentially undefined behaviour. In src/nxt_unit_sptr.h::nxt_unit_sptr_set() we are setting one member of a union based on another member which cppcheck[0] flags as undefined behaviour src/nxt_unit_sptr.h:27:18: error: Overlapping read/write of union is undefined behavior [overlappingWriteUnion] sptr->offset = (uint8_t *) ptr - sptr->base; ^ I think this warning is correct as I can't see where we are setting sptr->base beforehand which I think would make this defined behaviour. To avoid any doubts take a copy of sptr->base and then use that value in the above. [0]: https://cppcheck.sourceforge.io/ --- src/nxt_unit_sptr.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nxt_unit_sptr.h b/src/nxt_unit_sptr.h index 314416e4..6d867a5e 100644 --- a/src/nxt_unit_sptr.h +++ b/src/nxt_unit_sptr.h @@ -24,7 +24,10 @@ union nxt_unit_sptr_u { static inline void nxt_unit_sptr_set(nxt_unit_sptr_t *sptr, void *ptr) { - sptr->offset = (uint8_t *) ptr - sptr->base; + const uint8_t *base; + + base = sptr->base; + sptr->offset = (uint8_t *) ptr - base; } -- cgit