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 <lokokung@google.com>
Commit-Queue: Austin Eng <enga@chromium.org>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
diff --git a/src/dawn/common/Numeric.h b/src/dawn/common/Numeric.h
index f8e5595..a9a4521 100644
--- a/src/dawn/common/Numeric.h
+++ b/src/dawn/common/Numeric.h
@@ -16,6 +16,9 @@
#define SRC_DAWN_COMMON_NUMERIC_H_
#include <limits>
+#include <type_traits>
+
+#include "dawn/common/Assert.h"
namespace detail {
@@ -39,4 +42,12 @@
template <typename T>
inline constexpr uint32_t u32_alignof = detail::u32_alignof<T>();
+// Only defined for unsigned integers because that is all that is
+// needed at the time of writing.
+template <typename Dst, typename Src, typename = std::enable_if_t<std::is_unsigned_v<Src>>>
+inline Dst checked_cast(const Src& value) {
+ ASSERT(value <= std::numeric_limits<Dst>::max());
+ return static_cast<Dst>(value);
+}
+
#endif // SRC_DAWN_COMMON_NUMERIC_H_
diff --git a/src/dawn/utils/WGPUHelpers.cpp b/src/dawn/utils/WGPUHelpers.cpp
index 7c36d81..52e070a 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 @@
wgpu::BindGroupDescriptor descriptor;
descriptor.layout = layout;
- descriptor.entryCount = entries.size();
+ descriptor.entryCount = checked_cast<uint32_t>(entries.size());
descriptor.entries = entries.data();
return device.CreateBindGroup(&descriptor);