plpgsql FOR loop doesn't guard against strange step values - Mailing list pgsql-hackers

From Tom Lane
Subject plpgsql FOR loop doesn't guard against strange step values
Date
Msg-id 20659.1184437651@sss.pgh.pa.us
Whole thread Raw
Responses Re: plpgsql FOR loop doesn't guard against strange step values  (Peter Eisentraut <peter_e@gmx.net>)
Re: plpgsql FOR loop doesn't guard against strange step values  ("Jaime Casanova" <systemguards@gmail.com>)
List 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


pgsql-hackers by date:

Previous
From: Guillaume Lelarge
Date:
Subject: Re: non-blocking CREATE INDEX in 8.2??
Next
From: Peter Eisentraut
Date:
Subject: Re: plpgsql FOR loop doesn't guard against strange step values