Skip to content

Commit 051086b

Browse files
committed
Merge branch 'jc/hash-object'
"hash-object --literally" introduced in v2.2 was not prepared to take a really long object type name. * jc/hash-object: write_sha1_file(): do not use a separate sha1[] array t1007: add hash-object --literally tests hash-object --literally: fix buffer overrun with extra-long object type git-hash-object.txt: document --literally option
2 parents b9f5d38 + 1427a7f commit 051086b

File tree

5 files changed

+43
-9
lines changed

5 files changed

+43
-9
lines changed

Documentation/git-hash-object.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ git-hash-object - Compute object ID and optionally creates a blob from a file
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git hash-object' [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin] [--] <file>...
12+
'git hash-object' [-t <type>] [-w] [--path=<file>|--no-filters] [--stdin [--literally]] [--] <file>...
1313
'git hash-object' [-t <type>] [-w] --stdin-paths [--no-filters] < <list-of-paths>
1414

1515
DESCRIPTION
@@ -51,7 +51,13 @@ OPTIONS
5151
Hash the contents as is, ignoring any input filter that would
5252
have been chosen by the attributes mechanism, including the end-of-line
5353
conversion. If the file is read from standard input then this
54-
is always implied, unless the --path option is given.
54+
is always implied, unless the `--path` option is given.
55+
56+
--literally::
57+
Allow `--stdin` to hash any garbage into a loose object which might not
58+
otherwise pass standard object parsing or git-fsck checks. Useful for
59+
stress-testing Git itself or reproducing characteristics of corrupt or
60+
bogus objects encountered in the wild.
5561

5662
GIT
5763
---

builtin/hash-object.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ static int hash_literally(unsigned char *sha1, int fd, const char *type, unsigne
2222

2323
if (strbuf_read(&buf, fd, 4096) < 0)
2424
ret = -1;
25-
else if (flags & HASH_WRITE_OBJECT)
26-
ret = write_sha1_file(buf.buf, buf.len, type, sha1);
2725
else
28-
ret = hash_sha1_file(buf.buf, buf.len, type, sha1);
26+
ret = hash_sha1_file_literally(buf.buf, buf.len, type, sha1, flags);
2927
strbuf_release(&buf);
3028
return ret;
3129
}

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ static inline const unsigned char *lookup_replace_object_extended(const unsigned
909909
extern int sha1_object_info(const unsigned char *, unsigned long *);
910910
extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);
911911
extern int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *return_sha1);
912+
extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, unsigned char *sha1, unsigned flags);
912913
extern int pretend_sha1_file(void *, unsigned long, enum object_type, unsigned char *);
913914
extern int force_object_loose(const unsigned char *sha1, time_t mtime);
914915
extern int git_open_noatime(const char *name);

sha1_file.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3007,23 +3007,41 @@ static int freshen_packed_object(const unsigned char *sha1)
30073007
return 1;
30083008
}
30093009

3010-
int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
3010+
int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1)
30113011
{
3012-
unsigned char sha1[20];
30133012
char hdr[32];
30143013
int hdrlen;
30153014

30163015
/* Normally if we have it in the pack then we do not bother writing
30173016
* it out into .git/objects/??/?{38} file.
30183017
*/
30193018
write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen);
3020-
if (returnsha1)
3021-
hashcpy(returnsha1, sha1);
30223019
if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
30233020
return 0;
30243021
return write_loose_object(sha1, hdr, hdrlen, buf, len, 0);
30253022
}
30263023

3024+
int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type,
3025+
unsigned char *sha1, unsigned flags)
3026+
{
3027+
char *header;
3028+
int hdrlen, status = 0;
3029+
3030+
/* type string, SP, %lu of the length plus NUL must fit this */
3031+
header = xmalloc(strlen(type) + 32);
3032+
write_sha1_file_prepare(buf, len, type, sha1, header, &hdrlen);
3033+
3034+
if (!(flags & HASH_WRITE_OBJECT))
3035+
goto cleanup;
3036+
if (freshen_packed_object(sha1) || freshen_loose_object(sha1))
3037+
goto cleanup;
3038+
status = write_loose_object(sha1, header, hdrlen, buf, len, 0);
3039+
3040+
cleanup:
3041+
free(header);
3042+
return status;
3043+
}
3044+
30273045
int force_object_loose(const unsigned char *sha1, time_t mtime)
30283046
{
30293047
void *buf;

t/t1007-hash-object.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,15 @@ test_expect_success 'hash-object complains about truncated type name' '
209209
test_must_fail git hash-object -t bl --stdin </dev/null
210210
'
211211

212+
test_expect_success '--literally' '
213+
t=1234567890 &&
214+
echo example | git hash-object -t $t --literally --stdin
215+
'
216+
217+
test_expect_success '--literally with extra-long type' '
218+
t=12345678901234567890123456789012345678901234567890 &&
219+
t="$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t" &&
220+
echo example | git hash-object -t $t --literally --stdin
221+
'
222+
212223
test_done

0 commit comments

Comments
 (0)