diff options
author | nulltoken <emeric.fermas@gmail.com> | 2013-09-08 00:39:05 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-09-08 01:00:20 +0400 |
commit | 031f3f8028835c935d1e75ebd136aaaefffea821 (patch) | |
tree | f1464b2553df04c9860bfb9404c41a74b4509c0f /src/odb.c | |
parent | ef6389ad504037e7a4311adbf14f1fa5a5aa4190 (diff) |
odb: Error when streaming in too [few|many] bytes
Diffstat (limited to 'src/odb.c')
-rw-r--r-- | src/odb.c | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -888,17 +888,44 @@ int git_odb_open_wstream( hash_header(ctx, size, type); (*stream)->hash_ctx = ctx; + (*stream)->declared_size = size; + (*stream)->received_bytes = 0; + return error; } +static int git_odb_stream__invalid_length( + const git_odb_stream *stream, + const char *action) +{ + giterr_set(GITERR_ODB, + "Cannot %s - " + "Invalid length. %"PRIuZ" was expected. The " + "total size of the received chunks amounts to %"PRIuZ".", + action, stream->declared_size, stream->received_bytes); + + return -1; +} + int git_odb_stream_write(git_odb_stream *stream, const char *buffer, size_t len) { git_hash_update(stream->hash_ctx, buffer, len); + + stream->received_bytes += len; + + if (stream->received_bytes > stream->declared_size) + return git_odb_stream__invalid_length(stream, + "stream_write()"); + return stream->write(stream, buffer, len); } int git_odb_stream_finalize_write(git_oid *out, git_odb_stream *stream) { + if (stream->received_bytes != stream->declared_size) + return git_odb_stream__invalid_length(stream, + "stream_finalize_write()"); + git_hash_final(out, stream->hash_ctx); if (git_odb_exists(stream->backend->odb, out)) |