Bad code: using the loop variable after the loop
Original Reporter info from Mantis: Martok @martok
-
Reporter name:
Original Reporter info from Mantis: Martok @martok
- Reporter name:
Description:
In many places, Lazarus, LCL and components use something like this pattern:
begin
for Result:= alow to ahigh do
if Something(Result) then exit;
end;
The value of Result after the loop is actually undefined. Indeed, it is removed if the loop gets unrolled, causing all sorts of uninitialized memory problems.
Using grep -PiR "for\s+result\s*:=" *
over the Lazarus source tree gives lots of occurences of this particular code. There are likely to be others that use another variable name. I'm not sure how to find those...
Likely related to 0033614. Reported as per request on the ML.
Mantis conversion info:
- Mantis ID: 33753