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

[PATCH 1/2] read_and_strip_branch: fix typo'd address-of operator

From
Jeff King <peff@peff.net>
Date
Jan 28, 2015, 17:57 UTC
Message-ID
<20150128175735.GA8172@peff.net>
In-Reply-To
<20150128175314.GA13362@peff.net>

When we are chomping newlines from the end of a strbuf, we must check "sb.len != 0" before accessing "sb.buf[sb.len - 1]". However, this code mistakenly checks "&sb.len", which is always true (it is a part of an auto struct, so the address is always non-zero). This could lead to us accessing memory outside the strbuf when we read an empty file.

Signed-off-by: Jeff King <peff@peff.net>
---
This dates back to 8b87cfd (wt-status: move strbuf into
read_and_strip_branch(), 2013-03-16), so it is not a bug that needs
addressed during the -rc period.

This is the most minimal fix, but I kind of wonder if it should just be using strbuf_rtrim (or even strbuf_trim) in the first place.

 wt-status.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/wt-status.c b/wt-status.c
index b54eac5..29666d0 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1140,7 +1140,7 @@ static char *read_and_strip_branch(const char *path)
 	if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
 		goto got_nothing;
 
-	while (&sb.len && sb.buf[sb.len - 1] == '\n')
+	while (sb.len && sb.buf[sb.len - 1] == '\n')
 		strbuf_setlen(&sb, sb.len - 1);
 	if (!sb.len)
 		goto got_nothing;
-- 
2.3.0.rc1.287.g761fd19
Previous: Jeff KingNext: Junio C Hamano
Message 2 of 6 in “silence clang-3.6 warnings”
  1. 0/2 silence clang-3.6 warningsJeff King, Jan 28, 2015
  2. 1/2 read_and_strip_branch: fix typo'd address-of operatorJeff King, Jan 28, 2015
  3. Junio C HamanoJan 28, 2015
  4. Jeff KingJan 28, 2015
  5. Junio C HamanoJan 29, 2015
  6. 2/2 do not check truth value of flex arraysJeff King, Jan 28, 2015

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.