git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 35/36] sha1_file: introduce a constant for max header length

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Feb 25, 2018, 21:12 UTC
Message-ID
<20180225211212.477570-36-sandals@crustytoothpaste.net>
In-Reply-To
<20180225211212.477570-1-sandals@crustytoothpaste.net>

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>
---
 sha1_file.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
diff --git a/sha1_file.c b/sha1_file.c
index c41fbe2f01..4d4f9248c7 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -30,6 +30,9 @@
 #include "packfile.h"
 #include "fetch-object.h"
 
+/* The maximum size for an object header. */
+#define MAX_HEADER_LEN 32
+
 const unsigned char null_sha1[GIT_MAX_RAWSZ];
 const struct object_id null_oid;
 const struct object_id empty_tree_oid = {
@@ -791,7 +794,7 @@ int check_object_signature(const struct object_id *oid, void *map,
 	enum object_type obj_type;
 	struct git_istream *st;
 	git_hash_ctx c;
-	char hdr[32];
+	char hdr[MAX_HEADER_LEN];
 	int hdrlen;
 
 	if (map) {
@@ -1150,7 +1153,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,
 	unsigned long mapsize;
 	void *map;
 	git_zstream stream;
-	char hdr[32];
+	char hdr[MAX_HEADER_LEN];
 	struct strbuf hdrbuf = STRBUF_INIT;
 	unsigned long size_scratch;
 
@@ -1512,7 +1515,7 @@ static int write_buffer(int fd, const void *buf, size_t len)
 int hash_object_file(const void *buf, unsigned long len, const char *type,
 		     struct object_id *oid)
 {
-	char hdr[32];
+	char hdr[MAX_HEADER_LEN];
 	int hdrlen = sizeof(hdr);
 	write_object_file_prepare(buf, len, type, oid, hdr, &hdrlen);
 	return 0;
@@ -1667,7 +1670,7 @@ static int freshen_packed_object(const unsigned char *sha1)
 int write_object_file(const void *buf, unsigned long len, const char *type,
 		      struct object_id *oid)
 {
-	char hdr[32];
+	char hdr[MAX_HEADER_LEN];
 	int hdrlen = sizeof(hdr);
 
 	/* Normally if we have it in the pack then we do not bother writing
@@ -1687,7 +1690,7 @@ int hash_object_file_literally(const void *buf, unsigned long len,
 	int hdrlen, status = 0;
 
 	/* type string, SP, %lu of the length plus NUL must fit this */
-	hdrlen = strlen(type) + 32;
+	hdrlen = strlen(type) + MAX_HEADER_LEN;
 	header = xmalloc(hdrlen);
 	write_object_file_prepare(buf, len, type, oid, header, &hdrlen);
 
@@ -1707,7 +1710,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
 	void *buf;
 	unsigned long len;
 	enum object_type type;
-	char hdr[32];
+	char hdr[MAX_HEADER_LEN];
 	int hdrlen;
 	int ret;
 
@@ -2189,7 +2192,7 @@ int read_loose_object(const char *path,
 	void *map = NULL;
 	unsigned long mapsize;
 	git_zstream stream;
-	char hdr[32];
+	char hdr[MAX_HEADER_LEN];
 
 	*contents = NULL;
 
Previous: brian m. carlsonNext: brian m. carlson
Message 19 of 42 in “object_id part 12”
  1. 00/36 object_id part 12brian m. carlson, Feb 25, 2018
  2. 01/36 bulk-checkin: convert index_bulk_checkin to struct object_idbrian m. carlson, Feb 25, 2018
  3. 03/36 cache-tree: convert write_*_as_tree to object_idbrian m. carlson, Feb 25, 2018
  4. 04/36 cache-tree: convert remnants to struct object_idbrian m. carlson, Feb 25, 2018
  5. Duy NguyenFeb 26, 2018
  6. 02/36 builtin/write-tree: convert to struct object_idbrian m. carlson, Feb 25, 2018
  7. 06/36 tree: convert read_tree_recursive to struct object_idbrian m. carlson, Feb 25, 2018
  8. 12/36 send-pack: convert remaining functions to struct object_idbrian m. carlson, Feb 25, 2018
  9. 13/36 replace_object: convert struct replace_object to object_idbrian m. carlson, Feb 25, 2018
  10. 14/36 builtin/mktag: convert to struct object_idbrian m. carlson, Feb 25, 2018
  11. 15/36 archive: convert write_archive_entry_fn_t to object_idbrian m. carlson, Feb 25, 2018
  12. 17/36 builtin/index-pack: convert struct ref_delta_entry to object_idbrian m. carlson, Feb 25, 2018
  13. 18/36 sha1_file: convert read_loose_object to use struct object_idbrian m. carlson, Feb 25, 2018
  14. 23/36 sha1_file: convert retry_bad_packed_offset to struct object_idbrian m. carlson, Feb 25, 2018
  15. 30/36 streaming: convert istream internals to struct object_idbrian m. carlson, Feb 25, 2018
  16. 22/36 sha1_file: convert assert_sha1_type to object_idbrian m. carlson, Feb 25, 2018
  17. 29/36 tree-walk: convert get_tree_entry_follow_symlinks internals to object_idbrian m. carlson, Feb 25, 2018
  18. 32/36 sha1_file: convert read_object_with_reference to object_idbrian m. carlson, Feb 25, 2018
  19. 35/36 sha1_file: introduce a constant for max header lengthbrian m. carlson, Feb 25, 2018
  20. 34/36 Convert lookup_replace_object to struct object_idbrian m. carlson, Feb 25, 2018
  21. Duy NguyenFeb 26, 2018
  22. 33/36 sha1_file: convert read_sha1_file to struct object_idbrian m. carlson, Feb 25, 2018
  23. 36/36 convert: convert to struct object_idbrian m. carlson, Feb 25, 2018
  24. 31/36 tree-walk: convert tree entry functions to object_idbrian m. carlson, Feb 25, 2018
  25. 28/36 builtin/notes: convert static functions to object_idbrian m. carlson, Feb 25, 2018
  26. 27/36 builtin/fmt-merge-msg: convert remaining code to object_idbrian m. carlson, Feb 25, 2018
  27. 26/36 sha1_file: convert sha1_object_info* to object_idbrian m. carlson, Feb 25, 2018
  28. 25/36 Convert remaining callers of sha1_object_info_extended to object_idbrian m. carlson, Feb 25, 2018
  29. 24/36 packfile: convert unpack_entry to struct object_idbrian m. carlson, Feb 25, 2018
  30. 21/36 builtin/mktree: convert to struct object_idbrian m. carlson, Feb 25, 2018
  31. 11/36 http-walker: convert struct object_request to use struct object_idbrian m. carlson, Feb 25, 2018
  32. 05/36 resolve-undo: convert struct resolve_undo_info to object_idbrian m. carlson, Feb 25, 2018
  33. Duy NguyenFeb 26, 2018
  34. brian m. carlsonFeb 27, 2018
  35. Duy NguyenFeb 28, 2018
  36. 20/36 streaming: convert open_istream to use struct object_idbrian m. carlson, Feb 25, 2018
  37. 19/36 sha1_file: convert check_sha1_signature to struct object_idbrian m. carlson, Feb 25, 2018
  38. 16/36 archive: convert sha1_file_to_archive to struct object_idbrian m. carlson, Feb 25, 2018
  39. 10/36 Convert find_unique_abbrev* to struct object_idbrian m. carlson, Feb 25, 2018
  40. 09/36 wt-status: convert struct wt_status_state to object_idbrian m. carlson, Feb 25, 2018
  41. 08/36 strbuf: convert strbuf_add_unique_abbrev to use struct object_idbrian m. carlson, Feb 25, 2018
  42. 07/36 ref-filter: convert grab_objectname to struct object_idbrian m. carlson, Feb 25, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.