12 Commits

Author SHA1 Message Date
zeichensystem
45f07986c3 Add string functions 2025-12-21 17:30:13 +01:00
jun
c54fc75221 Use least vs fast int types 2025-05-19 09:50:05 +02:00
jun
57f0e47efc Refactor to use portable minimum-width integers.
The signed and unsigned fixed-width integers (int32_t, uint32_t etc.) are optional
in C99 (and above). Use the non-optional minimum-width integers (int_fast32_t, uint_fast32_t and int_least32_t, uint_least32_t etc.) instead.

To simulate unsigned wrap-around, use the GUF_UWRAP macros in guf_common.h

cf. https://en.cppreference.com/w/c/types/integer (last-retrieved: 2025-05-18)
2025-05-18 22:03:03 +02:00
jun
614a9716cc Fix failed assertion in guf_str_copy
An assertion GUF_ASSERT(str_is_valid(dst)) failed in guf_str_copy when it called guf_str_cstr(dst)
 since guf_str_cstr assumes an already valid string, which was not the case when src was a short string.

Therefore, we get the dst's c_str now without calling guf_str_cstr(dst)

(Found by writing DbufStrTest.)
2025-05-15 20:53:50 +02:00
jun
9b64b22806 Make signed guf_wrapping_mul more general
Don't fail on mod == 0 (in weird cases where the given UNSIGNED_TYPE_MAX == SIGNED_TYPE_MAX)
but
res = mod > 0 ? (1u * res % mod) : res;
2025-05-15 09:21:30 +02:00
jun
466982ffcb Use better terminology for overflow and integer 'underflow' 2025-05-15 09:11:31 +02:00
jun
ec074d5753 Fix potential int-promotion bugs in ckdint
Expressions like
(uin16_t)a * (uint16_t)b * (uint16_t)c
might be promoted to (signed) int (in that example, on platforms where sizeof(int) > sizeof(uint16_t)),
and therefore lead to undefined behaviour on overflow.

The above expression can be fixed as
1u * (uint16_t)a * (uint16_t)b * (uint16_t)c
(The 1u makes sure a, b, and c would be promoted to unsigned int (instead of int) on platforms where sizeof(int) > sizeof(uint16_t))

cf. https://stackoverflow.com/questions/27001604/32-bit-unsigned-multiply-on-64-bit-causing-undefined-behavior
2025-05-15 08:46:54 +02:00
jun
37f9011166 Add guf_alloc_tracker 2025-05-14 14:47:20 +02:00
jun
6ffb79f7a0 Fix implementation defined behaviour in guf_wrapping_mul 2025-05-14 08:51:41 +02:00
jun
6091eac820 Add guf_math_ckdint IMPL_STATIC etc. 2025-05-13 14:56:21 +02:00
jun
873cdf20b1 Add checked mul arithmetic 2025-05-13 12:45:17 +02:00
jun
9c417d2aa1 Add checked arithmetic 2025-05-11 08:55:03 +02:00