git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>,
	"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>,
	git@vger.kernel.org,
	"Johannes Schindelin" <johannes.schindelin@gmx.de>
Subject: [PATCH] bug_fl(): correctly initialize trace2 va_list
Date: Thu, 16 Jun 2022 16:04:25 -0400	[thread overview]
Message-ID: <YquMyakxYnU6mI5a@coredump.intra.peff.net> (raw)
In-Reply-To: <YquCaE+Vw9P/fybU@coredump.intra.peff.net>

On Thu, Jun 16, 2022 at 03:20:08PM -0400, Jeff King wrote:

> > > The fix Peff's got here LGTM. I can (re)submit it with
> > > format-patch+send-email after giving it a commit message describing the
> > > issue if you'd like, but the change would be the same.
> > 
> > Yup, I think the code change there looks the most sensible.
> 
> I'll wrap it up with a commit message and modify the test to be more
> thorough.

Here it is. This can replace Johannes's patch 11.

-- >8 --
Subject: 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>
---
 t/helper/test-trace2.c | 4 ++--
 usage.c                | 8 +++++---
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/t/helper/test-trace2.c b/t/helper/test-trace2.c
index 180c7f53f3..a714130ece 100644
--- a/t/helper/test-trace2.c
+++ b/t/helper/test-trace2.c
@@ -224,8 +224,8 @@ static int ut_009bug_BUG(int argc, const char **argv)
 
 static int ut_010bug_BUG(int argc, const char **argv)
 {
-	bug("a bug message");
-	BUG("a BUG message");
+	bug("a %s message", "bug");
+	BUG("a %s message", "BUG");
 }
 
 /*
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
-- 
2.37.0.rc0.352.g10876ef154


  reply	other threads:[~2022-06-16 20:04 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-15 23:35 [PATCH 00/11] Coverity fixes Johannes Schindelin via GitGitGadget
2022-06-15 23:35 ` [PATCH 01/11] mingw: avoid accessing uninitialized memory in `is_executable()` Johannes Schindelin via GitGitGadget
2022-06-16  4:07   ` Junio C Hamano
2022-06-16 19:53   ` René Scharfe
2022-06-16 20:13     ` Junio C Hamano
2022-06-16 20:20       ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 02/11] fsmonitor: avoid memory leak in `fsm_settings__get_incompatible_msg()` Johannes Schindelin via GitGitGadget
2022-06-16  4:10   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 03/11] submodule--helper: avoid memory leak in `update_submodule()` Johannes Schindelin via GitGitGadget
2022-06-16  4:23   ` Junio C Hamano
2022-06-16 17:51     ` Glen Choo
2022-06-15 23:35 ` [PATCH 04/11] get_oid_with_context_1(): avoid use-after-free Johannes Schindelin via GitGitGadget
2022-06-16  4:29   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 05/11] submodule-config: avoid memory leak Johannes Schindelin via GitGitGadget
2022-06-16  4:36   ` Junio C Hamano
2022-06-16 18:09     ` Glen Choo
2022-06-15 23:35 ` [PATCH 06/11] pack-redundant: avoid using uninitialized memory Johannes Schindelin via GitGitGadget
2022-06-16  4:53   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 07/11] submodule--helper: avoid memory leak when fetching submodules Johannes Schindelin via GitGitGadget
2022-06-16  4:55   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 08/11] read_index_from(): avoid memory leak Johannes Schindelin via GitGitGadget
2022-06-17 21:27   ` Tom Levy
2022-06-15 23:35 ` [PATCH 09/11] pack-mtimes: avoid closing a bogus file descriptor Johannes Schindelin via GitGitGadget
2022-06-16 20:43   ` Taylor Blau
2022-06-15 23:35 ` [PATCH 10/11] relative_url(): fix incorrect condition Johannes Schindelin via GitGitGadget
2022-06-16  5:02   ` Junio C Hamano
2022-06-16 13:09   ` Ævar Arnfjörð Bjarmason
2022-06-16 17:55     ` Junio C Hamano
2022-06-16 16:41   ` Junio C Hamano
2022-06-15 23:35 ` [PATCH 11/11] bug_fl(): add missing `va_end()` call Johannes Schindelin via GitGitGadget
2022-06-16  4:53   ` Jeff King
2022-06-16  5:00     ` Junio C Hamano
2022-06-16 13:02       ` Ævar Arnfjörð Bjarmason
2022-06-16 18:03         ` Junio C Hamano
2022-06-16 19:20           ` Jeff King
2022-06-16 20:04             ` Jeff King [this message]
2022-06-16 20:11             ` Junio C Hamano
2022-06-16  4:05 ` [PATCH 00/11] Coverity fixes Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YquMyakxYnU6mI5a@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=gitster@pobox.com \
    --cc=johannes.schindelin@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).