Jesper Lund wrote:
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.
- The program below illustrates some of these problems:
Yes, this is a known bug. I think it will be solved soon.
- 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).
Strange! There has always been a check for the maximum length in the read[ln] procedure.
On the other hand, Read (F, stringVar) only reads a maximum of Length(stringVar) characters [as it should].
Even more strange, since Read and Readln are actually the same procedure (or rather function) internally. Readln (F, stringVar) first does exactly the same as Read (F, stringVar), and then a Readln (stringVar).
Perhaps something was broken temporarily while I changed other things in the Read/Write procedures. OTOH, the following program works correctly (i.e. outputs only 5 characters, even if I type more than 5) on my Linux GPC (which has been modified somewhat after 971001) as well as on my DJGPP GPC (which is the original 971001 binary distribution):
program x; var s:string(5); begin readln(s); writeln(s) end.
Perhaps you did some other thing in your program that triggered the first bug? If not, please send me a program and sample input to show this problem.