[fpc-devel] Data flow analysis (dfa) and "case ... of"

Tomas Hajny XHajT03 at hajny.biz
Sun Jul 2 01:25:23 CEST 2017


On Sat, July 1, 2017 16:45, Martok wrote:


Hi,

> The attitude displayed over on #32079 is, quite frankly, terrifying.
> Apparently a language which from the beginning has intrinsics for reading
> and writing files must never be used for doing so, or wild things may
> happen /and that's okay/.
>
> Implying that input should already be sanitized on a bug about something
> that breaks input sanitation code (but only sometimes) is just... wow.
>
> If anybody wants it, here's the patch I'll be rolling on the windows
> snapshots from now on.

I agree that it might be useful to extend the documentation of case ... of
as suggested in one of the comments.

I also agree to Jonas that it should not be a problem of the compiler to
perform these checks. However, there's one thing that worries me - I
assumed that the respective checks should be performed when reading the
input from a file and an error condition should be raised when
encountering a value not fitting the respective type. Unfortunately, this
is apparently not the case, because the following test program happily
runs even when the test file contains a value outside the TEnum range:

type
 TEnum = (one, two, three);

var
 F: file of TEnum;
 E: TEnum;

begin
{$I+}
{$R+}
 Assign (F, 'testfile.tst');
 Reset (F, 1);
 while not (Eof (F)) do
  begin
   Read (F, E);
   WriteLn (Ord (E));
  end;
 Close (F);
end.

Interestingly, the error is caught if I try to write out the read value
back to console (i.e. add WriteLn (E) after the WriteLn line). I believe
that the same error should already happen while reading...

Tomas





More information about the fpc-devel mailing list