Thanks Xusinboy.
- file must have the UTF-8 BOM or OPEN will fail
- encoding "utf8" and "utf-8" specifiers are both valid, the manual doesn't mention that.
The change from 1.06 to 1.07 is due to:
changelog.txt wrote:- github #145: WSTRING concat and assign buffer (&=) overrun
I didn't notice at the time it affected LINE INPUT for WSTRING. Internally, LINE INPUT uses wstring concat and assign to read the input from the file.
fbc and fb run-time lib have no concept of variable length wstrings. fbc tries to know the max buffer length from the variable type, for example 'dim s as WSTRING * N' and passes that to the max number of characters to run-time library functions to limit the buffer length. There were some places in the run-time lib where the maximum characters were unknown, as in the case of passing a wstring ptr buffer, in which case the maximum chars were ignored, leading to a possible buffer overrun.
The work around is as follows:
Code: Select all
extern "rtlib"
declare function LineInputWstr alias "fb_FileLineInputWstr"_
( _
byval filenumber as long, _
byval dst as wstring ptr, _
byval maxchars as integer _
) as long
end extern
If Open("file.txt" For Input Encoding "utf-8" As #3) = 0 Then
Dim As WString Ptr buff
dim buffAnsi as string
buff = reallocate( buff, (lof(3) + 1) * sizeof(wstring) )
Do Until EOF(3)
LineInputWstr 3, buff, lof(3)
buffAnsi = *buff
print buffAnsi
print *buff
Loop
deallocate buff
else
print "error"
End If
If the file contains many many lines, allocating a buffer of LOF(3) is probably too much. Better will be when fb supports var-len wstring internally.