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

[PATCH 1/2] expand_user_path: do not look at NULL path

From
Jeff King <peff@peff.net>
Date
Jan 28, 2014, 01:36 UTC
Message-ID
<20140128013612.GA29548@sigill.intra.peff.net>

We explicitly check for and handle the case that the incoming "path" variable is NULL, but before doing so we call strchrnul on it, leading to a potential segfault.

We can fix this simply by moving the strchrnul call down; as a bonus, we can tighten the scope on the associated variable.

Signed-off-by: Jeff King <peff@peff.net>
---
Most of the callers already check for NULL, so the only way to trigger
this is via an empty "include.path". That is addressed separately in the
next patch, so technically this is not needed if we apply that one. But
in that case, the "path == NULL" check here is useless, so I think this
makes things safer overall.
 path.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/path.c b/path.c
index 24594c4..f9c5062 100644
--- a/path.c
+++ b/path.c
@@ -265,12 +265,12 @@ static struct passwd *getpw_str(const char *username, size_t len)
 char *expand_user_path(const char *path)
 {
 	struct strbuf user_path = STRBUF_INIT;
-	const char *first_slash = strchrnul(path, '/');
 	const char *to_copy = path;
 
 	if (path == NULL)
 		goto return_null;
 	if (path[0] == '~') {
+		const char *first_slash = strchrnul(path, '/');
 		const char *username = path + 1;
 		size_t username_len = first_slash - username;
 		if (username_len == 0) {
-- 
1.8.5.2.500.g8060133
Next: Jeff King
Message 1 of 2 in “expand_user_path: do not look at NULL path”
  1. 1/2 expand_user_path: do not look at NULL pathJeff King, Jan 28, 2014
  2. 2/2 handle_path_include: don't look at NULL valueJeff King, Jan 28, 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.