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

Re: [PATCH/RFC] doc: document error handling functions and conventions (Re: [PATCH 03/14] copy_fd: pass error message back through a strbuf)

From
Jeff King <peff@peff.net>
Date
Dec 7, 2014, 10:07 UTC
Message-ID
<20141207100755.GB22230@peff.net>
In-Reply-To
<xmqqfvcuq8nu.fsf@gitster.dls.corp.google.com>
On Fri, Dec 05, 2014 at 10:00:05AM -0800, Junio C Hamano wrote:
Show 22 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > The only downside I can think of is that we may truncate the message in
> > exceptional circumstances. But is it really any less helpful to say:
> >
> >   error: unable to open file: some-incredibly-long-filename-aaaaaa...
> >
> > than printing out an extra 100 lines of "a"? And I mean the "..."
> > literally. I think mkerror() should indicate the truncation with a
> > "...", just so that it is clear to the user. It should almost never
> > happen, but when it does, it can be helpful to show the user that yes,
> > we know we are truncating the message, and it is not that git truncated
> > your filename during the operation.
> >
> > Is this truncation really a concern, and/or is there some other downside
> > I'm not thinking of?
> 
> I am more worried about variable length part pushing the information
> that is given later out to the right, e.g. "error: missing file '%s'
> prevents us from doing X".  Chomping to [1024] is not a good
> strategy for that kind of message; abbreviating %s to /path/name/...
> (again, with literally "...") would be.

True. Unfortunately I do not think there is an easy way to truncate the expanded strings used by placeholders. The two approaches I could think of are:

  1. Loop over the va_list and tweak any pointers we find. Except that
     loop means we actually need to loop over the _format string_, since
     that is the only thing which tells us which placeholders are strings.
     But I am not even sure there is a portable way to munge a va_list,
     as opposed to just reading it.
  2. Transparently rewrite '%s' in the format string to '%.128s' or
     similar. This also requires iterating over the format string, but
     it's fairly straightforward. Code is below, but it's not _quite_
     right. We would want to conditionally add "..." based on whether or
     not the particular item was truncated. But we cannot know when
     munging the format string if that will happen or not (we know
     _something_ will be truncated, but not which string).

I don't think you can do it right without reimplementing vsnprintf yourself. Which is obviously a terrible idea.

I still think truncation is going to be a non-issue in practice, and I'd rather deal with that potential fallout than have to deal with memory management in every error handler that uses this technique. But I don't have much more to say on the matter, so if you disagree, I guess that's that.

Unless we can do something clever with a set of global error strbufs or something (i.e., that expand as needed, but the caller does not have to free themselves, as they will get recycled eventually). That has its own corner cases, though.

Anyway, the truncating-fmt code is below, for fun.
-Peff
static int abbrev_fmt(char *buf, int len, int abbrev, const char *fmt, va_list ap)
{
	va_list cp;
	int got;
	va_copy(cp, ap);
	got = vsnprintf(buf, len, fmt, ap);
	if (got >= len) {
		struct strbuf munged = STRBUF_INIT;
		while (*fmt) {
			char ch = *fmt++;
			strbuf_addch(&munged, ch);
			if (ch == '%') {
				if (*fmt == 's') {
					strbuf_addf(&munged, ".%ds...", abbrev);
					fmt++;
				} else /* skips past %%-quoting */
					strbuf_addch(&munged, *fmt);
			}
		}
		got = vsnprintf(buf, len, munged.buf, cp);
		strbuf_release(&munged);
	}
	return got;
}
Previous: Junio C HamanoNext: Junio C Hamano
Message 37 of 83 in “copy.c: make copy_fd preserve meaningful errno”
  1. copy.c: make copy_fd preserve meaningful errnoStefan Beller, Nov 17, 2014
  2. Junio C HamanoNov 17, 2014
  3. Stefan BellerNov 17, 2014
  4. Jonathan NiederNov 17, 2014
  5. Stefan BellerNov 18, 2014
  6. Jonathan NiederNov 18, 2014
  7. Stefan BellerNov 18, 2014
  8. 0/14 Re: copy.c: make copy_fd preserve meaningful errnoJonathan Nieder, Dec 3, 2014
  9. 01/14 strbuf: introduce strbuf_prefixf()Jonathan Nieder, Dec 3, 2014
  10. Eric SunshineDec 3, 2014
  11. Jonathan NiederDec 3, 2014
  12. Junio C HamanoDec 3, 2014
  13. Jonathan NiederDec 3, 2014
  14. Jonathan NiederDec 3, 2014
  15. Junio C HamanoDec 3, 2014
  16. 02/14 add_to_alternates_file: respect GIT_OBJECT_DIRECTORYJonathan Nieder, Dec 3, 2014
  17. Junio C HamanoDec 3, 2014
  18. 03/14 copy_fd: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  19. Junio C HamanoDec 3, 2014
  20. Jonathan NiederDec 3, 2014
  21. Jeff KingDec 3, 2014
  22. Jonathan NiederDec 3, 2014
  23. Jeff KingDec 4, 2014
  24. Stefan BellerDec 4, 2014
  25. Jeff KingDec 4, 2014
  26. Michael HaggertyDec 10, 2014
  27. Junio C HamanoDec 10, 2014
  28. Jeff KingDec 10, 2014
  29. doc: document error handling functions and conventions (Re: [PATCH 03/14] copy_fd: pass error message back through a strbuf)Jonathan Nieder, Dec 4, 2014
  30. Junio C HamanoDec 4, 2014
  31. Jonathan NiederDec 4, 2014
  32. Jonathan NiederDec 4, 2014
  33. Jeff KingDec 4, 2014
  34. Junio C HamanoDec 4, 2014
  35. Jeff KingDec 5, 2014
  36. Junio C HamanoDec 5, 2014
  37. Jeff KingDec 7, 2014
  38. Junio C HamanoDec 9, 2014
  39. Jeff KingDec 9, 2014
  40. Junio C HamanoFeb 12, 2015
  41. Michael HaggertyFeb 17, 2015
  42. Junio C HamanoFeb 17, 2015
  43. Jeff KingFeb 17, 2015
  44. Junio C HamanoFeb 17, 2015
  45. Junio C HamanoDec 3, 2014
  46. Jonathan NiederDec 3, 2014
  47. Junio C HamanoDec 3, 2014
  48. Jonathan NiederDec 3, 2014
  49. Stefan BellerDec 3, 2014
  50. 04/14 hold_lock_file_for_append: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  51. Torsten BögershausenDec 3, 2014
  52. Jonathan NiederDec 3, 2014
  53. 05/14 lock_packed_refs: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  54. 06/14 lockfile: introduce flag for locks outside .gitJonathan Nieder, Dec 3, 2014
  55. Junio C HamanoDec 3, 2014
  56. Jonathan NiederDec 3, 2014
  57. Junio C HamanoDec 3, 2014
  58. Jonathan NiederDec 3, 2014
  59. Junio C HamanoDec 3, 2014
  60. Jonathan NiederDec 3, 2014
  61. Junio C HamanoDec 3, 2014
  62. Jeff KingDec 3, 2014
  63. Junio C HamanoDec 4, 2014
  64. Jonathan NiederDec 4, 2014
  65. 07/14 fast-import: use message from lockfile API when writing marks failsJonathan Nieder, Dec 3, 2014
  66. 08/14 credentials: use message from lockfile API when locking ~/.git-credentials failsJonathan Nieder, Dec 3, 2014
  67. 09/14 config: use message from lockfile API when locking config file failsJonathan Nieder, Dec 3, 2014
  68. Junio C HamanoDec 3, 2014
  69. Jonathan NiederDec 3, 2014
  70. 10/14 rerere: error out on autoupdate failureJonathan Nieder, Dec 3, 2014
  71. 11/14 hold_locked_index: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  72. 12/14 hold_lock_file_for_update: pass error message back through a strbufJonathan Nieder, Dec 3, 2014
  73. Jonathan NiederDec 3, 2014
  74. 13/14 lockfile: remove unused function 'unable_to_lock_die'Jonathan Nieder, Dec 3, 2014
  75. 14/14 lockfile: make 'unable_to_lock_message' privateJonathan Nieder, Dec 3, 2014
  76. Stefan BellerDec 3, 2014
  77. Junio C HamanoNov 18, 2014
  78. Junio C HamanoNov 18, 2014
  79. Michael HaggertyNov 21, 2014
  80. Michael HaggertyNov 21, 2014
  81. Junio C HamanoNov 21, 2014
  82. Jeff KingNov 21, 2014
  83. Junio C HamanoNov 21, 2014

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.