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 c43c7d6
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 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,22 +309,22 @@ class iovector
struct iovec* begin()
{
do_assert();
return iovs + iov_begin;
return iovs.begin() + iov_begin;
}
const struct iovec* begin() const
{
do_assert();
return iovs + iov_begin;
return iovs.begin() + iov_begin;
}
struct iovec* end()
{
do_assert();
return iovs + iov_end;
return iovs.begin() + iov_end;
}
const struct iovec* end() const
{
do_assert();
return iovs + iov_end;
return iovs.begin() + iov_end;
}
struct iovec& operator[] (int64_t i)
{
Expand Down Expand Up @@ -791,7 +792,7 @@ class iovector
}

void update(iovector_view va) {
iov_begin = va.iov - iovs;
iov_begin = va.iov - iovs.begin();
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];
std::array<struct iovec, 0> 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 const_cast<struct iovec*>(iovs.begin());
}

// 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 @@ -936,7 +937,7 @@ template<uint16_t CAPACITY, uint16_t DEF_PRESERVE>
class IOVectorEntity : public iovector
{
public:
struct iovec iovs[CAPACITY]; // same address with iovector::iovs
std::array<struct iovec, CAPACITY> iovs; // same address with iovector::iovs
struct IOAlloc allocator;
void* bases[CAPACITY]; // same address with IOVAllocation::bases

Expand Down

0 comments on commit c43c7d6

Please sign in to comment.