[PATCH 06/11] Sptr: avoided potentially undefined behaviour.
Andrew Clayton
andrew at digital-domain.net
Thu Jun 16 01:00:56 UTC 2022
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(-)
diff --git a/src/nxt_unit_sptr.h b/src/nxt_unit_sptr.h
index 314416e..6d867a5 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;
}
--
2.36.1
More information about the unit
mailing list