git-compat-util.h: implement checked size_t to uint32_t conversion
In a similar fashion as other checked cast functions in this header (such as `cast_size_t_to_ulong()` and `cast_size_t_to_int()`), implement a checked cast function for going from a size_t to a uint32_t value. This function will be utilized in a future commit which needs to make such a conversion. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Taylor Blau committed
Dec 14, 2023 at 17:24 UTC
ed9f41480a32fff7f3ec66ba2d4bf618885e6ad2
1 file changed
+9
git-compat-util.h
+9
@@ -1013,6 +1013,15 @@ static inline unsigned long cast_size_t_to_ulong(size_t a)
1013
return (unsigned long)a;
1014
}
1015
1016
+static inline uint32_t cast_size_t_to_uint32_t(size_t a)
1017
+{
1018
+ if (a != (uint32_t)a)
1019
+ die("object too large to read on this platform: %"
1020
+ PRIuMAX" is cut off to %u",
1021
+ (uintmax_t)a, (uint32_t)a);
1022
+ return (uint32_t)a;
1023
+}
1024
+
1025
static inline int cast_size_t_to_int(size_t a)
1026
{
1027
if (a > INT_MAX)