sha1_file: introduce a constant for max header length

There were several instances of 32 sprinkled throughout this file, all of which were used for allocating a buffer to store the header of an object. Introduce a constant, MAX_HEADER_LEN, for this purpose. Note that this constant is slightly larger than required; the longest possible header is 28 (7 for "commit", 1 for a space, 20 for a 63-bit length in decimal, and 1 for the NUL). However, the overallocation should not cause any problems, so leave it as it is. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Mar 12, 2018 at 02:27 UTC 1af64f73a9113b189ce4cc3e422ec927e9b32084
1 file changed +10 -7
sha1_file.c
+10 -7
@@ -30,6 +30,9 @@
30 #include "packfile.h"
31 #include "fetch-object.h"
32
33 +/* The maximum size for an object header. */
34 +#define MAX_HEADER_LEN 32
35 +
36 const unsigned char null_sha1[GIT_MAX_RAWSZ];
37 const struct object_id null_oid;
38 const struct object_id empty_tree_oid = {
@@ -791,7 +794,7 @@ int check_object_signature(const struct object_id *oid, void *map,
794 enum object_type obj_type;
795 struct git_istream *st;
796 git_hash_ctx c;
794 - char hdr[32];
797 + char hdr[MAX_HEADER_LEN];
798 int hdrlen;
799
800 if (map) {
@@ -1150,7 +1153,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,
1153 unsigned long mapsize;
1154 void *map;
1155 git_zstream stream;
1153 - char hdr[32];
1156 + char hdr[MAX_HEADER_LEN];
1157 struct strbuf hdrbuf = STRBUF_INIT;
1158 unsigned long size_scratch;
1159
@@ -1514,7 +1517,7 @@ static int write_buffer(int fd, const void *buf, size_t len)
1517 int hash_object_file(const void *buf, unsigned long len, const char *type,
1518 struct object_id *oid)
1519 {
1517 - char hdr[32];
1520 + char hdr[MAX_HEADER_LEN];
1521 int hdrlen = sizeof(hdr);
1522 write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
1523 return 0;
@@ -1669,7 +1672,7 @@ static int freshen_packed_object(const unsigned char *sha1)
1672 int write_object_file(const void *buf, unsigned long len, const char *type,
1673 struct object_id *oid)
1674 {
1672 - char hdr[32];
1675 + char hdr[MAX_HEADER_LEN];
1676 int hdrlen = sizeof(hdr);
1677
1678 /* Normally if we have it in the pack then we do not bother writing
@@ -1689,7 +1692,7 @@ int hash_object_file_literally(const void *buf, unsigned long len,
1692 int hdrlen, status = 0;
1693
1694 /* type string, SP, %lu of the length plus NUL must fit this */
1692 - hdrlen = strlen(type) + 32;
1695 + hdrlen = strlen(type) + MAX_HEADER_LEN;
1696 header = xmalloc(hdrlen);
1697 write_object_file_prepare(buf, len, type, oid, header, &hdrlen);
1698
@@ -1709,7 +1712,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
1712 void *buf;
1713 unsigned long len;
1714 enum object_type type;
1712 - char hdr[32];
1715 + char hdr[MAX_HEADER_LEN];
1716 int hdrlen;
1717 int ret;
1718
@@ -2191,7 +2194,7 @@ int read_loose_object(const char *path,
2194 void *map = NULL;
2195 unsigned long mapsize;
2196 git_zstream stream;
2194 - char hdr[32];
2197 + char hdr[MAX_HEADER_LEN];
2198
2199 *contents = NULL;
2200