Hi,
I've just noticed that there is a bug in the string assignment if a string is passed by value to a procedure and then assigned to. See the excerpt below, the length of "a" after the assignment is still 1. I'm using: adamn@linux:~/pascal/bug/gpc>gpc -v Reading specs from /usr/local/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/specs Configured with: ../gcc-3.3.1/configure --enable-languages=pascal Thread model: posix gpc version 20040516, based on gcc-3.3.1
--------------------------- program string_value_bug;
procedure P(a:string); begin a:='BC'; writeln('a=',a); end;
begin P('A'); end. ---------------------------
Best, Adam Naumowicz
====================================================================== Department of Applied Logic fax. +48 (85) 745-7662 Institute of Computer Science tel. +48 (85) 745-7559 (office) University of Bialystok e-mail: adamn@mizar.org Sosnowa 64, 15-887 Bialystok, Poland http://math.uwb.edu.pl/~adamn/ ======================================================================
Adam Naumowicz wrote:
I've just noticed that there is a bug in the string assignment if a string is passed by value to a procedure and then assigned to.
It's not a bug. `String' is a schema type which adjusts to the actual parameter. In this case it has a capacity of 1 (since the value passed is a constant, the capacity is set equal to its length). So assigning anything longer will not fit.
(Or to express it low-level, as some people seem to prefer: How much space should the compiler allocate on the stack for the parameter? It can't know in advance how long a value you're going to assign to it.)
If you're used to BP where `String' means `String [255]', you might want to write so (or you might want to use `TString' from module GPC which is a bit longer). But any fixed length may, of course, overrun, unless you've verified what you're actually assigning to it ...
Frank
On Tue, 7 Sep 2004, Frank Heckenbach wrote:
Adam Naumowicz wrote:
I've just noticed that there is a bug in the string assignment if a string is passed by value to a procedure and then assigned to.
It's not a bug. `String' is a schema type which adjusts to the actual parameter. In this case it has a capacity of 1 (since the value passed is a constant, the capacity is set equal to its length). So assigning anything longer will not fit.
Thanks, I suspected that it can be a "feature", but since there was no error/warning (knowing the strict rules of GPC parameter type checking), I expected it to make a full assignment. I strongly feel that a warning is due here.
(Or to express it low-level, as some people seem to prefer: How much space should the compiler allocate on the stack for the parameter? It can't know in advance how long a value you're going to assign to it.)
I just forgot what was the actual realization of GPC string schema and imagined that Chars is a POINTER to a char array which is dynamically allocated when necessery. A bit like FPC/Delphi Ansistrings with which all the low-level conversion stuff is done transparently for all string-like type assignments...
Regards,
Adam Naumowicz
====================================================================== Department of Applied Logic fax. +48 (85) 745-7662 Institute of Computer Science tel. +48 (85) 745-7559 (office) University of Bialystok e-mail: adamn@mizar.org Sosnowa 64, 15-887 Bialystok, Poland http://math.uwb.edu.pl/~adamn/ ======================================================================
Adam Naumowicz wrote:
It's not a bug. `String' is a schema type which adjusts to the actual parameter. In this case it has a capacity of 1 (since the value passed is a constant, the capacity is set equal to its length). So assigning anything longer will not fit.
Thanks, I suspected that it can be a "feature", but since there was no error/warning (knowing the strict rules of GPC parameter type checking), I expected it to make a full assignment. I strongly feel that a warning is due here.
It's strictly typed. That's jsut the behaviour of schema types. I don't see what we could warn about (or when -- generally warning about assignments to value parameters would be far too broad I suppose).
Perhaps there should be an (optional) runtime check on the assignment instead of silent truncation, though.
I just forgot what was the actual realization of GPC string schema and imagined that Chars is a POINTER to a char array which is dynamically allocated when necessery. A bit like FPC/Delphi Ansistrings with which all the low-level conversion stuff is done transparently for all string-like type assignments...
No, the string schema is not like that. We might add such a type in the future, but not now.
Frank