Skip to content

Commit 18ad13e

Browse files
committed
Adjust for 2.19.x series
* jk/detect-truncated-zlib-input cat-file: handle streaming failures consistently check_stream_sha1(): handle input underflow t1450: check large blob in trailing-garbage test
2 parents cae598d + 98f425b commit 18ad13e

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

builtin/cat-file.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ static int filter_object(const char *path, unsigned mode,
5050
return 0;
5151
}
5252

53+
static int stream_blob(const struct object_id *oid)
54+
{
55+
if (stream_blob_to_fd(1, oid, NULL, 0))
56+
die("unable to stream %s to stdout", oid_to_hex(oid));
57+
return 0;
58+
}
59+
5360
static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
5461
int unknown_type)
5562
{
@@ -131,7 +138,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
131138
}
132139

133140
if (type == OBJ_BLOB)
134-
return stream_blob_to_fd(1, &oid, NULL, 0);
141+
return stream_blob(&oid);
135142
buf = read_object_file(&oid, &type, &size);
136143
if (!buf)
137144
die("Cannot read object %s", obj_name);
@@ -154,7 +161,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
154161
oidcpy(&blob_oid, &oid);
155162

156163
if (oid_object_info(the_repository, &blob_oid, NULL) == OBJ_BLOB)
157-
return stream_blob_to_fd(1, &blob_oid, NULL, 0);
164+
return stream_blob(&blob_oid);
158165
/*
159166
* we attempted to dereference a tag to a blob
160167
* and failed; there may be new dereference
@@ -317,8 +324,9 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d
317324
BUG("invalid cmdmode: %c", opt->cmdmode);
318325
batch_write(opt, contents, size);
319326
free(contents);
320-
} else if (stream_blob_to_fd(1, oid, NULL, 0) < 0)
321-
die("unable to stream %s to stdout", oid_to_hex(oid));
327+
} else {
328+
stream_blob(oid);
329+
}
322330
}
323331
else {
324332
enum object_type type;

sha1-file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2191,7 +2191,8 @@ static int check_stream_sha1(git_zstream *stream,
21912191
* see the comment in unpack_sha1_rest for details.
21922192
*/
21932193
while (total_read <= size &&
2194-
(status == Z_OK || status == Z_BUF_ERROR)) {
2194+
(status == Z_OK ||
2195+
(status == Z_BUF_ERROR && !stream->avail_out))) {
21952196
stream->next_out = buf;
21962197
stream->avail_out = sizeof(buf);
21972198
if (size - total_read < stream->avail_out)

t/t1450-fsck.sh

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,16 +673,35 @@ test_expect_success 'fsck detects trailing loose garbage (commit)' '
673673
test_i18ngrep "garbage.*$commit" out
674674
'
675675

676-
test_expect_success 'fsck detects trailing loose garbage (blob)' '
676+
test_expect_success 'fsck detects trailing loose garbage (large blob)' '
677677
blob=$(echo trailing | git hash-object -w --stdin) &&
678678
file=$(sha1_file $blob) &&
679679
test_when_finished "remove_object $blob" &&
680680
chmod +w "$file" &&
681681
echo garbage >>"$file" &&
682-
test_must_fail git fsck 2>out &&
682+
test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
683683
test_i18ngrep "garbage.*$blob" out
684684
'
685685

686+
test_expect_success 'fsck detects truncated loose object' '
687+
# make it big enough that we know we will truncate in the data
688+
# portion, not the header
689+
test-tool genrandom truncate 4096 >file &&
690+
blob=$(git hash-object -w file) &&
691+
file=$(sha1_file $blob) &&
692+
test_when_finished "remove_object $blob" &&
693+
test_copy_bytes 1024 <"$file" >tmp &&
694+
rm "$file" &&
695+
mv -f tmp "$file" &&
696+
697+
# check both regular and streaming code paths
698+
test_must_fail git fsck 2>out &&
699+
test_i18ngrep corrupt.*$blob out &&
700+
701+
test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
702+
test_i18ngrep corrupt.*$blob out
703+
'
704+
686705
# for each of type, we have one version which is referenced by another object
687706
# (and so while unreachable, not dangling), and another variant which really is
688707
# dangling.

0 commit comments

Comments
 (0)