diff options
author | Junio C Hamano <gitster@pobox.com> | 2015-05-11 14:23:59 -0700 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2015-05-11 14:23:59 -0700 |
commit | 051086b947ee7a6fdd5697889a225098f723761d (patch) | |
tree | 0920d85e5e97dad89618cf87307a3f4d0bf5abbe /sha1_file.c | |
parent | b9f5d3874f3406c820049b26fd7b10f7cae7138f (diff) | |
parent | 1427a7ff70bce0c1040831d5d674c83ebcda6d7d (diff) | |
download | git-051086b947ee7a6fdd5697889a225098f723761d.tar.gz git-051086b947ee7a6fdd5697889a225098f723761d.tar.xz |
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
Diffstat (limited to 'sha1_file.c')
-rw-r--r-- | sha1_file.c | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/sha1_file.c b/sha1_file.c index 7634917e4..866021b2b 100644 --- a/sha1_file.c +++ b/sha1_file.c @@ -3007,9 +3007,8 @@ static int freshen_packed_object(const unsigned char *sha1) return 1; } -int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *returnsha1) +int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1) { - unsigned char sha1[20]; char hdr[32]; int hdrlen; @@ -3017,13 +3016,32 @@ int write_sha1_file(const void *buf, unsigned long len, const char *type, unsign * it out into .git/objects/??/?{38} file. */ write_sha1_file_prepare(buf, len, type, sha1, hdr, &hdrlen); - if (returnsha1) - hashcpy(returnsha1, sha1); if (freshen_packed_object(sha1) || freshen_loose_object(sha1)) return 0; return write_loose_object(sha1, hdr, hdrlen, buf, len, 0); } +int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, + unsigned char *sha1, unsigned flags) +{ + char *header; + int hdrlen, status = 0; + + /* type string, SP, %lu of the length plus NUL must fit this */ + header = xmalloc(strlen(type) + 32); + write_sha1_file_prepare(buf, len, type, sha1, header, &hdrlen); + + if (!(flags & HASH_WRITE_OBJECT)) + goto cleanup; + if (freshen_packed_object(sha1) || freshen_loose_object(sha1)) + goto cleanup; + status = write_loose_object(sha1, header, hdrlen, buf, len, 0); + +cleanup: + free(header); + return status; +} + int force_object_loose(const unsigned char *sha1, time_t mtime) { void *buf; |