Make Chips, Mult and Score unsigned and protect against overflow (#212)

---------

Co-authored-by: MathisMartin31 <mathis.martin31@gmail.com>
This commit is contained in:
Geralt
2025-11-20 12:39:15 +02:00
committed by GitHub
co-authored by MathisMartin31
parent 47b1bac31f
commit 2fb41e533c
6 changed files with 74 additions and 38 deletions
+23
View File
@@ -14,3 +14,26 @@ int int_arr_max(int int_arr[], int size)
return max;
}
// Avoid uint overflow when add/multiplying score
uint32_t u32_protected_add(uint32_t a, uint32_t b)
{
return (a > (UINT32_MAX - b)) ? UINT32_MAX : (a + b);
}
uint16_t u16_protected_add(uint16_t a, uint16_t b)
{
return (a > (UINT16_MAX - b)) ? UINT16_MAX : (a + b);
}
uint32_t u32_protected_mult(uint32_t a, uint32_t b)
{
return (a == 0 || b == 0) ? 0 : (a > (UINT32_MAX / b) ? UINT32_MAX : a * b);
}
uint16_t u16_protected_mult(uint16_t a, uint16_t b)
{
return (a == 0 || b == 0) ? 0 : (a > (UINT16_MAX / b) ? UINT16_MAX : a * b);
}