Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-07

FPC allows modifying loop variable

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Graeme Geldenhuys

Hi,

I just discovered a surprising bug in my application. I had a variable "b" defined which I used in a FOR loop. FPC forbids you to modify that loop variable inside the for loop - which is understandable.

However, the loop called a local procedure, and that local procedure modified the loop variable. FPC never complained about it. Is this a FPC bug?

Here is a test program to show the issue.

=========================
program test;
{$mode objfpc}{$H+}
uses
  SysUtils, classes;
type
  TMyTest = class(TObject)
  public
    procedure Run;
  end;
procedure TMyTest.Run;
var
  b: integer;
  procedure ModifyB;
  begin
    b := 5; // FPC never complains - BUG?
  end;
begin
  for b := 1 to 10 do
  begin
    writeln(b);
    // b := 5;   // FPC will give a compiler error. CORRECT.
    ModifyB;
  end;
end;
var
  t: TMyTest;
begin
  t := TMyTest.Create;
  t.Run;
  t.Free;
end.
=========================
Regards,
  Graeme
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp
Graeme Geldenhuys
to Graeme Geldenhuys
Graeme Geldenhuys wrote
However, the loop called a local procedure, and that local procedure modified the loop variable. FPC never complained about it. Is this a FPC bug?

I forgot to mention, I used FPC 2.6.4.

Regards,
Graeme

Jonas Maebe
to Graeme Geldenhuys: I just discovered a surprising bug in my application. I had a variable "b" defined which…
Graeme Geldenhuys wrote 7 lines
I just discovered a surprising bug in my application. I had a variable "b" defined which I used in a FOR loop. FPC forbids you to modify that loop variable inside the for loop - which is understandable. However, the loop called a local procedure, and that local procedure modified the loop variable. FPC never complained about it. Is this a FPC bug?

FPC has no interprocedural dataflow analysis that could catch something like that.

Jonas

Graeme Geldenhuys
to Jonas Maebe
Jonas Maebe wrote
FPC has no interprocedural dataflow analysis that could catch something like that.

I just tested with Delphi 7, and FPC is Delphi compatible. ;-) D7 doesn't seem to have inter-procedural dataflow analysis either.

Regards,
Graeme