Skip to content

Commit

Permalink
fix UB that cause warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldwings committed Feb 6, 2024
1 parent ee5a2f5 commit bd6aab3
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions common/iovector.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ limitations under the License.
#include <cstring>
#include <new>
#include <memory>
#include <array>
#include <sys/uio.h>

#include <photon/common/callback.h>
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -841,7 +842,7 @@ class iovector

inline __attribute__((always_inline))
struct iovec* iovs_ptr() const {
return const_cast<struct iovec*>(iovs);
return reinterpret_cast<struct iovec*>(const_cast<iovector*>(this + 1));
}

// not allowed to freely construct / destruct
Expand All @@ -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);
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit bd6aab3

Please sign in to comment.