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

[PATCH 3/3] wt-status: use "format" function attribute for status_printf

From
Jeff King <peff@peff.net>
Date
Jul 10, 2013, 00:23 UTC
Message-ID
<20130710002328.GC19423@sigill.intra.peff.net>
In-Reply-To
<20130710001659.GA11643@sigill.intra.peff.net>

These functions could benefit from the added compile-time safety of having the compiler check printf arguments.

Unfortunately, we also sometimes pass an empty format string, which will cause false positives with -Wformat-zero-length. In this case, that warning is wrong because our function is not a no-op with an empty format: it may be printing colorized output along with a trailing newline.

Signed-off-by: Jeff King <peff@peff.net>
---
I'm torn on this one. It really does provide us with more compile-time
safety checks, but it's annoying that "-Wall -Werror" will no longer
work out of the box.

We could also add a status_printf_empty_line() function, but that feels like a bit of a hack. Searching online, I also found the amusing suggestion to do:

  status_printf_ln(s, color, "%.*s", 0,
                   "-Wformat-zero-length please choke on a bucket of socks");
but I think that is probably worse than adding a specialized function.
 wt-status.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/wt-status.h b/wt-status.h
index 4121bc2..fb7152e 100644
--- a/wt-status.h
+++ b/wt-status.h
@@ -96,9 +96,9 @@ void wt_porcelain_print(struct wt_status *s);
 void wt_shortstatus_print(struct wt_status *s);
 void wt_porcelain_print(struct wt_status *s);
 
-void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, ...)
-	;
-void status_printf(struct wt_status *s, const char *color, const char *fmt, ...)
-	;
+__attribute__((format (printf, 3, 4)))
+void status_printf_ln(struct wt_status *s, const char *color, const char *fmt, ...);
+__attribute__((format (printf, 3, 4)))
+void status_printf(struct wt_status *s, const char *color, const char *fmt, ...);
 
 #endif /* STATUS_H */
-- 
1.8.3.rc3.24.gec82cb9
Previous: Jeff KingNext: Junio C Hamano
Message 8 of 17 in “remote-http: use argv-array”
  1. remote-http: use argv-arrayJunio C Hamano, Jul 9, 2013
  2. Bert WesargJul 9, 2013
  3. Jeff KingJul 9, 2013
  4. Matt KraaiJul 9, 2013
  5. Jeff KingJul 10, 2013
  6. 1/3 add missing "format" function attributesJeff King, Jul 10, 2013
  7. 2/3 use "sentinel" function attribute for variadic listsJeff King, Jul 10, 2013
  8. 3/3 wt-status: use "format" function attribute for status_printfJeff King, Jul 10, 2013
  9. Junio C HamanoJul 10, 2013
  10. Jeff KingJul 10, 2013
  11. Junio C HamanoJul 10, 2013
  12. Jeff KingJul 10, 2013
  13. Junio C HamanoJul 10, 2013
  14. Jeff KingJul 10, 2013
  15. Junio C HamanoJul 10, 2013
  16. Junio C HamanoJul 12, 2013
  17. Jeff KingJul 12, 2013

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.