Skip to content

Commit 03aab82

Browse files
committed
Put in some more safeguards against executing a division-by-zero.
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
1 parent 2f418e8 commit 03aab82

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/backend/utils/adt/int8.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,13 @@ int8div(PG_FUNCTION_ARGS)
592592
int64 result;
593593

594594
if (arg2 == 0)
595+
{
595596
ereport(ERROR,
596597
(errcode(ERRCODE_DIVISION_BY_ZERO),
597598
errmsg("division by zero")));
599+
/* ensure compiler realizes we mustn't reach the division (gcc bug) */
600+
PG_RETURN_NULL();
601+
}
598602

599603
result = arg1 / arg2;
600604

@@ -639,9 +643,14 @@ int8mod(PG_FUNCTION_ARGS)
639643
int64 arg2 = PG_GETARG_INT64(1);
640644

641645
if (arg2 == 0)
646+
{
642647
ereport(ERROR,
643648
(errcode(ERRCODE_DIVISION_BY_ZERO),
644649
errmsg("division by zero")));
650+
/* ensure compiler realizes we mustn't reach the division (gcc bug) */
651+
PG_RETURN_NULL();
652+
}
653+
645654
/* No overflow is possible */
646655

647656
PG_RETURN_INT64(arg1 % arg2);
@@ -815,9 +824,13 @@ int84div(PG_FUNCTION_ARGS)
815824
int64 result;
816825

817826
if (arg2 == 0)
827+
{
818828
ereport(ERROR,
819829
(errcode(ERRCODE_DIVISION_BY_ZERO),
820830
errmsg("division by zero")));
831+
/* ensure compiler realizes we mustn't reach the division (gcc bug) */
832+
PG_RETURN_NULL();
833+
}
821834

822835
result = arg1 / arg2;
823836

@@ -999,9 +1012,13 @@ int82div(PG_FUNCTION_ARGS)
9991012
int64 result;
10001013

10011014
if (arg2 == 0)
1015+
{
10021016
ereport(ERROR,
10031017
(errcode(ERRCODE_DIVISION_BY_ZERO),
10041018
errmsg("division by zero")));
1019+
/* ensure compiler realizes we mustn't reach the division (gcc bug) */
1020+
PG_RETURN_NULL();
1021+
}
10051022

10061023
result = arg1 / arg2;
10071024

0 commit comments

Comments
 (0)