Skip to content

Commit 04c76ac

Browse files
committed
Fix overflow handling in plpgsql's integer FOR loops.
The test to exit the loop if the integer control value would overflow an int32 turns out not to work on some ICC versions, as it's dependent on the assumption that the compiler will execute the code as written rather than "optimize" it. ICC lacks any equivalent of gcc's -fwrapv switch, so it was optimizing on the assumption of no integer overflow, and that breaks this. Rewrite into a form that in fact does not do any overflowing computations. Per Tomas Vondra and buildfarm member fulmar. It's been like this for a long time, although it was not till we added a regression test case covering the behavior (in commit dd2243f) that the problem became apparent. Back-patch to all supported versions. Discussion: https://postgr.es/m/50562fdc-0876-9843-c883-15b8566c7511@2ndquadrant.com
1 parent ee7bf0f commit 04c76ac

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/pl/plpgsql/src/pl_exec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,13 +2201,13 @@ exec_stmt_fori(PLpgSQL_execstate *estate, PLpgSQL_stmt_fori *stmt)
22012201
*/
22022202
if (stmt->reverse)
22032203
{
2204-
if ((int32) (loop_value - step_value) > loop_value)
2204+
if (loop_value < (PG_INT32_MIN + step_value))
22052205
break;
22062206
loop_value -= step_value;
22072207
}
22082208
else
22092209
{
2210-
if ((int32) (loop_value + step_value) < loop_value)
2210+
if (loop_value > (PG_INT32_MAX - step_value))
22112211
break;
22122212
loop_value += step_value;
22132213
}

0 commit comments

Comments
 (0)