Skip to content

Commit

Permalink
fix warning violations of array-bounds in small_vector
Browse files Browse the repository at this point in the history
Summary:
Fixes or works around the following warning violations:
```
In file included from folly/small_vector.h:17,
                 from folly/container/test/small_vector_test.cpp:17:
In lambda function,
    inlined from ‘void folly::detail::populateMemForward(T*, std::size_t, const Function&) [with T = int; Function = folly::small_vector<int>::constructImpl(size_type, const value_type&, std::true_type)::<lambda(void*)>]’ at folly/container/small_vector.h:229:9,
    inlined from ‘void folly::small_vector<T, M, P>::doConstruct(size_type, InitFunc&&) [with InitFunc = folly::small_vector<int>::constructImpl(size_type, const value_type&, std::true_type)::<lambda(void*)>; Value = int; long unsigned int RequestedMaxInline = 1; Policy = void]’ at folly/container/small_vector.h:1164:33,
    inlined from ‘void folly::small_vector<T, M, P>::constructImpl(size_type, const value_type&, std::true_type) [with Value = int; long unsigned int RequestedMaxInline = 1; Policy = void]’ at folly/container/small_vector.h:1172:16,
    inlined from ‘folly::small_vector<T, M, P>::small_vector(Arg, Arg) [with Arg = int; Value = int; long unsigned int RequestedMaxInline = 1; Policy = void]’ at folly/container/small_vector.h:575:18,
    inlined from ‘virtual void smallVector_InsertTrivial_Test::TestBody()’ at folly/container/test/small_vector_test.cpp:406:40:
folly/container/small_vector.h:1172:35: warning: array subscript 4 is outside array bounds of ‘folly::small_vector<int> [1]’ [-Warray-bounds=]
 1172 |     doConstruct(n, [&](void* p) { new (p) value_type(val); });
      |                                   ^~~~~~~~~~~~~~~~~~~~~~~
```

Reviewed By: ilvokhin

Differential Revision: D68424534

fbshipit-source-id: 9ba06273d38b6008fa4e759fdcf4e40859567c4c
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Jan 22, 2025
1 parent 7a35334 commit 4903369
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions third-party/folly/src/folly/container/small_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,17 @@ class small_vector
}

explicit small_vector(size_type n) {
FOLLY_PUSH_WARNING
FOLLY_GCC_DISABLE_WARNING("-Warray-bounds")
doConstruct(n, [&](void* p) { new (p) value_type(); });
FOLLY_POP_WARNING
}

small_vector(size_type n, value_type const& t) {
FOLLY_PUSH_WARNING
FOLLY_GCC_DISABLE_WARNING("-Warray-bounds")
doConstruct(n, [&](void* p) { new (p) value_type(t); });
FOLLY_POP_WARNING
}

template <class Arg>
Expand Down Expand Up @@ -773,7 +779,10 @@ class small_vector

void resize(size_type sz, value_type const& v) {
if (sz < size()) {
FOLLY_PUSH_WARNING
FOLLY_GCC_DISABLE_WARNING("-Warray-bounds")
erase(begin() + sz, end());
FOLLY_POP_WARNING
return;
}
auto extra = sz - size();
Expand Down Expand Up @@ -1169,7 +1178,10 @@ class small_vector
// The true_type means we should forward to the size_t,value_type
// overload.
void constructImpl(size_type n, value_type const& val, std::true_type) {
FOLLY_PUSH_WARNING
FOLLY_GCC_DISABLE_WARNING("-Warray-bounds")
doConstruct(n, [&](void* p) { new (p) value_type(val); });
FOLLY_POP_WARNING
}

/*
Expand Down

0 comments on commit 4903369

Please sign in to comment.