From 68467da17d576a17e4ff96c60f581561ac0bbe88 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Fri, 2 Feb 2024 13:22:25 -0500 Subject: check for invalid integer conversions --- src/inttypes/imaxabs.c | 2 +- src/inttypes/imaxdiv.c | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'src/inttypes') diff --git a/src/inttypes/imaxabs.c b/src/inttypes/imaxabs.c index 9a49cfc9..04cb0440 100644 --- a/src/inttypes/imaxabs.c +++ b/src/inttypes/imaxabs.c @@ -8,7 +8,7 @@ intmax_t imaxabs(intmax_t j) SIGNAL_SAFE(0); if (j == INTMAX_MIN) { - /* undefined behavior */ + UNDEFINED("In call to imaxabs(): The absoluate value of INTMAX_MIN is not representable as an intmax_t"); return INTMAX_MIN; } diff --git a/src/inttypes/imaxdiv.c b/src/inttypes/imaxdiv.c index 7600cfab..c007f762 100644 --- a/src/inttypes/imaxdiv.c +++ b/src/inttypes/imaxdiv.c @@ -7,6 +7,10 @@ imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom) { SIGNAL_SAFE(0); + if ((denom == 0) || (numer == INTMAX_MIN && denom == -1)) { + UNDEFINED("In call to imaxdiv(): The result of %jd / %jd is not representable as an intmax_t", numer, denom); + } + imaxdiv_t r; r.quot = numer / denom; r.rem = numer % denom; -- cgit v1.2.1