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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 3, 2016 at 18:56 UTC c81539b5f6f608ec48358ca6a35f7323e96432e1
1 file changed +16 -4
trace.c
+16 -4
@@ -25,15 +25,25 @@
25 #include "cache.h"
26 #include "quote.h"
27
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 +
41 /* Get a trace file descriptor from "key" env variable. */
42 static int get_trace_fd(struct trace_key *key)
43 {
31 - static struct trace_key trace_default = { "GIT_TRACE" };
44 const char *trace;
45
34 - /* use default "GIT_TRACE" if NULL */
35 - if (!key)
36 - key = &trace_default;
46 + normalize_trace_key(&key);
47
48 /* don't open twice */
49 if (key->initialized)
@@ -75,6 +85,8 @@ static int get_trace_fd(struct trace_key *key)
85
86 void trace_disable(struct trace_key *key)
87 {
88 + normalize_trace_key(&key);
89 +
90 if (key->need_close)
91 close(key->fd);
92 key->fd = 0;