Skip to content

Commit e83c267

Browse files
drafnelgitster
authored andcommitted
trace.c: ensure NULL is not passed to printf
GNU printf, and many others, will print the string "(null)" if a NULL pointer is passed as the argument to a "%s" format specifier. Some implementations (like on Solaris) do not detect a NULL pointer and will produce a segfault in this case. So, fix this by ensuring that pointer variables do not contain the value NULL. Assign the string "(null)" to the variables are NULL. Signed-off-by: Brandon Casey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 685e9d9 commit e83c267

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

trace.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ static const char *quote_crnl(const char *path)
154154
/* FIXME: move prefix to startup_info struct and get rid of this arg */
155155
void trace_repo_setup(const char *prefix)
156156
{
157+
const char *git_work_tree;
157158
char cwd[PATH_MAX];
158159
char *trace = getenv("GIT_TRACE");
159160

@@ -164,8 +165,14 @@ void trace_repo_setup(const char *prefix)
164165
if (!getcwd(cwd, PATH_MAX))
165166
die("Unable to get current working directory");
166167

168+
if (!(git_work_tree = get_git_work_tree()))
169+
git_work_tree = "(null)";
170+
171+
if (!prefix)
172+
prefix = "(null)";
173+
167174
trace_printf("setup: git_dir: %s\n", quote_crnl(get_git_dir()));
168-
trace_printf("setup: worktree: %s\n", quote_crnl(get_git_work_tree()));
175+
trace_printf("setup: worktree: %s\n", quote_crnl(git_work_tree));
169176
trace_printf("setup: cwd: %s\n", quote_crnl(cwd));
170177
trace_printf("setup: prefix: %s\n", quote_crnl(prefix));
171178
}

0 commit comments

Comments
 (0)