<div>Hello,<br/>
<br/>
This patch is useless. Please try to understand the structure of sptr_t before made such changes.<br/>
<br/>
How extra assignment to temporary variable may change undefined behavior to defined?<br/>
<br/>
This change was made just to shut the cppcheck which warning is a false positive.<br/>
<br/>
--<br/>
Max<br/><br/>-----Original Message-----<br/>From: Andrew Clayton <andrew@digital-domain.net><br/>To: unit@nginx.org<br/>Sent: чт, 16 июн. 2022 4:02<br/>Subject: [PATCH 06/11] Sptr: avoided potentially undefined behaviour.<br/><br/></div>In src/nxt_unit_sptr.h::nxt_unit_sptr_set() we are setting one member of
<br/>
a union based on another member which cppcheck[0] flags as undefined
<br/>
behaviour
<br/>
<br/>
src/nxt_unit_sptr.h:27:18: error: Overlapping read/write of union is undefined behavior [overlappingWriteUnion]
<br/>
sptr->offset = (uint8_t *) ptr - sptr->base;
<br/>
^
<br/>
<br/>
I think this warning is correct as I can't see where we are setting
<br/>
sptr->base beforehand which I think would make this defined behaviour.
<br/>
<br/>
To avoid any doubts take a copy of sptr->base and then use that value in
<br/>
the above.
<br/>
<br/>
[0]: <a href="https://cppcheck.sourceforge.io">https://cppcheck.sourceforge.io</a>/
<br/>
---
<br/>
src/nxt_unit_sptr.h | 5 ++++-
<br/>
1 file changed, 4 insertions(+), 1 deletion(-)
<br/>
<br/>
diff --git a/src/nxt_unit_sptr.h b/src/nxt_unit_sptr.h
<br/>
index 314416e..6d867a<a href="tel:5100644">5 100644</a>
<br/>
--- a/src/nxt_unit_sptr.h
<br/>
+++ b/src/nxt_unit_sptr.h
<br/>
@@ -24,7 +24,10 @@ union nxt_unit_sptr_u {
<br/>
static inline void
<br/>
nxt_unit_sptr_set(nxt_unit_sptr_t *sptr, void *ptr)
<br/>
{
<br/>
- sptr->offset = (uint8_t *) ptr - sptr->base;
<br/>
+ const uint8_t *base;
<br/>
+
<br/>
+ base = sptr->base;
<br/>
+ sptr->offset = (uint8_t *) ptr - base;
<br/>
}
<br/>
<br/>
<br/>
--
<br/>
2.36.1
<br/>
<br/>
_______________________________________________
<br/>
unit mailing list -- <a href="mailto:unit@nginx.org">unit@nginx.org</a>
<br/>
To unsubscribe send an email to <a href="mailto:unit-leave@nginx.org">unit-leave@nginx.org</a>
<br/>