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

[PATCH 3/3] improved validation of entry type in mktree

From
JMJosh Micich <josh.micich@gmail.com>
Date
May 14, 2009, 05:11 UTC
Message-ID
<loom.20090514T051052-920@post.gmane.org>
In-Reply-To
<loom.20090514T050424-673@post.gmane.org>

Previously mktree would accept tree entries which had a mismatch between the declared type and the actual type of object identified by the sha.

Signed-off-by: Josh Micich <josh.micich@gmail.com>
---
 builtin-mktree.c |   34 +++++++++++++++++++++-------------
 1 files changed, 21 insertions(+), 13 deletions(-)
diff --git a/builtin-mktree.c b/builtin-mktree.c
index a56c917..a37954a 100644
--- a/builtin-mktree.c
+++ b/builtin-mktree.c
@@ -71,7 +71,7 @@ static void mktree_line(char *buf, size_t len, int 
line_termination, int allow_m
 {
 	char *ptr, *ntr;
 	unsigned mode;
-	enum object_type type;
+	enum object_type mode_type;
 	char *path;
 	unsigned char sha1[20];
 
@@ -94,29 +94,37 @@ static void mktree_line(char *buf, size_t len, int 
line_termination, int allow_m
 	if (S_ISGITLINK(mode))
 		allow_missing = 1;
 
-	if (!allow_missing)
-		type = sha1_object_info(sha1, NULL);
-	else
-		type = object_type(mode);
-
-	if (type < 0)
-		die("object %s unavailable", sha1_to_hex(sha1));
 
 	*ntr++ = 0; /* now at the beginning of SHA1 */
-	if (type != type_from_string(ptr))
-		die("object type %s mismatch (%s)", ptr, typename(type));
 
 	path = ntr + 41;  /* at the beginning of name */
+	struct strbuf p_uq = STRBUF_INIT;
 	if (line_termination && path[0] == '"') {
-		struct strbuf p_uq = STRBUF_INIT;
 		if (unquote_c_style(&p_uq, path, NULL))
 			die("invalid quoting");
 		path = strbuf_detach(&p_uq, NULL);
 		append_to_tree(mode, sha1, path);
-		strbuf_release(&p_uq);
-		return;
 	}
+
+	mode_type = object_type(mode);
+	if (mode_type != type_from_string(ptr)) {
+		die("entry '%s' object type (%s) doesn't match mode type (%s)", 
path, ptr, typename(mode_type));
+	}
+
+	enum object_type obj_type = sha1_object_info(sha1, NULL);
+	if (obj_type < 0) {
+		if (!allow_missing) {
+			die("entry '%s' object %s is unavailable", path, 
sha1_to_hex(sha1));
+		}
+	} else {
+		if (obj_type != mode_type) {
+			die("entry '%s' object %s is a %s but specified type 
was (%s)",
+				path, sha1_to_hex(sha1), typename(obj_type), 
typename(mode_type));
+		}
+	}
+	
 	append_to_tree(mode, sha1, path);
+	strbuf_release(&p_uq); // safe to call on unused empty buffer
 }
 
 int cmd_mktree(int ac, const char **av, const char *prefix)
-- 
1.6.3.165.g2cce5.dirty
Previous: Josh MicichNext: Junio C Hamano
Message 10 of 16 in “Proposed patch for mktree [0/3]”
  1. Josh MicichMay 14, 2009
  2. 1/3 '--missing' option for mktree: re-added strbuf_release(&p_uq), Updated man pageJosh Micich, May 14, 2009
  3. Junio C HamanoMay 14, 2009
  4. Josh MicichMay 14, 2009
  5. 2/3 added --batch option to mktreeJosh Micich, May 14, 2009
  6. Junio C HamanoMay 14, 2009
  7. Josh MicichMay 14, 2009
  8. Sverre RabbelierMay 14, 2009
  9. Josh MicichMay 14, 2009
  10. 3/3 improved validation of entry type in mktreeJosh Micich, May 14, 2009
  11. Junio C HamanoMay 14, 2009
  12. Josh MicichMay 14, 2009
  13. Josh MicichMay 14, 2009
  14. Jakub NarebskiMay 15, 2009
  15. Josh MicichMay 15, 2009
  16. Junio C HamanoMay 15, 2009

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.