Skip to content

Commit ed9f414

Browse files
ttaylorrgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b96289a commit ed9f414

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

git-compat-util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,15 @@ static inline unsigned long cast_size_t_to_ulong(size_t a)
10131013
return (unsigned long)a;
10141014
}
10151015

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+
10161025
static inline int cast_size_t_to_int(size_t a)
10171026
{
10181027
if (a > INT_MAX)

0 commit comments

Comments
 (0)