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.
e26e0d2b83
This commit is contained in:
Jan Edmund Lazo
2018-09-19 21:21:45 -04:00
parent e5046822c9
commit 42419e5a73

View File

@@ -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. /// Read 2 bytes from "fd" and turn them into an int, MSB first.
/// Returns -1 when encountering EOF.
int get2c(FILE *fd) int get2c(FILE *fd)
{ {
int n; const int n = getc(fd);
if (n == EOF) {
n = getc(fd); return -1;
n = (n << 8) + getc(fd); }
return n; 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. /// Read 3 bytes from "fd" and turn them into an int, MSB first.
/// Returns -1 when encountering EOF.
int get3c(FILE *fd) int get3c(FILE *fd)
{ {
int n; int n = getc(fd);
if (n == EOF) {
n = getc(fd); return -1;
n = (n << 8) + getc(fd); }
n = (n << 8) + getc(fd); int c = getc(fd);
return n; 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. /// Read 4 bytes from "fd" and turn them into an int, MSB first.
/// Returns -1 when encountering EOF.
int get4c(FILE *fd) int get4c(FILE *fd)
{ {
// Use unsigned rather than int otherwise result is undefined // Use unsigned rather than int otherwise result is undefined
// when left-shift sets the MSB. // when left-shift sets the MSB.
unsigned n; unsigned n;
n = (unsigned)getc(fd); int c = getc(fd);
n = (n << 8) + (unsigned)getc(fd); if (c == EOF) {
n = (n << 8) + (unsigned)getc(fd); return -1;
n = (n << 8) + (unsigned)getc(fd); }
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; return (int)n;
} }
/// Read 8 bytes from `fd` and turn them into a time_t, MSB first. /// 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 get8ctime(FILE *fd)
{ {
time_t n = 0; time_t n = 0;
int i;
for (i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
n = (n << 8) + getc(fd); const int c = getc(fd);
if (c == EOF) {
return -1;
}
n = (n << 8) + c;
} }
return n; return n;
} }