Skip to content

Commit 1af64f7

Browse files
bk2204gitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b383a13 commit 1af64f7

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

sha1_file.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include "packfile.h"
3131
#include "fetch-object.h"
3232

33+
/* The maximum size for an object header. */
34+
#define MAX_HEADER_LEN 32
35+
3336
const unsigned char null_sha1[GIT_MAX_RAWSZ];
3437
const struct object_id null_oid;
3538
const struct object_id empty_tree_oid = {
@@ -791,7 +794,7 @@ int check_object_signature(const struct object_id *oid, void *map,
791794
enum object_type obj_type;
792795
struct git_istream *st;
793796
git_hash_ctx c;
794-
char hdr[32];
797+
char hdr[MAX_HEADER_LEN];
795798
int hdrlen;
796799

797800
if (map) {
@@ -1150,7 +1153,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,
11501153
unsigned long mapsize;
11511154
void *map;
11521155
git_zstream stream;
1153-
char hdr[32];
1156+
char hdr[MAX_HEADER_LEN];
11541157
struct strbuf hdrbuf = STRBUF_INIT;
11551158
unsigned long size_scratch;
11561159

@@ -1514,7 +1517,7 @@ static int write_buffer(int fd, const void *buf, size_t len)
15141517
int hash_object_file(const void *buf, unsigned long len, const char *type,
15151518
struct object_id *oid)
15161519
{
1517-
char hdr[32];
1520+
char hdr[MAX_HEADER_LEN];
15181521
int hdrlen = sizeof(hdr);
15191522
write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
15201523
return 0;
@@ -1669,7 +1672,7 @@ static int freshen_packed_object(const unsigned char *sha1)
16691672
int write_object_file(const void *buf, unsigned long len, const char *type,
16701673
struct object_id *oid)
16711674
{
1672-
char hdr[32];
1675+
char hdr[MAX_HEADER_LEN];
16731676
int hdrlen = sizeof(hdr);
16741677

16751678
/* 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,
16891692
int hdrlen, status = 0;
16901693

16911694
/* type string, SP, %lu of the length plus NUL must fit this */
1692-
hdrlen = strlen(type) + 32;
1695+
hdrlen = strlen(type) + MAX_HEADER_LEN;
16931696
header = xmalloc(hdrlen);
16941697
write_object_file_prepare(buf, len, type, oid, header, &hdrlen);
16951698

@@ -1709,7 +1712,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
17091712
void *buf;
17101713
unsigned long len;
17111714
enum object_type type;
1712-
char hdr[32];
1715+
char hdr[MAX_HEADER_LEN];
17131716
int hdrlen;
17141717
int ret;
17151718

@@ -2191,7 +2194,7 @@ int read_loose_object(const char *path,
21912194
void *map = NULL;
21922195
unsigned long mapsize;
21932196
git_zstream stream;
2194-
char hdr[32];
2197+
char hdr[MAX_HEADER_LEN];
21952198

21962199
*contents = NULL;
21972200

0 commit comments

Comments
 (0)