dj(1): fix potential skip/seek bug in non-std io

This commit is contained in:
dtb 2024-07-03 15:44:42 -06:00
parent aff658d611
commit 6548a448c7
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -133,7 +133,9 @@ Io_fdopen(struct Io *io, char *fn){
} }
/* Seeks io->seek bytes through *io's file descriptor, subtracting the number /* Seeks io->seek bytes through *io's file descriptor, subtracting the number
* of sought bytes from io->seek. This procedure leaves garbage in io->buf. */ * of sought bytes from io->seek. This procedure leaves garbage in io->buf.
* Read/written bytes here aren't counted in the statistics because successful
* seeking is guaranteed. */
static void static void
Io_fdseek(struct Io *io){ Io_fdseek(struct Io *io){
@ -143,8 +145,10 @@ Io_fdseek(struct Io *io){
assert(io->fd != STDERR_FILENO || io->fl == write_flags); assert(io->fd != STDERR_FILENO || io->fl == write_flags);
if(io->seek == 0 if(io->seek == 0
|| (!fdisstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) != -1)) || (!fdisstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) != -1)){
io->seek = 0;
return; return;
}
if(io->fl == write_flags) if(io->fl == write_flags)
memset(io->buf, '\0', io->bs); memset(io->buf, '\0', io->bs);