From 5bc2777230cdc4cea23ff87df356af3fb0cfadf9 Mon Sep 17 00:00:00 2001 From: Ralph Corderoy Date: Wed, 24 Jan 2018 00:27:01 +0000 Subject: [PATCH] %(divide): Avoid SIGFPE on integer divide of INT_MIN by -1. Dividing by zero isn't the only cause of SIGFPE. fmttest -raw -format '%(num -2147483648) %(divide -1)' foo Also, add FIXME comment suggesting the user should be told of overflow rather than silently using zero as the result. --- sbr/fmt_scan.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/sbr/fmt_scan.c b/sbr/fmt_scan.c index 091ba75d..cb29b72c 100644 --- a/sbr/fmt_scan.c +++ b/sbr/fmt_scan.c @@ -795,15 +795,18 @@ fmt_scan (struct format *format, charstring_t scanlp, int width, int *dat, value *= fmt->f_value; break; case FT_LV_DIVIDE_L: - if (fmt->f_value) - value /= fmt->f_value; - else + if (fmt->f_value == 0 || (fmt->f_value == -1 && value == INT_MIN)) { + // FIXME: Tell the user, and probably stop. value = 0; + } else { + value /= fmt->f_value; + } break; case FT_LV_MODULO_L: if (fmt->f_value) value %= fmt->f_value; else + // FIXME: Tell the user, and probably stop. value = 0; break; case FT_SAVESTR: -- 2.56.0