[PATCH 01/11] Array: avoided void pointer arithmetic in nxt_array_copy().

Andrew Clayton andrew at digital-domain.net
Thu Jun 16 01:00:51 UTC 2022


As was pointed out by the cppcheck[0] static code analysis utility we
were doing void pointer arithmetic on src->elts which is technically
undefined behaviour.

While GCC allows this by treating the size of void as 1[1]. Same with
Clang. Other compilers I'm not sure about, so lets just be safe and cast
src->nelts to (char *) where sizeof(char) is guaranteed to be 1.

[0]: https://cppcheck.sourceforge.io/
[1]: https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html
---
 src/nxt_array.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/nxt_array.c b/src/nxt_array.c
index 1e13c22..0a7945e 100644
--- a/src/nxt_array.c
+++ b/src/nxt_array.c
@@ -140,7 +140,7 @@ nxt_array_copy(nxt_mp_t *mp, nxt_array_t *dst, nxt_array_t *src)
                 return NULL;
             }
 
-            nxt_memcpy(data, src->elts + (i * size), size);
+            nxt_memcpy(data, (char *) src->elts + (i * size), size);
         }
     }
 
-- 
2.36.1



More information about the unit mailing list