summaryrefslogtreecommitdiff
path: root/src/netops.c
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2019-05-21 10:57:30 +0100
committerEdward Thomson <ethomson@edwardthomson.com>2019-06-24 15:00:41 +0100
commitf75974105db108150892a743c69e8adbf23bb14f (patch)
tree8af11dc9c547731d4c87437489ca0145fc47ebad /src/netops.c
parentcfd44d6a33acef7bb4115baf27cf39d8fb9db6b8 (diff)
downloadlibgit2-f75974105db108150892a743c69e8adbf23bb14f.tar.gz
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.
Diffstat (limited to 'src/netops.c')
-rw-r--r--src/netops.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/netops.c b/src/netops.c
index 708f694e3..f19bae9de 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)