Skip to content

Commit c81539b

Browse files
peffgitster
authored andcommitted
trace: handle NULL argument in trace_disable()
All of the trace functions treat a NULL key as a synonym for the default GIT_TRACE key. Except for trace_disable(), which will segfault. Fortunately, this can't cause any bugs, as the function has no callers. But rather than drop it, let's fix the bug, as I plan to add a caller. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 80460f5 commit c81539b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

trace.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@
2525
#include "cache.h"
2626
#include "quote.h"
2727

28+
/*
29+
* "Normalize" a key argument by converting NULL to our trace_default,
30+
* and otherwise passing through the value. All caller-facing functions
31+
* should normalize their inputs in this way, though most get it
32+
* for free by calling get_trace_fd() (directly or indirectly).
33+
*/
34+
static void normalize_trace_key(struct trace_key **key)
35+
{
36+
static struct trace_key trace_default = { "GIT_TRACE" };
37+
if (!*key)
38+
*key = &trace_default;
39+
}
40+
2841
/* Get a trace file descriptor from "key" env variable. */
2942
static int get_trace_fd(struct trace_key *key)
3043
{
31-
static struct trace_key trace_default = { "GIT_TRACE" };
3244
const char *trace;
3345

34-
/* use default "GIT_TRACE" if NULL */
35-
if (!key)
36-
key = &trace_default;
46+
normalize_trace_key(&key);
3747

3848
/* don't open twice */
3949
if (key->initialized)
@@ -75,6 +85,8 @@ static int get_trace_fd(struct trace_key *key)
7585

7686
void trace_disable(struct trace_key *key)
7787
{
88+
normalize_trace_key(&key);
89+
7890
if (key->need_close)
7991
close(key->fd);
8092
key->fd = 0;

0 commit comments

Comments
 (0)