plpgsql FOR loop doesn't guard against strange step values

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Subject: plpgsql FOR loop doesn't guard against strange step values
Date: 2007-07-14 18:27:31
Message-ID: 20659.1184437651@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

I just noticed that when the BY option was added to plpgsql FOR loops,
no real error checking was done. If you specify a zero step value,
you'll have an infinite loop. If you specify a negative value, the
loop variable will increment in the "wrong direction" until integer
overflow occurs. Neither of these behaviors seem desirable in the
least.

Another problem is that no check for overflow is done when incrementing
the loop variable, which means that an infinite loop is possible if the
step value is larger than the distance from the loop upper bound to
INT_MAX --- the loop variable could overflow before it is seen to be
greater than the upper bound, and after wrapping around to negative
it's still less than the upper bound, so the loop continues to run.

I suggest throwing an error for zero or negative step value, and
terminating the loop if the loop variable overflows.

Any objections?

regards, tom lane

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2007-07-14 20:33:22 Re: plpgsql FOR loop doesn't guard against strange step values
Previous Message Guillaume Lelarge 2007-07-14 16:23:37 Re: non-blocking CREATE INDEX in 8.2??