From 7e03dc7f6347fa7a780a4d144466b43e3ed2094e Mon Sep 17 00:00:00 2001 From: Austin Eng Date: Thu, 28 Apr 2022 22:27:53 +0000 Subject: [PATCH] Fix 64 to 32 bit narrowing in dawn/utils Bug: dawn:1377 Change-Id: Iece057afeeca43092e5e16d7f00d2388dde31d13 Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/87673 Reviewed-by: Loko Kung Commit-Queue: Austin Eng Kokoro: Kokoro Reviewed-by: Corentin Wallez --- src/dawn/common/Numeric.h | 11 +++++++++++ src/dawn/utils/WGPUHelpers.cpp | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/dawn/common/Numeric.h b/src/dawn/common/Numeric.h index f8e559599e..a9a4521fc9 100644 --- a/src/dawn/common/Numeric.h +++ b/src/dawn/common/Numeric.h @@ -16,6 +16,9 @@ #define SRC_DAWN_COMMON_NUMERIC_H_ #include +#include + +#include "dawn/common/Assert.h" namespace detail { @@ -39,4 +42,12 @@ inline constexpr uint32_t u32_sizeof = detail::u32_sizeof(); template inline constexpr uint32_t u32_alignof = detail::u32_alignof(); +// Only defined for unsigned integers because that is all that is +// needed at the time of writing. +template >> +inline Dst checked_cast(const Src& value) { + ASSERT(value <= std::numeric_limits::max()); + return static_cast(value); +} + #endif // SRC_DAWN_COMMON_NUMERIC_H_ diff --git a/src/dawn/utils/WGPUHelpers.cpp b/src/dawn/utils/WGPUHelpers.cpp index 7c36d81428..52e070af3b 100644 --- a/src/dawn/utils/WGPUHelpers.cpp +++ b/src/dawn/utils/WGPUHelpers.cpp @@ -22,6 +22,7 @@ #include "dawn/common/Constants.h" #include "dawn/common/Log.h" +#include "dawn/common/Numeric.h" #include "spirv-tools/optimizer.hpp" @@ -382,7 +383,7 @@ namespace utils { wgpu::BindGroupDescriptor descriptor; descriptor.layout = layout; - descriptor.entryCount = entries.size(); + descriptor.entryCount = checked_cast(entries.size()); descriptor.entries = entries.data(); return device.CreateBindGroup(&descriptor);