Skip to content

Commit 930a70d

Browse files
CJ-Johnsoncopybara-github
authored andcommitted
PR #1860: Add unsigned to character buffers to ensure they can provide storage (https://eel.is/c++draft/intro.object#3)
Imported from GitHub PR #1860 I just learned today that `char` cannot "provide storage" in the way I previously thought. Only `std::byte` and `unsigned char` are allowed. So I decided to fix my past mistakes in `absl::InlinedVector` and `absl::Cleanup` as well as any others I could find in Abseil. See: https://eel.is/c++draft/intro.object#3 Merge ae9c1ef into c4ff4d5 Merging this change closes #1860 COPYBARA_INTEGRATE_REVIEW=#1860 from CJ-Johnson:master ae9c1ef PiperOrigin-RevId: 739220589 Change-Id: I533b040e4cf4ee2f48008affb6603c25f34d5b4b
1 parent d95be7f commit 930a70d

File tree

8 files changed

+13
-12
lines changed

8 files changed

+13
-12
lines changed

absl/cleanup/internal/cleanup.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Storage {
8888

8989
private:
9090
bool is_callback_engaged_;
91-
alignas(Callback) char callback_buffer_[sizeof(Callback)];
91+
alignas(Callback) unsigned char callback_buffer_[sizeof(Callback)];
9292
};
9393

9494
} // namespace cleanup_internal

absl/container/fixed_array.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,8 @@ class ABSL_ATTRIBUTE_WARN_UNUSED FixedArray {
447447

448448
private:
449449
ABSL_ADDRESS_SANITIZER_REDZONE(redzone_begin_);
450-
alignas(StorageElement) char buff_[sizeof(StorageElement[inline_elements])];
450+
alignas(StorageElement) unsigned char buff_[sizeof(
451+
StorageElement[inline_elements])];
451452
ABSL_ADDRESS_SANITIZER_REDZONE(redzone_end_);
452453
};
453454

absl/container/internal/inlined_vector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ class Storage {
543543
(std::max)(N, sizeof(Allocated) / sizeof(ValueType<A>));
544544

545545
struct Inlined {
546-
alignas(ValueType<A>) char inlined_data[sizeof(
546+
alignas(ValueType<A>) unsigned char inlined_data[sizeof(
547547
ValueType<A>[kOptimalInlinedSize])];
548548
};
549549

absl/flags/internal/flag.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ class FlagImpl final : public CommandLineFlag {
783783
// heap allocation during initialization, which is both slows program startup
784784
// and can fail. Using reserved space + placement new allows us to avoid both
785785
// problems.
786-
alignas(absl::Mutex) mutable char data_guard_[sizeof(absl::Mutex)];
786+
alignas(absl::Mutex) mutable unsigned char data_guard_[sizeof(absl::Mutex)];
787787
};
788788
#if defined(__GNUC__) && !defined(__clang__)
789789
#pragma GCC diagnostic pop
@@ -876,7 +876,8 @@ class FlagImplPeer {
876876
template <typename T>
877877
void* FlagOps(FlagOp op, const void* v1, void* v2, void* v3) {
878878
struct AlignedSpace {
879-
alignas(MaskedPointer::RequiredAlignment()) alignas(T) char buf[sizeof(T)];
879+
alignas(MaskedPointer::RequiredAlignment()) alignas(
880+
T) unsigned char buf[sizeof(T)];
880881
};
881882
using Allocator = std::allocator<AlignedSpace>;
882883
switch (op) {

absl/flags/internal/registry.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void FinalizeRegistry();
7373
//
7474

7575
// Retire flag with name "name" and type indicated by ops.
76-
void Retire(const char* name, FlagFastTypeId type_id, char* buf);
76+
void Retire(const char* name, FlagFastTypeId type_id, unsigned char* buf);
7777

7878
constexpr size_t kRetiredFlagObjSize = 3 * sizeof(void*);
7979
constexpr size_t kRetiredFlagObjAlignment = alignof(void*);
@@ -87,7 +87,7 @@ class RetiredFlag {
8787
}
8888

8989
private:
90-
alignas(kRetiredFlagObjAlignment) char buf_[kRetiredFlagObjSize];
90+
alignas(kRetiredFlagObjAlignment) unsigned char buf_[kRetiredFlagObjSize];
9191
};
9292

9393
} // namespace flags_internal

absl/flags/reflection.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,10 @@ class RetiredFlagObj final : public CommandLineFlag {
289289

290290
} // namespace
291291

292-
void Retire(const char* name, FlagFastTypeId type_id, char* buf) {
292+
void Retire(const char* name, FlagFastTypeId type_id, unsigned char* buf) {
293293
static_assert(sizeof(RetiredFlagObj) == kRetiredFlagObjSize, "");
294294
static_assert(alignof(RetiredFlagObj) == kRetiredFlagObjAlignment, "");
295-
auto* flag = ::new (static_cast<void*>(buf))
296-
flags_internal::RetiredFlagObj(name, type_id);
295+
auto* flag = ::new (buf) flags_internal::RetiredFlagObj(name, type_id);
297296
FlagRegistry::GlobalRegistry().RegisterFlag(*flag, nullptr);
298297
}
299298

absl/functional/internal/any_invocable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ union TypeErasedState {
174174
} remote;
175175

176176
// Local-storage for the type-erased object when small and trivial enough
177-
alignas(kAlignment) char storage[kStorageSize];
177+
alignas(kAlignment) unsigned char storage[kStorageSize];
178178
};
179179

180180
// A typed accessor for the object in `TypeErasedState` storage

absl/synchronization/mutex_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ TEST(Mutex, Logging) {
17251725
TEST(Mutex, LoggingAddressReuse) {
17261726
// Repeatedly re-create a Mutex with debug logging at the same address.
17271727
ScopedInvariantDebugging scoped_debugging;
1728-
alignas(absl::Mutex) char storage[sizeof(absl::Mutex)];
1728+
alignas(absl::Mutex) unsigned char storage[sizeof(absl::Mutex)];
17291729
auto invariant =
17301730
+[](void *alive) { EXPECT_TRUE(*static_cast<bool *>(alive)); };
17311731
constexpr size_t kIters = 10;

0 commit comments

Comments
 (0)