When 0 bytes are to be written, make sure to go into read() at least
once, in order to validate the parameters, such as the passed fd.
Return error on huge values, add a couple of asserts and casts where
appropriate.
assert(fd >= 0);
assert(buf);
assert(fd >= 0);
assert(buf);
+ /* If called with nbytes == 0, let's call read() at least
+ * once, to validate the operation */
+
+ if (nbytes > (size_t) SSIZE_MAX)
+ return -EINVAL;
+
+ do {
ssize_t k;
k = read(fd, p, nbytes);
ssize_t k;
k = read(fd, p, nbytes);
* and expect that any error/EOF is reported
* via read() */
* and expect that any error/EOF is reported
* via read() */
- fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
+ (void) fd_wait_for_event(fd, POLLIN, USEC_INFINITY);
+ assert((size_t) k <= nbytes);
+
p += k;
nbytes -= k;
n += k;
p += k;
nbytes -= k;
n += k;
n = loop_read(fd, buf, nbytes, do_poll);
if (n < 0)
n = loop_read(fd, buf, nbytes, do_poll);
if (n < 0)
if ((size_t) n != nbytes)
return -EIO;
if ((size_t) n != nbytes)
return -EIO;
assert(fd >= 0);
assert(buf);
assert(fd >= 0);
assert(buf);
+ if (nbytes > (size_t) SSIZE_MAX)
+ return -EINVAL;
* and expect that any error/EOF is reported
* via write() */
* and expect that any error/EOF is reported
* via write() */
- fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
+ (void) fd_wait_for_event(fd, POLLOUT, USEC_INFINITY);
continue;
}
return -errno;
}
continue;
}
return -errno;
}
- if (nbytes > 0 && k == 0) /* Can't really happen */
+ if (_unlikely_(nbytes > 0 && k == 0)) /* Can't really happen */
+ assert((size_t) k <= nbytes);
+
p += k;
nbytes -= k;
} while (nbytes > 0);
p += k;
nbytes -= k;
} while (nbytes > 0);