From 6007dd7683752c77cdf9d7c2fbfe8d3a61ea42df Mon Sep 17 00:00:00 2001 From: Jake-Moss Date: Sun, 8 Sep 2024 12:27:19 +1000 Subject: [PATCH] Fix division by zero in `monomials_deflate.c`. The documentation for `fmpz_mpoly_deflate` (and similar) states > If any `stride[v]` is zero, the corresponding numerator `e - shift[v]` is assumed to > be zero, and the quotient is defined as zero. This allows the function to undo the > operation performed by `fmpz_mpoly_inflate()` when possible. The previous comment and if-statement did not align meaning that the denominator was not being checked leading to divisions by zero when `stride[v]` is 0. --- src/mpoly/monomials_deflate.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/mpoly/monomials_deflate.c b/src/mpoly/monomials_deflate.c index cc7b4809aa..b9c9cfab17 100644 --- a/src/mpoly/monomials_deflate.c +++ b/src/mpoly/monomials_deflate.c @@ -36,12 +36,18 @@ void mpoly_monomials_deflate(ulong * Aexps, flint_bitcnt_t Abits, for (j = 0; j < nvars; j++) { fmpz_sub(exps + j, exps + j, shift + j); - /* stride + j is allowed to be zero */ - if (!fmpz_is_zero(exps + j)) + /* stride + j is allowed to be zero, if it is then exps + j is + assumed to be zero, and the quotient is defined as zero */ + if (!fmpz_is_zero(exps + j) && !fmpz_is_zero(stride + j)) { FLINT_ASSERT(fmpz_divisible(exps + j, stride + j)); fmpz_divexact(exps + j, exps + j, stride + j); } + else + { + fmpz_zero(exps + j); + } + } FLINT_ASSERT(Abits >= mpoly_exp_bits_required_ffmpz(exps, mctx)); mpoly_set_monomial_ffmpz(Aexps + NA*i, exps, Abits, mctx);