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

[PATCH v3 1/2] bugfix: fix broken time_buf paddings for git-blame

From
Jiang Xin <worldhello.net@gmail.com>
Date
Apr 21, 2014, 06:02 UTC
Message-ID
<e8340b76f5c2b676a9594018b3385ead451412fa.1398059411.git.worldhello.net@gmail.com>
In-Reply-To
<cover.1398059411.git.worldhello.net@gmail.com>

Command `git blame --date relative` aligns the date field with a fixed-width (defined by blame_date_width), and if time_str is shorter than that, it adds spaces for padding. But there are two bugs in the following codes:

        time_len = strlen(time_str);
        ...
        memset(time_buf + time_len, ' ', blame_date_width - time_len);
1. The type of blame_date_width is size_t, which is unsigned.  If
   time_len is greater than blame_date_width, the result of
   "blame_date_width - time_len" will never be a negative number, but a
   really big positive number, and will cause memory overwrite.
   This bug can be triggered if either l10n message for function
   show_date_relative() in date.c is longer than 30 characters, then
   `git blame --date relative` may exit abnormally.
2. When show blame information with relative time, the UTF-8 characters
   in time_str will break the alignment of columns after the date field.
   This is because the time_buf padding with spaces should have a
   constant display width, not a fixed strlen size.  So we should call
   utf8_strwidth() instead of strlen() for width calibration.
Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
---
 builtin/blame.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/builtin/blame.c b/builtin/blame.c
index 88cb799..35e95db 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -1556,22 +1556,29 @@ static void assign_blame(struct scoreboard *sb, int opt)
 static const char *format_time(unsigned long time, const char *tz_str,
 			       int show_raw_time)
 {
-	static char time_buf[128];
+	static struct strbuf time_buf = STRBUF_INIT;
 
+	strbuf_reset(&time_buf);
 	if (show_raw_time) {
-		snprintf(time_buf, sizeof(time_buf), "%lu %s", time, tz_str);
+		strbuf_addf(&time_buf, "%lu %s", time, tz_str);
 	}
 	else {
 		const char *time_str;
-		int time_len;
+		size_t time_width;
 		int tz;
 		tz = atoi(tz_str);
 		time_str = show_date(time, tz, blame_date_mode);
-		time_len = strlen(time_str);
-		memcpy(time_buf, time_str, time_len);
-		memset(time_buf + time_len, ' ', blame_date_width - time_len);
+		strbuf_addstr(&time_buf, time_str);
+		/*
+		 * Add space paddings to time_buf to display a fixed width
+		 * string, and use time_width for display width calibration.
+		 */
+		for (time_width = utf8_strwidth(time_str);
+		     time_width < blame_date_width;
+		     time_width++)
+			strbuf_addch(&time_buf, ' ');
 	}
-	return time_buf;
+	return time_buf.buf;
 }
 
 #define OUTPUT_ANNOTATE_COMPAT	001
-- 
1.9.2.476.ga74def0
Previous: Jiang XinNext: Jiang Xin
Message 11 of 20 in “blame: add correct paddings in time_buf for align”
  1. blame: add correct paddings in time_buf for alignJiang Xin, Apr 18, 2014
  2. Duy NguyenApr 18, 2014
  3. Junio C HamanoApr 18, 2014
  4. Duy NguyenApr 19, 2014
  5. 0/2 peroper align of datetime filed of git-blameJiang Xin, Apr 20, 2014
  6. 1/2 bugfix: fix broken time_buf paddings for git-blameJiang Xin, Apr 20, 2014
  7. Eric SunshineApr 20, 2014
  8. 2/2 blame: use different blame_date_width for different localeJiang Xin, Apr 20, 2014
  9. Junio C HamanoApr 20, 2014
  10. 0/2 peroper align of datetime filed of git-blameJiang Xin, Apr 21, 2014
  11. 1/2 bugfix: fix broken time_buf paddings for git-blameJiang Xin, Apr 21, 2014
  12. 2/2 blame: use a helper to get suitable blame_date_widthJiang Xin, Apr 21, 2014
  13. Junio C HamanoApr 21, 2014
  14. Junio C HamanoApr 21, 2014
  15. Jiang XinApr 22, 2014
  16. 0/2 peroper align of datetime filed of git-blameJiang Xin, Apr 22, 2014
  17. 1/2 bugfix: fix broken time_buf paddings for git-blameJiang Xin, Apr 22, 2014
  18. 2/2 blame: dynamic blame_date_width for different localesJiang Xin, Apr 22, 2014
  19. David KastrupApr 22, 2014
  20. Jiang XinApr 22, 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.