From f33b1a4a966e612a563ef761fdb55ef25e67136b Mon Sep 17 00:00:00 2001 From: "Dmitrij D. Czarkoff" Date: Sun, 9 Oct 2016 03:14:37 +0200 Subject: Be more correct in read_from_stdin() From read(2) manual page on OpenBSD: Error checks should explicitly test for -1. Code such as while ((nr = read(fd, buf, sizeof(buf))) > 0) is not maximally portable, as some platforms allow for nbytes to range between SSIZE_MAX and SIZE_MAX - 2, in which case the return value of an error-free read() may appear as a negative number distinct from -1. Proper loops should use while ((nr = read(fd, buf, sizeof(buf))) != -1 && nr != 0) Distingushing between error and eof would also help for debugging. --- src/util.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/util.c') diff --git a/src/util.c b/src/util.c index 6d10c00..9294f51 100644 --- a/src/util.c +++ b/src/util.c @@ -31,7 +31,10 @@ size_t read_from_stdin(void **buffer) { for(*buffer = NULL; (*buffer = realloc((p = *buffer), len + step)); len += (size_t)r) { - if((r = read(STDIN_FILENO, (uint8_t *)*buffer + len, step)) <= 0) { + if((r = read(STDIN_FILENO, (uint8_t *)*buffer + len, step)) == -1) { + perror(NULL); + break; + } else if (r == 0) { break; } } -- cgit v1.2.3