1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 09:27:59 +00:00

Fix tight loop on reading truncated key files.

In commit 9cc586e605 I changed the low-level key-file reading
routines like read_header and read_body so that they read from a
BinarySource via get_byte(), rather than from a FILE * via fgetc. But
I forgot that the two functions don't signal end-of-file the same way,
so testing the return value of get_byte() against EOF is pointless and
will never match, and conversely, real EOF won't be spotted unless you
also examine the error indicator in the BinarySource.

As a result, a key file that ends without a trailing newline will
cause a tight loop in one of those low-level read routines.

(cherry picked from commit d008d235f3)
This commit is contained in:
Simon Tatham 2021-05-23 08:59:13 +01:00
parent ff53c6716a
commit fd3f05d215

View File

@ -480,7 +480,7 @@ static bool read_header(BinarySource *src, char *header)
while (1) { while (1) {
c = get_byte(src); c = get_byte(src);
if (c == '\n' || c == '\r' || c == EOF) if (c == '\n' || c == '\r' || get_err(src))
return false; /* failure */ return false; /* failure */
if (c == ':') { if (c == ':') {
c = get_byte(src); c = get_byte(src);
@ -503,10 +503,10 @@ static char *read_body(BinarySource *src)
while (1) { while (1) {
int c = get_byte(src); int c = get_byte(src);
if (c == '\r' || c == '\n' || c == EOF) { if (c == '\r' || c == '\n' || get_err(src)) {
if (c != EOF) { if (!get_err(src)) {
c = get_byte(src); c = get_byte(src);
if (c != '\r' && c != '\n') if (c != '\r' && c != '\n' && !get_err(src))
src->pos--; src->pos--;
} }
return strbuf_to_str(buf); return strbuf_to_str(buf);