diff options
author | Chris Robinson <chris.kcat@gmail.com> | 2022-03-26 17:25:57 -0700 |
---|---|---|
committer | Chris Robinson <chris.kcat@gmail.com> | 2022-03-26 17:25:57 -0700 |
commit | b9ff9fa6d90676e2bb961fae25d83b9e4bbbea93 (patch) | |
tree | c014d2e1f0ea6efcc6115b1082e009d179e532bc /common/almalloc.h | |
parent | e53013a549e175777c288066d4d1e7900c8670cc (diff) |
Avoid a variable to subscript an array in offsetof
Diffstat (limited to 'common/almalloc.h')
-rw-r--r-- | common/almalloc.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/common/almalloc.h b/common/almalloc.h index 5efa4f63..ddbd72f4 100644 --- a/common/almalloc.h +++ b/common/almalloc.h @@ -221,7 +221,10 @@ struct FlexArrayStorage { }; static constexpr size_t Sizeof(size_t count, size_t base=0u) noexcept - { return std::max(offsetof(FlexArrayStorage,mArray[count]), sizeof(FlexArrayStorage)) + base; } + { + const size_t len{sizeof(T)*count}; + return std::max(offsetof(FlexArrayStorage,mArray)+len, sizeof(FlexArrayStorage)) + base; + } FlexArrayStorage(size_t size) : mSize{size} { al::uninitialized_default_construct_n(mArray, mSize); } @@ -240,7 +243,10 @@ struct FlexArrayStorage<T,alignment,false> { }; static constexpr size_t Sizeof(size_t count, size_t base) noexcept - { return std::max(offsetof(FlexArrayStorage,mArray[count]), sizeof(FlexArrayStorage)) + base; } + { + const size_t len{sizeof(T)*count}; + return std::max(offsetof(FlexArrayStorage,mArray)+len, sizeof(FlexArrayStorage)) + base; + } FlexArrayStorage(size_t size) : mSize{size} { al::uninitialized_default_construct_n(mArray, mSize); } |