Commit f75974105db108150892a743c69e8adbf23bb14f

Edward Thomson 2019-05-21T10:57:30

netops: safely cast to int Only read at most INT_MAX from the underlying stream, so that we can accurately return the number of bytes read. Since callers are not guaranteed to get as many bytes as requested (due to availability of input), this is safe and callers should call in a loop until EOF.

diff --git a/src/netops.c b/src/netops.c
index 708f694..f19bae9 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -37,14 +37,17 @@ void gitno_buffer_setup_callback(
 static int recv_stream(gitno_buffer *buf)
 {
 	git_stream *io = (git_stream *) buf->cb_data;
-	int ret;
+	size_t readlen = buf->len - buf->offset;
+	ssize_t ret;
 
-	ret = git_stream_read(io, buf->data + buf->offset, buf->len - buf->offset);
+	readlen = min(readlen, INT_MAX);
+
+	ret = git_stream_read(io, buf->data + buf->offset, (int)readlen);
 	if (ret < 0)
 		return -1;
 
 	buf->offset += ret;
-	return ret;
+	return (int)ret;
 }
 
 void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data, size_t len)