unix: Prevent compiler optimization to ignore our memset to zero

It's well known that memset may be optimized out by compilers and this
is one of these cases that freebsd CI highlighted.

To prevent this to happen we should use memset_explicit() but that's C23, so
till we don't support that, let's re-implement that ourself
making the compiler not to optimize our memset's.

In theory we could just rely on C11's memset_s, but that's not working
either in freebsd.
This commit is contained in:
Marco Trevisan (Treviño) 2024-04-16 12:05:52 +02:00
parent 035c318324
commit 3d474bd8c1

View File

@ -593,6 +593,10 @@ test_signal_alternate_stack (int signal)
*/
g_assert_cmpint (memcmp (stack_memory, zero_mem, MINSIGSTKSZ), !=, 0);
/* We need to memset again zero_mem since compiler may have optimized it out
* as we've seen in freebsd CI.
*/
memset (zero_mem, 0, MINSIGSTKSZ);
memset (stack_memory, 0, MINSIGSTKSZ);
g_assert_cmpmem (stack_memory, MINSIGSTKSZ, zero_mem, MINSIGSTKSZ);