Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2022-06-16 23:04:25 +0300
committerJunio C Hamano <gitster@pobox.com>2022-06-16 23:28:22 +0300
commitf8535596aa7bd7f6862af3fe6420ac12b17c9912 (patch)
tree8cde9e8678bbe95705f43589e27bdcfa81554a17 /usage.c
parent6d40f0ad15e72daab6524988c9c4b4a3460488f8 (diff)
bug_fl(): correctly initialize trace2 va_list
The code added 0cc05b044f (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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'usage.c')
-rw-r--r--usage.c8
1 files changed, 5 insertions, 3 deletions
diff --git a/usage.c b/usage.c
index 79900d0287..56e29d6cd6 100644
--- a/usage.c
+++ b/usage.c
@@ -334,15 +334,17 @@ NORETURN void BUG_fl(const char *file, int line, const char *fmt, ...)
int bug_called_must_BUG;
void bug_fl(const char *file, int line, const char *fmt, ...)
{
- va_list ap, cp;
+ va_list ap;
bug_called_must_BUG = 1;
- va_copy(cp, ap);
va_start(ap, fmt);
BUG_vfl_common(file, line, fmt, ap);
va_end(ap);
- trace2_cmd_error_va(fmt, cp);
+
+ va_start(ap, fmt);
+ trace2_cmd_error_va(fmt, ap);
+ va_end(ap);
}
#ifdef SUPPRESS_ANNOTATED_LEAKS