From 53ec551c87c731c5c4171e943842998bdfb5548e Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 27 Jan 2014 20:36:12 -0500 Subject: expand_user_path: do not look at NULL path 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 Signed-off-by: Junio C Hamano diff --git a/path.c b/path.c index 6f2aa69..3a57961 100644 --- a/path.c +++ b/path.c @@ -231,12 +231,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) { -- cgit v0.10.2-6-g49f6 From 67beb600563cf28186f44450e528df1ec4d524fd Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 27 Jan 2014 20:37:30 -0500 Subject: handle_path_include: don't look at NULL value When we see config like: [include] path the expand_user_path helper notices that the config value is empty, but we then dereference NULL while printing the error message (glibc will helpfully print "(null)" for us here, but we cannot rely on that). $ git -c include.path rev-parse error: Could not expand include path '(null)' fatal: unable to parse command-line config Instead of tweaking our message, let's actually use config_error_nonbool to match other config variables that expect a value: $ git -c include.path rev-parse error: Missing value for 'include.path' fatal: unable to parse command-line config Signed-off-by: Jeff King Signed-off-by: Junio C Hamano diff --git a/config.c b/config.c index 2bbf02d..47ce8d8 100644 --- a/config.c +++ b/config.c @@ -37,8 +37,12 @@ static int handle_path_include(const char *path, struct config_include_data *inc { int ret = 0; struct strbuf buf = STRBUF_INIT; - char *expanded = expand_user_path(path); + char *expanded; + if (!path) + return config_error_nonbool("include.path"); + + expanded = expand_user_path(path); if (!expanded) return error("Could not expand include path '%s'", path); path = expanded; -- cgit v0.10.2-6-g49f6