Skip to content

Commit f853559

Browse files
peffgitster
authored andcommitted
bug_fl(): correctly initialize trace2 va_list
The code added 0cc05b0 (usage.c: add a non-fatal bug() function to go with BUG(), 2022-06-02) sets up two va_list variables: one to output to stderr, and one to trace2. But the order of initialization is wrong: va_list ap, cp; va_copy(cp, ap); va_start(ap, fmt); We copy the contents of "ap" into "cp" before it is initialized, meaning it is full of garbage. The two should be swapped. However, there's another bug, noticed by Johannes Schindelin: we forget to call va_end() for the copy. So instead of just fixing the copy's initialization, let's do two separate start/end pairs. This is allowed by the standard, and we don't need to use copy here since we have access to the original varargs. Matching the pairs with the calls makes it more obvious that everything is being done correctly. Note that we do call bug_fl() in the tests, but it didn't trigger this problem because our format string doesn't have any placeholders. So even though we were passing a garbage va_list through the stack, nobody ever needed to look at it. We can easily adjust one of the trace2 tests to trigger this, both for bug() and for BUG(). The latter isn't broken, but it's nice to exercise both a bit more. Without the fix in this patch (but with the test change), the bug() case causes a segfault. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6d40f0a commit f853559

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

t/helper/test-trace2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ static int ut_009bug_BUG(int argc, const char **argv)
224224

225225
static int ut_010bug_BUG(int argc, const char **argv)
226226
{
227-
bug("a bug message");
228-
BUG("a BUG message");
227+
bug("a %s message", "bug");
228+
BUG("a %s message", "BUG");
229229
}
230230

231231
/*

usage.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,15 +334,17 @@ NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
334334
int bug_called_must_BUG;
335335
void bug_fl(const char *file, int line, const char *fmt, ...)
336336
{
337-
va_list ap, cp;
337+
va_list ap;
338338

339339
bug_called_must_BUG = 1;
340340

341-
va_copy(cp, ap);
342341
va_start(ap, fmt);
343342
BUG_vfl_common(file, line, fmt, ap);
344343
va_end(ap);
345-
trace2_cmd_error_va(fmt, cp);
344+
345+
va_start(ap, fmt);
346+
trace2_cmd_error_va(fmt, ap);
347+
va_end(ap);
346348
}
347349

348350
#ifdef SUPPRESS_ANNOTATED_LEAKS

0 commit comments

Comments
 (0)