Closed PertinentDetail closed 2 years ago
Good catch. Very helpful analysis.
I have the fix written and done some interactive testing. I'm hesitating to push yet because I want to add a test to data/test.ps
but I'm not seeing a nice way to test the EOF behavior just using currentfile
so it'll need a few extra files. Not really a problem but that's where I stopped to think before doing anything further.
Fixed. Tests added to data/test.ps
and data/readstring.ps
.
readstring
effectively boils down to a call toxpost_file_read()
andxpost_file_read()
doesn't do anything special when it reaches end of file. The function is:This is guaranteed to return
count
.There needs to be a test on whether
xpost_file_getc()
has returned EOF, and, if it has, the code needs to break out of thei
loop. Note that this will need to test the return value ofxpost_file_getc()
before it's written intobuf[]
, for the usual getc-returns-an-int reasons.A simple test input is:
This should return substring
(abc\n)
(could be "\n", "\r\n" or nothing at the end depending exactly how the file terminates) andfalse
. xpost returns a ten character string padded with character 255 andtrue
Here's are TIO links for the simple test: xpost (failing) and Ghostscript (working).
A stronger test is:
Currently this will give an infinite loop with xpost.
With this bug fixed, it's not clear what
xpost_file_read()
should do if the number of bytes remaining in the file is not a multiple ofsize
(assuming it's also less thancount
×size
). It will read all the remaining bytes and return the number of whole items it read. This will imply it readi
×size
bytes when it actually read up tosize
− 1 more bytes. This is probably correct behaviour, but at least should be documented in the code.While I'm here, in
xpost_op_file.c
, the comments documenting the inputs and outputs ofreadhexstring
,readstring
andreadline
are wrong. They all imply that if the functions hit end of file then they return justfalse
without a substring. In fact, if they always return a substring. The code is correct in all three cases. (Note thatread
has a similar comment, but in that case it's correct, as is the code).