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

[PATCH] pretty.c: Fix a compiler warning

From
Ramsay Jones <ramsay@ramsay1.demon.co.uk>
Date
Oct 4, 2012, 18:06 UTC
Message-ID
<506DD02D.2060809@ramsay1.demon.co.uk>
In particular, gcc complains thus:
        CC pretty.o
    pretty.c: In function 'format_commit_item':
    pretty.c:1282: warning: 'offset' might be used uninitialized in \
        this function

In order to suppress the warning we simply initialize the 'offset' variable in it's declarartion.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
---
Hi Nguyen,

If you need to re-roll your 'nd/pretty-placeholder-with-color-option' branch, could you please squash this (or something like it) into the relevant commit. [commit f1da75df ("pretty: support padding placeholders, %< %> and %><", 23-09-2012). NOTE: this patch is on top of pu@a78f4186]

An alternative solution looks like (this will probably break git-am):
--- >8 ---
diff --git a/pretty.c b/pretty.c
index deeacf2..25d8eb2 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1292,6 +1292,8 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
 			break;
 		case flush_left_and_steal:
 		case no_flush: /* to make gcc happy */
+		default:
+			offset = 0;
 			break;
 		}
 		/*
--- 8< ---

... but this disables the "enumeration value 'no_flush' not handled in switch"
type warnings.

[Actually, I would not bother with a switch at all; I find this:

	} else {
		int sb_len = sb->len, offset = 0;
		if (c->flush_type == flush_left)
			offset = padding - len;
		else if (c->flush_type == flush_both)
			offset = (padding - len) / 2;
		...

... much more readable. But that's just me ... :-D ]

Thanks!

ATB,
Ramsay Jones

 pretty.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pretty.c b/pretty.c
index deeacf2..d26428b 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1279,7 +1279,7 @@ static size_t format_and_pad_commit(struct strbuf *sb, /* in UTF-8 */
 		}
 		strbuf_addstr(sb, local_sb.buf);
 	} else {
-		int sb_len = sb->len, offset;
+		int sb_len = sb->len, offset = 0;
 		switch (c->flush_type) {
 		case flush_left:
 			offset = padding - len;
-- 
1.7.12
Message 1 of 1 in “pretty.c: Fix a compiler warning”
  1. pretty.c: Fix a compiler warningRamsay Jones, Oct 4, 2012

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.