[v2] avoid writing two iov in __stdio_write backend when not needed

Submitted by Jouni Roivas on Oct. 12, 2020, 4:43 p.m.

Details

Message ID 20201012164342.2987876-1-jouni.roivas@tuxera.com
State New
Series "Skip writing first iovec if it's empty"
Headers show

Commit Message

Jouni Roivas Oct. 12, 2020, 4:43 p.m.
formally, calling writev with a zero-length first iov component should
behave identically to calling write on just the second component, but
presence of a zero-length iov component has triggered bugs in some
kernels.
---
 src/stdio/__stdio_write.c | 4 ++++
 1 file changed, 4 insertions(+)

Patch hide | download patch | download mbox

diff --git a/src/stdio/__stdio_write.c b/src/stdio/__stdio_write.c
index d2d89475..eedce03a 100644
--- a/src/stdio/__stdio_write.c
+++ b/src/stdio/__stdio_write.c
@@ -11,6 +11,10 @@  size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
 	size_t rem = iov[0].iov_len + iov[1].iov_len;
 	int iovcnt = 2;
 	ssize_t cnt;
+	if (iov[0].iov_len == 0) {
+		iov++;
+		iovcnt--;
+	}
 	for (;;) {
 		cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
 		if (cnt == rem) {