Put in some more safeguards against executing a division-by-zero.
authorTom Lane <[email protected]>
Fri, 11 Mar 2011 23:19:03 +0000 (18:19 -0500)
committerTom Lane <[email protected]>
Fri, 11 Mar 2011 23:19:03 +0000 (18:19 -0500)
Add dummy returns before every potential division-by-zero in int8.c,
because apparently further "improvements" in gcc's optimizer have
enabled it to break functions that weren't broken before.

Aurelien Jarno, via Martin Pitt

src/backend/utils/adt/int8.c

index d8ee8c6c9825789e56c2781cf9bba770af4e202a..5e8cc03013759aaec8531cc689c5d4e959223f38 100644 (file)
@@ -599,9 +599,13 @@ int8div(PG_FUNCTION_ARGS)
        int64           result;
 
        if (arg2 == 0)
+       {
                ereport(ERROR,
                                (errcode(ERRCODE_DIVISION_BY_ZERO),
                                 errmsg("division by zero")));
+               /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+               PG_RETURN_NULL();
+       }
 
        result = arg1 / arg2;
 
@@ -646,9 +650,14 @@ int8mod(PG_FUNCTION_ARGS)
        int64           arg2 = PG_GETARG_INT64(1);
 
        if (arg2 == 0)
+       {
                ereport(ERROR,
                                (errcode(ERRCODE_DIVISION_BY_ZERO),
                                 errmsg("division by zero")));
+               /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+               PG_RETURN_NULL();
+       }
+
        /* No overflow is possible */
 
        PG_RETURN_INT64(arg1 % arg2);
@@ -824,9 +833,13 @@ int84div(PG_FUNCTION_ARGS)
        int64           result;
 
        if (arg2 == 0)
+       {
                ereport(ERROR,
                                (errcode(ERRCODE_DIVISION_BY_ZERO),
                                 errmsg("division by zero")));
+               /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+               PG_RETURN_NULL();
+       }
 
        result = arg1 / arg2;
 
@@ -1008,9 +1021,13 @@ int82div(PG_FUNCTION_ARGS)
        int64           result;
 
        if (arg2 == 0)
+       {
                ereport(ERROR,
                                (errcode(ERRCODE_DIVISION_BY_ZERO),
                                 errmsg("division by zero")));
+               /* ensure compiler realizes we mustn't reach the division (gcc bug) */
+               PG_RETURN_NULL();
+       }
 
        result = arg1 / arg2;