Skip to content

Commit c8a7db0

Browse files
r-barnesmeta-codesync[bot]
authored andcommitted
Revert D106566362: Fix -Wunsafe-buffer-usage errors in folly using safer C++ abstractions
Differential Revision: D106566362 Original commit changeset: e03856898593 Original Phabricator Diff: D106566362 fbshipit-source-id: 3cbd5925eb83634c39ebae0e1ccd739cd08d7789
1 parent 1c61ad4 commit c8a7db0

3 files changed

Lines changed: 49 additions & 26 deletions

File tree

‎folly/container/SparseByteSet.h‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#pragma once
1818

19-
#include <array>
2019
#include <cassert>
2120
#include <cstdint>
2221

@@ -117,8 +116,8 @@ class SparseByteSet {
117116
private:
118117
uint16_t size_; // can't use uint8_t because it would overflow if all
119118
// possible values were inserted.
120-
std::array<uint8_t, kCapacity> sparse_{};
121-
std::array<uint8_t, kCapacity> dense_{};
119+
uint8_t sparse_[kCapacity];
120+
uint8_t dense_[kCapacity];
122121
};
123122

124123
} // namespace folly

‎folly/detail/base64_detail/test/Base64PlatformTest.cpp‎

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <cstdint>
1919
#include <cstring>
2020
#include <numeric>
21-
#include <string_view>
2221
#include <folly/portability/GTest.h>
2322

2423
#include <folly/detail/base64_detail/Base64_SSE4_2_Platform.h>
@@ -31,15 +30,27 @@ std::array<std::uint8_t, 16> expectedEncodeToIndexes(
3130
std::array<std::uint8_t, 16> in) {
3231
std::array<std::uint8_t, 16> res{};
3332

34-
for (std::size_t fi = 0, oi = 0; oi < 16; fi += 3, oi += 4) {
35-
std::uint8_t aaab = in[fi];
36-
std::uint8_t bbcc = in[fi + 1];
37-
std::uint8_t cddd = in[fi + 2];
33+
std::uint8_t const* f = in.data();
34+
std::uint8_t* o = res.data();
35+
std::uint8_t* const oEnd = res.data() + res.size();
3836

39-
res[oi] = aaab >> 2;
40-
res[oi + 1] = ((aaab << 4) | (bbcc >> 4)) & 0x3f;
41-
res[oi + 2] = ((bbcc << 2) | (cddd >> 6)) & 0x3f;
42-
res[oi + 3] = cddd & 0x3f;
37+
while (o != oEnd) {
38+
std::uint8_t aaab = f[0];
39+
std::uint8_t bbcc = f[1];
40+
std::uint8_t cddd = f[2];
41+
42+
std::uint8_t aaa = aaab >> 2;
43+
std::uint8_t bbb = ((aaab << 4) | (bbcc >> 4)) & 0x3f;
44+
std::uint8_t ccc = ((bbcc << 2) | (cddd >> 6)) & 0x3f;
45+
std::uint8_t ddd = cddd & 0x3f;
46+
47+
o[0] = aaa;
48+
o[1] = bbb;
49+
o[2] = ccc;
50+
o[3] = ddd;
51+
52+
f += 3;
53+
o += 4;
4354
}
4455

4556
return res;
@@ -50,10 +61,26 @@ std::array<std::uint8_t, 16> expectedPackIndexesToBytes(
5061
std::array<std::uint8_t, 16> res{};
5162
res.fill(0);
5263

53-
for (std::size_t fi = 0, oi = 0; fi < 16; fi += 4, oi += 3) {
54-
res[oi] = (in[fi] << 2) | (in[fi + 1] >> 4);
55-
res[oi + 1] = (in[fi + 1] << 4) | (in[fi + 2] >> 2);
56-
res[oi + 2] = (in[fi + 2] << 6) | in[fi + 3];
64+
std::uint8_t const* f = in.data();
65+
std::uint8_t const* const inEnd = in.data() + in.size();
66+
std::uint8_t* o = res.data();
67+
68+
while (f != inEnd) {
69+
std::uint8_t aaa = f[0];
70+
std::uint8_t bbb = f[1];
71+
std::uint8_t ccc = f[2];
72+
std::uint8_t ddd = f[3];
73+
74+
std::uint8_t aaab = (aaa << 2) | (bbb >> 4);
75+
std::uint8_t bbcc = (bbb << 4) | (ccc >> 2);
76+
std::uint8_t cddd = (ccc << 6) | ddd;
77+
78+
o[0] = aaab;
79+
o[1] = bbcc;
80+
o[2] = cddd;
81+
82+
f += 4;
83+
o += 3;
5784
}
5885

5986
return res;
@@ -64,7 +91,7 @@ constexpr std::string_view kBase64EncodeTable{
6491
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="};
6592

6693
std::array<std::uint8_t, 16> expectedLookupByIndex(
67-
std::array<std::uint8_t, 16> in, std::string_view sampleTable) {
94+
std::array<std::uint8_t, 16> in, const char* sampleTable) {
6895
std::array<std::uint8_t, 16> res{};
6996

7097
for (std::size_t i = 0; i != in.size(); ++i) {
@@ -139,7 +166,7 @@ TYPED_TEST(Base64PlatformTest, EncodeToIndexes) {
139166

140167
for (std::uint16_t v = 0; v != 256; v += 8) {
141168
RegBytes in;
142-
std::iota(in.begin(), in.end(), static_cast<std::uint8_t>(v));
169+
std::iota(in.data(), in.data() + in.size(), static_cast<std::uint8_t>(v));
143170

144171
RegBytes expected = expectedEncodeToIndexes(in);
145172
RegBytes actual = TestFixture::actualEncodeToIndexes(in);
@@ -155,7 +182,7 @@ TYPED_TEST(Base64PlatformTest, IndexLookup) {
155182
i != kBase64EncodeTable.size() + 1 - RegBytes{}.size();
156183
i += 1) {
157184
RegBytes in;
158-
std::iota(in.begin(), in.end(), i);
185+
std::iota(in.data(), in.data() + in.size(), i);
159186
RegBytes expected = expectedLookupByIndex(in, kBase64EncodeTable);
160187
RegBytes actual = TestFixture::actualLookupByIndex(in);
161188
ASSERT_EQ(expected, actual);
@@ -203,7 +230,7 @@ TYPED_TEST(Base64PlatformTest, decodeToIndexSuccess) {
203230
// Some cases
204231
for (std::uint16_t v = 0; v < 256; v += 1) {
205232
RegBytes in;
206-
std::iota(in.begin(), in.end(), static_cast<std::uint8_t>(v));
233+
std::iota(in.data(), in.data() + in.size(), static_cast<std::uint8_t>(v));
207234

208235
for (auto& x : in) {
209236
x = x % 64;
@@ -223,9 +250,8 @@ TYPED_TEST(Base64PlatformTest, packIndexesToBytes) {
223250
for (std::uint16_t v = 0; v < 256; v += 1) {
224251
RegBytes in;
225252
in.fill(0);
226-
for (std::size_t i = 0; i < in.size() / 4 * 3; ++i) {
227-
in[i] = static_cast<std::uint8_t>(static_cast<std::uint8_t>(v) + i);
228-
}
253+
std::iota(
254+
in.data(), in.data() + in.size() / 4 * 3, static_cast<std::uint8_t>(v));
229255

230256
for (auto& x : in) {
231257
x = x % 64;

‎folly/functional/Invoke.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#pragma once
1818

19-
#include <array>
2019
#include <functional>
2120
#include <type_traits>
2221

@@ -421,8 +420,7 @@ struct invoke_first_match : private Invoker... {
421420
using at = type_pack_element_t<Idx, Invoker...>;
422421
template <size_t... Idx, typename... A>
423422
static constexpr size_t first_(std::index_sequence<Idx...>, tag_t<A...>) {
424-
constexpr std::array<bool, sizeof...(Idx) + 1> r = {
425-
is_invocable_v<at<Idx> const&, A...>..., false};
423+
constexpr bool r[] = {is_invocable_v<at<Idx> const&, A...>..., false};
426424
for (size_t i = 0; i < sizeof...(Invoker); ++i) {
427425
if (r[i]) {
428426
return i;

0 commit comments

Comments
 (0)