From 0ef5ede3823ef00e95d1cc1a5b475d8d30c9caa4 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 11 Jun 2025 19:32:28 +0200 Subject: py/mpz: Avoid undefined behavior decrementing NULL. In the case where an mpz number is zero, its `len` is 0 and its `dig` is NULL. In that case, decrementing NULL via `d--` is undefined behavior according to the C specification. Restructuring the loops in this way avoids undefined behavior. Also, ensure that these cases are tested in the coverage test. This doesn't make much difference now, but would otherwise cause errors later when the undefined behavior sanitizer is employed in CI. Signed-off-by: Jeff Epler --- ports/unix/coverage.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'ports/unix/coverage.c') diff --git a/ports/unix/coverage.c b/ports/unix/coverage.c index 29e1457cb..9201dab12 100644 --- a/ports/unix/coverage.c +++ b/ports/unix/coverage.c @@ -475,6 +475,18 @@ static mp_obj_t extra_coverage(void) { mp_int_t value_signed; mpz_as_int_checked(&mpz, &value_signed); mp_printf(&mp_plat_print, "%d\n", (int)value_signed); + + // hash the zero mpz integer + mpz_set_from_int(&mpz, 0); + mp_printf(&mp_plat_print, "%d\n", mpz_hash(&mpz)); + + // convert the mpz zero integer to int + mp_printf(&mp_plat_print, "%d\n", mpz_as_int_checked(&mpz, &value_signed)); + mp_printf(&mp_plat_print, "%d\n", value_signed); + + // mpz_set_from_float with 0 as argument + mpz_set_from_float(&mpz, 0); + mp_printf(&mp_plat_print, "%f\n", mpz_as_float(&mpz)); } // runtime utils -- cgit v1.2.3