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

[PATCH 2/2] read_info_alternates: warn on non-trivial errors

From
Jeff King <peff@peff.net>
Date
Sep 18, 2017, 15:55 UTC
Message-ID
<20170918155519.llmetd2ylebzokrl@sigill.intra.peff.net>
In-Reply-To
<20170918155059.54f7z6cnrl47f5el@sigill.intra.peff.net>

When we fail to open $GIT_DIR/info/alternates, we silently assume there are no alternates. This is the right thing to do for ENOENT, but not for other errors.

A hard error is probably overkill here. If we fail to read an alternates file then either we'll complete our operation anyway, or we'll fail to find some needed object. Either way, a warning is good idea. And we already have a helper function to handle this pattern; let's just call warn_on_fopen_error().

Note that technically the errno from strbuf_read_file() might be from a read() error, not open(). But since read() would never return ENOENT or ENOTDIR, and since it produces a generic "unable to access" error, it's suitable for handling errors from either.

Signed-off-by: Jeff King <peff@peff.net>
---
I'm pretty comfortable with the rationale in the final paragraph. But if
we're concerned that warn_on_fopen_errors() might change, we can swap
out strbuf_read_file() for fopen()+strbuf_read().
 sha1_file.c | 1 +
 1 file changed, 1 insertion(+)
diff --git a/sha1_file.c b/sha1_file.c
index b1e4193679..9cec326298 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -432,6 +432,7 @@ static void read_info_alternates(const char * relative_base, int depth)
 
 	path = xstrfmt("%s/info/alternates", relative_base);
 	if (strbuf_read_file(&buf, path, 1024) < 0) {
+		warn_on_fopen_errors(path);
 		free(path);
 		return;
 	}
-- 
2.14.1.1014.g252e627ae0
Previous: Jeff KingNext: Jonathan Nieder
Message 6 of 14 in “fix read past end of array in alternates files”
  1. 0/2 fix read past end of array in alternates filesJeff King, Sep 18, 2017
  2. 1/2 read_info_alternates: read contents into strbufJeff King, Sep 18, 2017
  3. Junio C HamanoSep 19, 2017
  4. Jonathan NiederSep 19, 2017
  5. Jeff KingSep 19, 2017
  6. 2/2 read_info_alternates: warn on non-trivial errorsJeff King, Sep 18, 2017
  7. Jonathan NiederSep 19, 2017
  8. Jeff KingSep 19, 2017
  9. Jonathan NiederSep 19, 2017
  10. Jeff KingSep 19, 2017
  11. 0/2 fix read past end of array in alternates filesJeff King, Sep 19, 2017
  12. 1/2 read_info_alternates: read contents into strbufJeff King, Sep 19, 2017
  13. 2/2 read_info_alternates: warn on non-trivial errorsJeff King, Sep 19, 2017
  14. Junio C HamanoSep 20, 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.