summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAndrew Clayton <andrew@digital-domain.net>2022-06-15 19:59:57 +0100
committerAndrew Clayton <andrew@digital-domain.net>2022-06-15 19:59:57 +0100
commit68afb3efcf123f0a18119a02bfa18691e0e80253 (patch)
tree2e7956d70948772e5c8495d38bf942fc8eb81060
parent6a8081d71e805b12d0f7fd32ce72d60babadfc85 (diff)
downloadunit-archive/overlappingWriteUnion.tar.gz
unit-archive/overlappingWriteUnion.tar.bz2
Sptr: avoided potentially undefined behaviour.archive/overlappingWriteUnion
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/
-rw-r--r--src/nxt_unit_sptr.h5
1 files changed, 4 insertions, 1 deletions
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;
}