Skip to content

Conversation

@Syeberman
Copy link

@Syeberman Syeberman commented Nov 30, 2024

The following expression will trigger a divide-by-zero error when max is zero:

munit/munit.c

Line 971 in fbbdf14

const munit_uint32_t min = (~max + 1U) % max;

This can occur in two places. The first is munit_rand_int_range, when min equals max:

munit/munit.c

Lines 1001 to 1009 in fbbdf14

munit_uint64_t range = (munit_uint64_t) max - (munit_uint64_t) min;
if (min > max)
return munit_rand_int_range(max, min);
if (range > (~((munit_uint32_t) 0U)))
range = (~((munit_uint32_t) 0U));
return min + munit_rand_at_most(0, (munit_uint32_t) range);

The second is in munit_test_runner_run_test, when a parameter has a single value (possible equals 1).

munit/munit.c

Line 1617 in fbbdf14

pidx = munit_rand_at_most(munit_str_hash(test_name), possible - 1);

Put checks in place to avoid calling munit_rand_state_at_most with a max of zero.

This is the same issue referenced in #30 and #59.

@Syeberman Syeberman force-pushed the fix-rand-at-most-divide-by-zero branch from 533b26e to 60ba29a Compare November 30, 2024 23:32
@Syeberman Syeberman force-pushed the fix-rand-at-most-divide-by-zero branch from 60ba29a to 389e5e2 Compare November 30, 2024 23:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants