From bd6aab3513babbfd12abf5c7a5a2629e9cc6bb68 Mon Sep 17 00:00:00 2001 From: Coldwings Date: Tue, 6 Feb 2024 14:35:24 +0800 Subject: [PATCH] fix UB that cause warning --- common/iovector.h | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/common/iovector.h b/common/iovector.h index 08f6193f..53bc633f 100644 --- a/common/iovector.h +++ b/common/iovector.h @@ -34,6 +34,7 @@ limitations under the License. #include #include #include +#include #include #include @@ -308,34 +309,34 @@ class iovector struct iovec* begin() { do_assert(); - return iovs + iov_begin; + return iovs_ptr() + iov_begin; } const struct iovec* begin() const { do_assert(); - return iovs + iov_begin; + return iovs_ptr() + iov_begin; } struct iovec* end() { do_assert(); - return iovs + iov_end; + return iovs_ptr() + iov_end; } const struct iovec* end() const { do_assert(); - return iovs + iov_end; + return iovs_ptr() + iov_end; } struct iovec& operator[] (int64_t i) { do_assert(); assert(iov_begin + i < iov_end); - return iovs[iov_begin + i]; + return iovs_ptr()[iov_begin + i]; } const struct iovec& operator[] (int64_t i) const { do_assert(); assert(iov_begin + i < iov_end); - return iovs[iov_begin + i]; + return iovs_ptr()[iov_begin + i]; } // push some stuff to the front, and @@ -344,7 +345,7 @@ class iovector { do_assert(); IF_ASSERT_RETURN(iov_begin > 0, 0); - iovs[--iov_begin] = iov; + iovs_ptr()[--iov_begin] = iov; return iov.iov_len; } size_t push_front(void* buf, size_t size) @@ -369,7 +370,7 @@ class iovector { do_assert(); IF_ASSERT_RETURN(iov_end < capacity, 0); - iovs[iov_end++] = iov; + iovs_ptr()[iov_end++] = iov; return iov.iov_len; } size_t push_back(void* buf, size_t size) @@ -393,13 +394,13 @@ class iovector { do_assert(); IF_ASSERT_RETURN(!empty(), 0); - return iovs[iov_begin++].iov_len; + return iovs_ptr()[iov_begin++].iov_len; } size_t pop_back() { do_assert(); IF_ASSERT_RETURN(!empty(), 0); - return iovs[--iov_end].iov_len; + return iovs_ptr()[--iov_end].iov_len; } void clear() // doesn't dispose() @@ -772,7 +773,7 @@ class iovector clear_dispose(); iov_end = end; - memcpy(begin(), rhs.begin(), iovcnt() * sizeof(iovs[0])); + memcpy(begin(), rhs.begin(), iovcnt() * sizeof(iovs_ptr()[0])); allocator()->copy(rhs.allocator(), rhs.nbases); nbases = rhs.nbases; rhs.nbases = 0; @@ -791,7 +792,7 @@ class iovector } void update(iovector_view va) { - iov_begin = va.iov - iovs; + iov_begin = va.iov - iovs_ptr(); iov_end = iov_begin + va.iovcnt; } @@ -827,7 +828,7 @@ class iovector uint16_t capacity; // total capacity uint16_t iov_begin, iov_end; // [iov_begin, iov_end) uint16_t nbases; // # of allocated pointers - struct iovec iovs[0]; + // struct iovec iovs[]; friend iovector* new_iovector(uint16_t capacity, uint16_t preserve); friend void delete_iovector(iovector* ptr); @@ -841,7 +842,7 @@ class iovector inline __attribute__((always_inline)) struct iovec* iovs_ptr() const { - return const_cast(iovs); + return reinterpret_cast(const_cast(this + 1)); } // not allowed to freely construct / destruct @@ -861,7 +862,7 @@ class iovector struct IOVAllocation_ : public IOAlloc { - void* bases[0]; + void* bases[]; void* do_allocate(int size, uint16_t& nbases, uint16_t capacity) { return do_allocate(size, size, nbases, capacity); @@ -898,7 +899,7 @@ class iovector IOVAllocation_* allocator() const { // this makes an assumption of the memory layout as `IOVectorEntity` - auto addr = (char*)this + sizeof(*this) +sizeof(iovs[0]) * capacity; + auto addr = (char*)this + sizeof(*this) +sizeof(iovs_ptr()[0]) * capacity; return (IOVAllocation_*)addr; } void dispose()