diff options
| author | Russell Belfer <rb@github.com> | 2013-09-09 10:17:54 -0700 |
|---|---|---|
| committer | Russell Belfer <rb@github.com> | 2013-09-09 10:17:54 -0700 |
| commit | 5fb1f9f2040df47ace275ab974b41d00883aef75 (patch) | |
| tree | f1464b2553df04c9860bfb9404c41a74b4509c0f /src/odb.c | |
| parent | ef6389ad504037e7a4311adbf14f1fa5a5aa4190 (diff) | |
| parent | 031f3f8028835c935d1e75ebd136aaaefffea821 (diff) | |
| download | libgit2-5fb1f9f2040df47ace275ab974b41d00883aef75.tar.gz | |
Merge pull request #1837 from libgit2/ntk/topic/control_stream_write_size
odb: Error when streaming in less|more bytes than declared
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)) |
