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

[PATCH] fsck: check results of lookup_blob() and lookup_tree() for NULL

From
René Scharfe <l.s.r@web.de>
Date
Oct 3, 2017, 13:47 UTC
Message-ID
<54f5877b-a143-11c2-d8f6-ff28ed9e7e38@web.de>
In-Reply-To
<19a08fb1-2fb3-f368-772b-36646a179975@web.de>
Am 03.10.2017 um 14:51 schrieb René Scharfe:
Show 9 quoted lines
> Am 03.10.2017 um 12:22 schrieb SZEDER Gábor:
>> Furthermore, fsck.c:fsck_walk_tree() does the same "immediately
>> reference the object member in lookup_blob()'s and lookup_tree()'s
>> return value" thing.  I think those should receive the same treatment
>> as well.
> 
> Hmm, are put_object_name() and all the walk() implementations ready for
> a NULL object handed to them?  Or would we rather need to error out
> right there?
How about this?

-- >8 -- lookup_blob() and lookup_tree() can return NULL if they find an object of an unexpected type. Error out of fsck_walk_tree() in that case, like we do when encountering a bad file mode. An error message is already shown by object_as_type(), which gets called by the lookup functions.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 fsck.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/fsck.c b/fsck.c
index 2ad00fc4d0..561a13ac27 100644
--- a/fsck.c
+++ b/fsck.c
@@ -358,14 +358,20 @@ static int fsck_walk_tree(struct tree *tree, void *data, struct fsck_options *op
 			continue;
 
 		if (S_ISDIR(entry.mode)) {
-			obj = &lookup_tree(entry.oid)->object;
+			struct tree *tree = lookup_tree(entry.oid);
+			if (!tree)
+				return -1;
+			obj = &tree->object;
 			if (name)
 				put_object_name(options, obj, "%s%s/", name,
 					entry.path);
 			result = options->walk(obj, OBJ_TREE, data, options);
 		}
 		else if (S_ISREG(entry.mode) || S_ISLNK(entry.mode)) {
-			obj = &lookup_blob(entry.oid)->object;
+			struct blob *blob = lookup_blob(entry.oid);
+			if (!blob)
+				return -1;
+			obj = &blob->object;
 			if (name)
 				put_object_name(options, obj, "%s%s", name,
 					entry.path);
-- 
2.14.2
Previous: René ScharfeNext: Junio C Hamano
Message 8 of 13 in “tag: avoid NULL pointer arithmetic”
  1. tag: avoid NULL pointer arithmeticRené Scharfe, Oct 1, 2017
  2. Junio C HamanoOct 2, 2017
  3. Jeff KingOct 2, 2017
  4. René ScharfeOct 2, 2017
  5. Jeff KingOct 2, 2017
  6. SZEDER GáborOct 3, 2017
  7. René ScharfeOct 3, 2017
  8. fsck: check results of lookup_blob() and lookup_tree() for NULLRené Scharfe, Oct 3, 2017
  9. Junio C HamanoOct 4, 2017
  10. René ScharfeOct 5, 2017
  11. fsck: handle NULL return of lookup_blob() and lookup_tree()René Scharfe, Oct 5, 2017
  12. Junio C HamanoOct 6, 2017
  13. René ScharfeOct 6, 2017

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.