I have encountered a couple problems with strings in the most recent GPC beta
version. Most of the problems are due to missing range checks, e.g. when
assigning a string to a shorter string, or passing a string to a procedure
with a shorter string as formal parameter.
1) The program below illustrates some of these problems:
program Test5;
var
s1 : string [500];
s2 : string [10];
type
str5 = string [5];
procedure p1 (s : str5);
begin
Writeln (s.capacity, ' ', Length (s));
end;
begin
s1 := 'This is a relatively long string';
s2 := s1; { Copies all of s1 to s2; not just the first 10 characters }
p1 (s1); { Passes all of s1 to procedure p1 }
p1 (s2);
end.
Before exiting with a SIGSEGV exception (under DJGPP), the program prints
5 32
5 32
2) Another facet of the problem occurs when reading strings from a text file
with Read[ln] (F, stringVar).
If you call Readln, the program will read from the file until the next CR/LF,
no matter the size (length) of stringVar, and try to copy the entire line to
the variables stringVar (which will almost surely crash the program somehow;
but not necessarily in the call to Readln. The SIGSEGV exception could come
later, and with a stack trace which makes no sense).
On the other hand, Read (F, stringVar) only reads a maximum of
Length(stringVar) characters [as it should].
Jesper Lund