]> diplodocus.org Git - nmh/commitdiff
%(divide): Avoid SIGFPE on integer divide of INT_MIN by -1.
authorRalph Corderoy <ralph@inputplus.co.uk>
Wed, 24 Jan 2018 00:27:01 +0000 (00:27 +0000)
committerRalph Corderoy <ralph@inputplus.co.uk>
Thu, 25 Jan 2018 14:44:44 +0000 (14:44 +0000)
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

index 091ba75..cb29b72 100644 (file)
@@ -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: