s->z.avail_out = zlib_buf_cap(s->avail_out);
}
-static void zlib_post_call(git_zstream *s)
+static void zlib_post_call(git_zstream *s, int status)
{
unsigned long bytes_consumed;
unsigned long bytes_produced;
bytes_produced = s->z.next_out - s->next_out;
if (s->z.total_out != s->total_out + bytes_produced)
BUG("total_out mismatch");
- if (s->z.total_in != s->total_in + bytes_consumed)
+ /*
+ * zlib does not update total_in when it returns Z_NEED_DICT,
+ * causing a mismatch here. Skip the sanity check in that case.
+ */
+ if (status != Z_NEED_DICT &&
+ s->z.total_in != s->total_in + bytes_consumed)
BUG("total_in mismatch");
s->total_out = s->z.total_out;
zlib_pre_call(strm);
status = inflateInit(&strm->z);
- zlib_post_call(strm);
+ zlib_post_call(strm, status);
if (status == Z_OK)
return;
die("inflateInit: %s (%s)", zerr_to_string(status),
zlib_pre_call(strm);
status = inflateInit2(&strm->z, windowBits);
- zlib_post_call(strm);
+ zlib_post_call(strm, status);
if (status == Z_OK)
return;
die("inflateInit2: %s (%s)", zerr_to_string(status),
zlib_pre_call(strm);
status = inflateEnd(&strm->z);
- zlib_post_call(strm);
+ zlib_post_call(strm, status);
if (status == Z_OK)
return;
error("inflateEnd: %s (%s)", zerr_to_string(status),
? 0 : flush);
if (status == Z_MEM_ERROR)
die("inflate: out of memory");
- zlib_post_call(strm);
+ zlib_post_call(strm, status);
/*
* Let zlib work another round, while we can still
memset(strm, 0, sizeof(*strm));
zlib_pre_call(strm);
status = deflateInit(&strm->z, level);
- zlib_post_call(strm);
+ zlib_post_call(strm, status);
if (status == Z_OK)
return;
die("deflateInit: %s (%s)", zerr_to_string(status),
status = deflateInit2(&strm->z, level,
Z_DEFLATED, windowBits,
8, Z_DEFAULT_STRATEGY);
- zlib_post_call(strm);
+ zlib_post_call(strm, status);
if (status == Z_OK)
return;
die("deflateInit2: %s (%s)", zerr_to_string(status),
zlib_pre_call(strm);
status = deflateEnd(&strm->z);
- zlib_post_call(strm);
+ zlib_post_call(strm, status);
return status;
}
zlib_pre_call(strm);
status = deflateEnd(&strm->z);
- zlib_post_call(strm);
+ zlib_post_call(strm, status);
return status;
}
? 0 : flush);
if (status == Z_MEM_ERROR)
die("deflate: out of memory");
- zlib_post_call(strm);
+ zlib_post_call(strm, status);
/*
* Let zlib work another round, while we can still
test_grep "unable to unpack $blob header" err
EOT
+test_expect_success 'object reading handles zlib dictionary' - <<\EOT
+ echo 'content that will be recompressed' >file &&
+ blob=$(git hash-object -w file) &&
+ objpath=.git/objects/$(test_oid_to_path "$blob") &&
+
+ # Recompress a loose object using a precomputed zlib dictionary.
+ # This was originally done with:
+ #
+ # perl -MCompress::Raw::Zlib -e '
+ # binmode STDIN;
+ # binmode STDOUT;
+ # my $data = do { local $/; <STDIN> };
+ # my $in = new Compress::Raw::Zlib::Inflate;
+ # my $de = new Compress::Raw::Zlib::Deflate(
+ # -Dictionary => "anything"
+ # );
+ # $in->inflate($data, $raw);
+ # $de->deflate($raw, $out);
+ # print $out;
+ # ' <obj.bak >$objpath
+ #
+ # but we do not want to require the perl module for all test runs (nor
+ # carry a custom t/helper program that uses zlib features we don't
+ # otherwise care about).
+ mv "$objpath" obj.bak &&
+ test_when_finished 'mv obj.bak "$objpath"' &&
+ printf '\170\273\017\112\003\143' >$objpath &&
+
+ test_must_fail git cat-file blob $blob 2>err &&
+ test_grep 'error: inflate: needs dictionary' err
+EOT
+
# Tests for git cat-file --follow-symlinks
test_expect_success 'prep for symlink tests' '
echo_without_newline "$hello_content" >morx &&