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

[PATCH 1/3] add git_psprintf helper function

From
Maxim Moseychuk <franchesko.salias.hudro.pedros@gmail.com>
Date
Feb 16, 2017, 11:28 UTC
Message-ID
<20170216112829.18079-2-franchesko.salias.hudro.pedros@gmail.com>
In-Reply-To
<20170216112829.18079-1-franchesko.salias.hudro.pedros@gmail.com>

There are a number of places in the code where we call xsnprintf(), with the assumption that the output will fit into the buffer. If the buffer is small, then git die. In many places buffers have compile-time size, but generated string depends from current system locale (gettext)and can have size greater the buffer. Just run "LANG=ru_RU.UTF8 git bisect start v4.9 v4.8" on linux sources - it impossible.

git_psprintf is similar to the standard C sprintf() function but safer, since it calculates the maximum space required and allocates memory to hold the result. The returned string should be freed with free() when no longer needed.

Signed-off-by: Maxim Moseychuk <franchesko.salias.hudro.pedros@gmail.com>
---
 git-compat-util.h |  3 +++
 wrapper.c         | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h
index 87237b092..fa98705d0 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -878,6 +878,9 @@ static inline size_t xsize_t(off_t len)
 	return (size_t)len;
 }
 
+__attribute__((format (printf, 1, 2)))
+extern char *git_psprintf(const char *fmt, ...);
+
 __attribute__((format (printf, 3, 4)))
 extern int xsnprintf(char *dst, size_t max, const char *fmt, ...);
 
diff --git a/wrapper.c b/wrapper.c
index e7f197996..deee46d2d 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -635,6 +635,25 @@ char *xgetcwd(void)
 	return strbuf_detach(&sb, NULL);
 }
 
+char *git_psprintf(const char *fmt, ...)
+{
+	va_list ap;
+	int len;
+	char *dst;
+
+	va_start(ap, fmt);
+	len = vsnprintf(NULL, 0, fmt, ap);
+	va_end(ap);
+
+	dst = xmallocz(len);
+
+	va_start(ap, fmt);
+	vsprintf(dst, fmt, ap);
+	va_end(ap);
+
+	return dst;
+}
+
 int xsnprintf(char *dst, size_t max, const char *fmt, ...)
 {
 	va_list ap;
-- 
2.11.1
Previous: Maxim MoseychukNext: Jonathan Tan
Message 2 of 9 in “Fix l10n”
  1. 0/3 Fix l10nMaxim Moseychuk, Feb 16, 2017
  2. 1/3 add git_psprintf helper functionMaxim Moseychuk, Feb 16, 2017
  3. Jonathan TanFeb 16, 2017
  4. Jeff KingFeb 16, 2017
  5. 3/3 stop_progress_msg: convert xsnprintf to git_psprintfMaxim Moseychuk, Feb 16, 2017
  6. Jeff KingFeb 16, 2017
  7. 2/3 bisect_next_all: convert xsnprintf to git_psprintfMaxim Moseychuk, Feb 16, 2017
  8. Jeff KingFeb 16, 2017
  9. Jeff KingFeb 16, 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.