Commit 7613086df460948f728e9f0534457c8af6fa522b

Patrick Steinhardt 2019-01-23T15:49:28

streams: handle short writes only in generic stream Now that the function `git_stream__write_full` exists and callers of `git_stream_write` have been adjusted, we can lift logic for short writes out of the stream implementations. Instead, this is now handled either by `git_stream__write_full` or by callers of `git_stream_write` directly.

diff --git a/src/streams/mbedtls.c b/src/streams/mbedtls.c
index 7ca3aa6..3a440e3 100644
--- a/src/streams/mbedtls.c
+++ b/src/streams/mbedtls.c
@@ -303,20 +303,15 @@ static int mbedtls_set_proxy(git_stream *stream, const git_proxy_options *proxy_
 	return git_stream_set_proxy(st->io, proxy_options);
 }
 
-static ssize_t mbedtls_stream_write(git_stream *stream, const char *data, size_t data_len, int flags)
+static ssize_t mbedtls_stream_write(git_stream *stream, const char *data, size_t len, int flags)
 {
-	ssize_t written = 0, len = min(data_len, SSIZE_MAX);
 	mbedtls_stream *st = (mbedtls_stream *) stream;
+	int written;
 
 	GIT_UNUSED(flags);
 
-	do {
-		int error = mbedtls_ssl_write(st->ssl, (const unsigned char *)data + written, len - written);
-		if (error <= 0) {
-			return ssl_set_error(st->ssl, error);
-		}
-		written += error;
-	} while (written < len);
+	if ((written = mbedtls_ssl_write(st->ssl, (const unsigned char *)data, len)) <= 0)
+		return ssl_set_error(st->ssl, written);
 
 	return written;
 }
diff --git a/src/streams/socket.c b/src/streams/socket.c
index 7134db4..066580f 100644
--- a/src/streams/socket.c
+++ b/src/streams/socket.c
@@ -130,23 +130,19 @@ static int socket_connect(git_stream *stream)
 	return 0;
 }
 
-static ssize_t socket_write(git_stream *stream, const char *data, size_t data_len, int flags)
+static ssize_t socket_write(git_stream *stream, const char *data, size_t len, int flags)
 {
-	ssize_t ret, off = 0, len = min(data_len, SSIZE_MAX);
 	git_socket_stream *st = (git_socket_stream *) stream;
+	ssize_t written;
 
-	while (off < len) {
-		errno = 0;
-		ret = p_send(st->s, data + off, len - off, flags);
-		if (ret < 0) {
-			net_set_error("Error sending data");
-			return -1;
-		}
+	errno = 0;
 
-		off += ret;
+	if ((written = p_send(st->s, data, len, flags)) < 0) {
+		net_set_error("Error sending data");
+		return -1;
 	}
 
-	return off;
+	return written;
 }
 
 static ssize_t socket_read(git_stream *stream, void *data, size_t len)