robhagemans / pcbasic

PC-BASIC - A free, cross-platform emulator for the GW-BASIC family of interpreters
http://www.pc-basic.org
Other
396 stars 48 forks source link

Strange memory behavior when copying strings from file to array #106

Closed yipal closed 3 years ago

yipal commented 4 years ago

Bug report

Problem I suspect there is a problem with how string memory is being handled when copying from a random access file to an array. What I observe is that when reading multiple records from a random access file, and copying the values to an array, the array content always equals the last value read from the file. I've included a sample program below.

Steps

  1. Run the following program. I expect the last output line to be "A B", but instead it is "B B"

Program

10 OPEN "R",#1,"foo.dat",10
20 FIELD#1,10 AS W$
30 LSET W$="A"
40 PUT#1,1
50 LSET W$="B"
60 PUT#1,2
70 CLOSE
100 REM Read the file
105 DIM X$(2)
110 OPEN "R",#1,"foo.dat",10
120 FIELD#1,10 AS R$
130 GET#1,1
135 X$(1)=R$
140 PRINT R$
150 GET#1,2
155 X$(2)=R$
160 PRINT R$
170 PRINT X$(1);X$(2)

PC-BASIC version: 2.0.2 Operating system version: Linux

Any thoughts? Thanks for writing pcbasic by the way!

yipal commented 4 years ago

This behavior is the same even if I replace the array with two string variables.

I looked for gwbasic documentation, but I could not find a description of how string assignment is supposed to work. I do know that I have an old gwbasic program that seems to assume the strings are copied by value, and not by reference.

robhagemans commented 4 years ago

Hi, thank you for reporting this and for the sample code. This is indeed a regression in version 2.0.2 - the 1.2 branch produces the correct A B line which is also what GW-BASIC produces.

Looks like the string assignment incorrectly copies the string pointer to the FIELD buffer, where it should copy the string by value.

robhagemans commented 3 years ago

Fixed on develop branch