diff options
author | Jan Edmund Lazo <janedmundlazo@hotmail.com> | 2018-09-19 21:21:45 -0400 |
---|---|---|
committer | Jan Edmund Lazo <janedmundlazo@hotmail.com> | 2018-11-12 08:45:03 -0500 |
commit | 42419e5a735332e075d3db599df5fd1b74a92868 (patch) | |
tree | a71a4763ec820a0e3ff05c651f7d4b8da80bc7ab | |
parent | e5046822c96ecfd7c774bf3ab025caf5aebd06d8 (diff) | |
download | rneovim-42419e5a735332e075d3db599df5fd1b74a92868.tar.gz rneovim-42419e5a735332e075d3db599df5fd1b74a92868.tar.bz2 rneovim-42419e5a735332e075d3db599df5fd1b74a92868.zip |
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
-rw-r--r-- | src/nvim/fileio.c | 71 |
1 files 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; } |