From 42419e5a735332e075d3db599df5fd1b74a92868 Mon Sep 17 00:00:00 2001 From: Jan Edmund Lazo Date: Wed, 19 Sep 2018 21:21:45 -0400 Subject: vim-patch:8.0.1620: reading spell file has no good EOF detection Problem: Reading spell file has no good EOF detection. Solution: Check for EOF at every character read for a length field. https://github.com/vim/vim/commit/e26e0d2b83c2875b9829b884c2ababf8ca771f7e --- src/nvim/fileio.c | 71 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index d0e30ddbd3..21cb76e220 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -4531,48 +4531,83 @@ bool vim_fgets(char_u *buf, int size, FILE *fp) FUNC_ATTR_NONNULL_ALL } /// Read 2 bytes from "fd" and turn them into an int, MSB first. +/// Returns -1 when encountering EOF. int get2c(FILE *fd) { - int n; - - n = getc(fd); - n = (n << 8) + getc(fd); - return n; + const int n = getc(fd); + if (n == EOF) { + return -1; + } + const int c = getc(fd); + if (c == EOF) { + return -1; + } + return (n << 8) + c; } /// Read 3 bytes from "fd" and turn them into an int, MSB first. +/// Returns -1 when encountering EOF. int get3c(FILE *fd) { - int n; - - n = getc(fd); - n = (n << 8) + getc(fd); - n = (n << 8) + getc(fd); - return n; + int n = getc(fd); + if (n == EOF) { + return -1; + } + int c = getc(fd); + if (c == EOF) { + return -1; + } + n = (n << 8) + c; + c = getc(fd); + if (c == EOF) { + return -1; + } + return (n << 8) + c; } /// Read 4 bytes from "fd" and turn them into an int, MSB first. +/// Returns -1 when encountering EOF. int get4c(FILE *fd) { // Use unsigned rather than int otherwise result is undefined // when left-shift sets the MSB. unsigned n; - n = (unsigned)getc(fd); - n = (n << 8) + (unsigned)getc(fd); - n = (n << 8) + (unsigned)getc(fd); - n = (n << 8) + (unsigned)getc(fd); + int c = getc(fd); + if (c == EOF) { + return -1; + } + n = (unsigned)c; + c = getc(fd); + if (c == EOF) { + return -1; + } + n = (n << 8) + (unsigned)c; + c = getc(fd); + if (c == EOF) { + return -1; + } + n = (n << 8) + (unsigned)c; + c = getc(fd); + if (c == EOF) { + return -1; + } + n = (n << 8) + (unsigned)c; return (int)n; } /// Read 8 bytes from `fd` and turn them into a time_t, MSB first. +/// Returns -1 when encountering EOF. time_t get8ctime(FILE *fd) { time_t n = 0; - int i; - for (i = 0; i < 8; i++) { - n = (n << 8) + getc(fd); + for (int i = 0; i < 8; i++) { + const int c = getc(fd); + if (c == EOF) { + return -1; + } + n = (n << 8) + c; } return n; } -- cgit