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

Re: [PATCH 5/5] streaming: simplify attaching a filter

From
Junio C Hamano <gitster@pobox.com>
Date
Feb 19, 2014, 00:02 UTC
Message-ID
<xmqq8ut8as1f.fsf@gitster.dls.corp.google.com>
In-Reply-To
<xmqqd2ikasbe.fsf@gitster.dls.corp.google.com>
Junio C Hamano <gitster@pobox.com> writes:
Show 12 quoted lines
> John Keeping <john@keeping.me.uk> writes:
>
>> We are guaranteed that 'nst' is non-null because it is allocated with
>> xmalloc(), and in fact we rely on this three lines later by
>> unconditionally dereferencing it.
>
> The intent of the original code is for attach_stream_filter() to
> detect an error condition and return NULL, in which case it closes
> the istream it allocated and signal error to the caller, I think,
> and falling thru to use st->anything and return st when that happens
> is *not* a guarantee that a-s-f will not detect an error ever, but
> rather is a bug in the error codepath.
In other words, something like this instead.
-- >8 --
Subject: [PATCH] open_istream(): do not dereference NULL in the error case

When stream-filter cannot be attached, it is expected to return NULL, and we should close the stream we opened and signal an error by returning NULL ourselves from this function.

However, we attempted to dereference that NULL pointer between the point we detected the error and returned from the function.

Brought-to-attention-by: John Keeping <john@keeping.me.uk>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 streaming.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/streaming.c b/streaming.c
index d7c9f32..2ff036a 100644
--- a/streaming.c
+++ b/streaming.c
@@ -152,8 +152,10 @@ struct git_istream *open_istream(const unsigned char *sha1,
 	if (filter) {
 		/* Add "&& !is_null_stream_filter(filter)" for performance */
 		struct git_istream *nst = attach_stream_filter(st, filter);
-		if (!nst)
+		if (!nst) {
 			close_istream(st);
+			return NULL;
+		}
 		st = nst;
 	}
 
-- 
1.9.0-279-gdc9e3eb
Previous: Junio C Hamano
Message 13 of 13 in “Miscellaneous fixes from static analysis”
  1. 0/5 Miscellaneous fixes from static analysisJohn Keeping, Feb 16, 2014
  2. 1/5 notes-utils: handle boolean notes.rewritemode correctlyJohn Keeping, Feb 16, 2014
  3. David KastrupFeb 16, 2014
  4. Jeff KingFeb 18, 2014
  5. David KastrupFeb 18, 2014
  6. Jeff KingFeb 18, 2014
  7. David KastrupFeb 18, 2014
  8. 2/5 utf8: fix iconv error detectionJohn Keeping, Feb 16, 2014
  9. 3/5 utf8: use correct type for values in interval tableJohn Keeping, Feb 16, 2014
  10. 4/5 builtin/mv: don't use memory after freeJohn Keeping, Feb 16, 2014
  11. 5/5 streaming: simplify attaching a filterJohn Keeping, Feb 16, 2014
  12. Junio C HamanoFeb 18, 2014
  13. Junio C HamanoFeb 19, 2014

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.