]> git.proxmox.com Git - libgit2.git/blobdiff - src/odb_loose.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / src / odb_loose.c
index 5bdf884d3ac9384a1ca193b7a47134dce5bb8aca..68287795a5404338da0c346ab13e536f43722e41 100644 (file)
@@ -10,7 +10,7 @@
 #include <zlib.h>
 #include "git2/object.h"
 #include "git2/sys/odb_backend.h"
-#include "fileops.h"
+#include "futils.h"
 #include "hash.h"
 #include "odb.h"
 #include "delta.h"
@@ -304,7 +304,7 @@ static int read_loose_standard(git_rawobj *out, git_buf *obj)
         * (including the initial sequence in the head buffer).
         */
        if (GIT_ADD_SIZET_OVERFLOW(&alloc_size, hdr.size, 1) ||
-               (body = git__malloc(alloc_size)) == NULL) {
+               (body = git__calloc(1, alloc_size)) == NULL) {
                error = -1;
                goto done;
        }
@@ -386,8 +386,8 @@ static int read_header_loose_standard(
        git_rawobj *out, const unsigned char *data, size_t len)
 {
        git_zstream zs = GIT_ZSTREAM_INIT;
-       obj_hdr hdr;
-       unsigned char inflated[MAX_HEADER_LEN];
+       obj_hdr hdr = {0};
+       unsigned char inflated[MAX_HEADER_LEN] = {0};
        size_t header_len, inflated_len = sizeof(inflated);
        int error;
 
@@ -408,7 +408,8 @@ done:
 static int read_header_loose(git_rawobj *out, git_buf *loc)
 {
        unsigned char obj[1024];
-       int fd, obj_len, error;
+       ssize_t obj_len;
+       int fd, error;
 
        assert(out && loc);
 
@@ -417,9 +418,13 @@ static int read_header_loose(git_rawobj *out, git_buf *loc)
 
        out->data = NULL;
 
-       if ((error = fd = git_futils_open_ro(loc->ptr)) < 0 ||
-               (error = obj_len = p_read(fd, obj, sizeof(obj))) < 0)
+       if ((error = fd = git_futils_open_ro(loc->ptr)) < 0)
+               goto done;
+
+       if ((obj_len = p_read(fd, obj, sizeof(obj))) < 0) {
+               error = (int)obj_len;
                goto done;
+       }
 
        if (!is_zlib_compressed_data(obj, (size_t)obj_len))
                error = read_header_loose_packlike(out, obj, (size_t)obj_len);
@@ -819,7 +824,7 @@ static int filebuf_flags(loose_backend *backend)
        return flags;
 }
 
-static int loose_backend__writestream(git_odb_stream **stream_out, git_odb_backend *_backend, git_off_t length, git_object_t type)
+static int loose_backend__writestream(git_odb_stream **stream_out, git_odb_backend *_backend, git_object_size_t length, git_object_t type)
 {
        loose_backend *backend;
        loose_writestream *stream = NULL;
@@ -828,7 +833,7 @@ static int loose_backend__writestream(git_odb_stream **stream_out, git_odb_backe
        size_t hdrlen;
        int error;
 
-       assert(_backend && length >= 0);
+       assert(_backend);
 
        backend = (loose_backend *)_backend;
        *stream_out = NULL;
@@ -871,6 +876,8 @@ static int loose_backend__readstream_read(
        size_t start_remain = stream->start_len - stream->start_read;
        int total = 0, error;
 
+       buffer_len = min(buffer_len, INT_MAX);
+
        /*
         * if we read more than just the header in the initial read, play
         * that back for the caller.
@@ -882,20 +889,20 @@ static int loose_backend__readstream_read(
                buffer += chunk;
                stream->start_read += chunk;
 
-               total += chunk;
+               total += (int)chunk;
                buffer_len -= chunk;
        }
 
        if (buffer_len) {
-               size_t chunk = min(buffer_len, INT_MAX);
+               size_t chunk = buffer_len;
 
                if ((error = git_zstream_get_output(buffer, &chunk, &stream->zstream)) < 0)
                        return error;
 
-               total += chunk;
+               total += (int)chunk;
        }
 
-       return total;
+       return (int)total;
 }
 
 static void loose_backend__readstream_free(git_odb_stream *_stream)