Avoid errors caused by fseek after ungetc on read-only files.

The error could occur because fseek calls fflush followed by ftell. fflush would reset the file position as if the characters in the putback buffer were removed, but ftell would still see them and try to adjust for them (in the case of a read-only file). This could result in trying to seek before the beginning of the file, producing an error.

Here is a program that was affected:

#include <stdio.h>
int main(void) {
        FILE *f = fopen("somefile","r");
        if (!f) return 0;
        fgetc(f);
        ungetc('X', f);
        fseek(f, 0, SEEK_CUR);
        if (ferror(f)) puts("error encountered");
}
This commit is contained in:
Stephen Heumann 2022-07-03 21:58:00 -05:00
parent 219e4352a0
commit 463d24a028
1 changed files with 5 additions and 0 deletions

View File

@ -4623,6 +4623,11 @@ ib2 ldy #FILE_cnt+2
dey
txa
sta [stream],Y
ldy #FILE_pbk nothing in the putback buffer
lda #$FFFF
sta [stream],Y
ldy #FILE_pbk+2
sta [stream],Y
creturn
end