[PATCH 06/11] Sptr: avoided potentially undefined behaviour.

Max Romanov max.romanov at gmail.com
Thu Jun 16 10:35:12 UTC 2022


Hello,

This patch is useless. Please try to understand the structure of sptr_t before made such changes.

How extra assignment to temporary variable may change undefined behavior to defined?

This change was made just to shut the cppcheck which warning is a false positive.

--
Max

-----Original Message-----
From: Andrew Clayton <andrew at digital-domain.net>
To: unit at nginx.org
Sent: чт, 16 июн. 2022 4:02
Subject: [PATCH 06/11] 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(-)

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

_______________________________________________
unit mailing list -- unit at nginx.org
To unsubscribe send an email to unit-leave at nginx.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/unit/attachments/20220616/4a0ea120/attachment.htm>


More information about the unit mailing list