aboutsummaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorDmitrij D. Czarkoff <czarkoff@gmail.com>2016-10-09 03:14:37 +0200
committerDmitrij D. Czarkoff <czarkoff@gmail.com>2016-10-09 03:14:37 +0200
commitf33b1a4a966e612a563ef761fdb55ef25e67136b (patch)
treeff3a8c53ad5d9065e65778eceb37a3c84066060c /src/util.c
parente4ded04c39a2ceca64d390b7aa998e7537cf6a2c (diff)
downloadimv-f33b1a4a966e612a563ef761fdb55ef25e67136b.tar.gz
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.
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c5
1 files changed, 4 insertions, 1 deletions
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;
}
}