|
| 1 | +From b0ff8c3b258a8816c05bdebf472dbba719d3c491 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Hans Wennborg < [email protected]> |
| 3 | +Date: Tue, 10 Jun 2025 09:51:47 -0700 |
| 4 | +Subject: [PATCH] Don't return an enum from EnumSizeTraits::Count |
| 5 | + |
| 6 | +`Enum::kMaxValue + 1` may be outside the representable range of the |
| 7 | +enum, which Clang will treat as an error in constexpr contexts (see |
| 8 | +bug). |
| 9 | + |
| 10 | +Bug: 423841920 |
| 11 | +Change-Id: I629402cf93bd8419a71f94ff9ed9340d4f88a706 |
| 12 | +Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6633292 |
| 13 | +Auto-Submit: Hans Wennborg < [email protected]> |
| 14 | +Commit-Queue: Nico Weber < [email protected]> |
| 15 | +Reviewed-by: Nico Weber < [email protected]> |
| 16 | +Commit-Queue: Hans Wennborg < [email protected]> |
| 17 | +Cr-Commit-Position: refs/heads/main@{#1471871} |
| 18 | + |
| 19 | +Upstream-Status: Backport [https://github.com/chromium/chromium/commit/b0ff8c3b258a8816c05bdebf472dbba719d3c491] |
| 20 | +--- |
| 21 | + base/metrics/histogram_macros_internal.h | 6 +++--- |
| 22 | + 1 file changed, 3 insertions(+), 3 deletions(-) |
| 23 | + |
| 24 | +diff --git a/base/metrics/histogram_macros_internal.h b/base/metrics/histogram_macros_internal.h |
| 25 | +index 39b232bc1023b8..daa5515c2ab357 100644 |
| 26 | +--- a/base/metrics/histogram_macros_internal.h |
| 27 | ++++ b/base/metrics/histogram_macros_internal.h |
| 28 | +@@ -28,16 +28,16 @@ namespace base::internal { |
| 29 | + template <typename Enum> |
| 30 | + requires(std::is_enum_v<Enum>) |
| 31 | + struct EnumSizeTraits { |
| 32 | +- static constexpr Enum Count() { |
| 33 | ++ static constexpr uintmax_t Count() { |
| 34 | + if constexpr (requires { Enum::kMaxValue; }) { |
| 35 | + // Since the UMA histogram macros expect a value one larger than the max |
| 36 | + // defined enumerator value, add one. |
| 37 | +- return static_cast<Enum>(base::to_underlying(Enum::kMaxValue) + 1); |
| 38 | ++ return static_cast<uintmax_t>(base::to_underlying(Enum::kMaxValue) + 1); |
| 39 | + } else { |
| 40 | + static_assert( |
| 41 | + sizeof(Enum) == 0, |
| 42 | + "enumerator must define kMaxValue enumerator to use this macro!"); |
| 43 | +- return Enum(); |
| 44 | ++ return 0; |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
0 commit comments