chprintf Argument Checking with gcc

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
Post Reply
efs113
Posts: 14
Joined: Mon Mar 19, 2012 3:51 pm

chprintf Argument Checking with gcc

Post by efs113 »

Hi All,

gcc has the capability to check arguments on printf() like functions. The patch below adds this ability to chprintf() when using gcc. As chprintf() doesn't support the full printf() format, it won't catch everything, but it will warn if you pass an int type for a '%s', have too many/few arguments, etc ...

Code: Select all

Index: os/various/chprintf.h
===================================================================
--- os/various/chprintf.h   (revision 865)
+++ os/various/chprintf.h   (working copy)
@@ -39,7 +39,11 @@
 #ifdef __cplusplus
 extern "C" {
 #endif
+#ifdef __GNUC__
+  void chprintf(BaseChannel *chp, const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
+#else
   void chprintf(BaseChannel *chp, const char *fmt, ...);
+#endif
 #ifdef __cplusplus
 }
 #endif


Eric
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: chprintf Argument Checking with gcc

Post by Giovanni »

Hi Eric,

Thanks for the patch, very useful, I will merge it.

Giovanni
Post Reply