git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/3] usage.c: add BUG() function

From
Jeff King <peff@peff.net>
Date
May 13, 2017, 03:55 UTC
Message-ID
<20170513035503.cubqhzcvhifp54yg@sigill.intra.peff.net>
In-Reply-To
<20170513032850.zeru4cm2l7i23rkc@sigill.intra.peff.net>
On Fri, May 12, 2017 at 11:28:50PM -0400, Jeff King wrote:
Show 13 quoted lines
> +static NORETURN void BUG_vfl(const char *file, int line, const char *fmt, va_list params)
> +{
> +	char prefix[256];
> +
> +	/* truncation via snprintf is OK here */
> +	if (file)
> +		snprintf(prefix, sizeof(prefix), "BUG: %s:%d: ", file, line);
> +	else
> +		snprintf(prefix, sizeof(prefix), "BUG: ");
> +
> +	vreportf(prefix, fmt, params);
> +	abort();
> +}

I used vreportf() here to match die(). But the two things that function does are:

  1. Respect error_handle. But after bw/forking-and-threading is merged,
     nobody will ever set error_handle (and I just sent a patch to drop
     it entirely).
  2. Quotes non-printable characters. I don't know how useful this is.
     Most of the assertion messages are pretty vanilla (because anything
     that relies on user input probably should be a regular die, not an
     assertion failure). But a few of them do actually print arbitrary
     strings in a reasonable way (e.g., a BUG() which is handling user
     string which was supposed to be vetted by an earlier function is
     still a reasonable assertion, but it's useful to show the string).
So an alternative would be just:
  fprintf(stderr, "BUG: ");
  if (file)
	fprintf(stderr, "%s:%d ", file, line);
  vfprintf(stderr, fmt, params);
  fputc('\n', stderr);

which is perhaps a bit simpler (not much in lines of code, but there's no extra buffer to reason about). But given the discussion in (2) above, it's probably worth continuing to use vreportf.

-Peff
Previous: Jeff KingNext: Junio C Hamano
Message 9 of 25 in “[Git 2.13.0] BUG: setup_git_env called without repository”
  1. Josh HaginsMay 12, 2017
  2. Johannes SchindelinMay 12, 2017
  3. Josh HaginsMay 15, 2017
  4. config: complain about --local outside of a git repoJeff King, May 12, 2017
  5. Ævar Arnfjörð BjarmasonMay 12, 2017
  6. Jeff KingMay 13, 2017
  7. 0/3 BUG() and "config --local" outside of repoJeff King, May 13, 2017
  8. 1/3 usage.c: add BUG() functionJeff King, May 13, 2017
  9. Jeff KingMay 13, 2017
  10. Junio C HamanoMay 15, 2017
  11. 2/3 setup_git_env: convert die("BUG") to BUG()Jeff King, May 13, 2017
  12. 3/3 config: complain about --local outside of a git repoJeff King, May 13, 2017
  13. 0/4 Finish the conversion from die("BUG: ...") to BUG()Johannes Schindelin, May 2, 2018
  14. 1/4 test-tool: help verifying BUG() code pathsJohannes Schindelin, May 2, 2018
  15. Duy NguyenMay 2, 2018
  16. Johannes SchindelinMay 5, 2018
  17. 2/4 run-command: use BUG() to report bugs, not die()Johannes Schindelin, May 2, 2018
  18. Jeff KingMay 7, 2018
  19. 3/4 Replace all die("BUG: ...") calls by BUG() onesJohannes Schindelin, May 2, 2018
  20. 4/4 Convert remaining die*(BUG) messagesJohannes Schindelin, May 2, 2018
  21. Jeff KingMay 7, 2018
  22. Jonathan NiederMay 13, 2017
  23. Jeff KingMay 13, 2017
  24. Josh HaginsMay 15, 2017
  25. Jeff KingMay 15, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.