[zeromq-dev] [RFC] czmq: check printf format options
Pieter Hintjens
ph at imatix.com
Wed Mar 14 23:32:41 CET 2012
Oh my, that is ugly. Is there no way to make this prettier?
Anyhow, looks like a good patch, why not send a pull request.
-Pieter
On Wed, Mar 14, 2012 at 4:54 PM, Stephen Hemminger
<shemminger at vyatta.com> wrote:
> Add checking of zmsg and zsocket arguments that take printf formats
>
> Gcc has support for attributes that trap errors in format strings at
> compile time. Not highly esthetic but it works.
>
>
> diff --git a/include/zmsg.h b/include/zmsg.h
> index bf866ff..d0d6fcd 100644
> --- a/include/zmsg.h
> +++ b/include/zmsg.h
> @@ -27,6 +27,15 @@
> #ifndef __ZMSG_H_INCLUDED__
> #define __ZMSG_H_INCLUDED__
>
> +// If using GCC this causes checking of syntax of format strings
> +#ifndef GCC_PRINTFLIKE
> +#ifdef __GNUC__
> +#define GCC_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
> +#else
> +#define GCC_PRINTFLIKE(fmt,var) /*nothing*/
> +#endif
> +#endif
> +
> #ifdef __cplusplus
> extern "C" {
> #endif
> @@ -84,12 +93,14 @@ int
> // Push string as new frame to front of message.
> // Returns 0 on success, -1 on error.
> int
> - zmsg_pushstr (zmsg_t *self, const char *format, ...);
> + zmsg_pushstr (zmsg_t *self, const char *format, ...)
> + GCC_PRINTFLIKE(2,3);
>
> // Push string as new frame to end of message.
> // Returns 0 on success, -1 on error.
> int
> - zmsg_addstr (zmsg_t *self, const char *format, ...);
> + zmsg_addstr (zmsg_t *self, const char *format, ...)
> + GCC_PRINTFLIKE(2,3);
>
> // Pop frame off front of message, return as fresh string
> char *
> diff --git a/include/zsocket.h b/include/zsocket.h
> index 8e7b993..7737888 100644
> --- a/include/zsocket.h
> +++ b/include/zsocket.h
> @@ -27,10 +27,20 @@
> #ifndef __ZSOCKET_H_INCLUDED__
> #define __ZSOCKET_H_INCLUDED__
>
> +/* Macro for usage of GCC's printf compilation warnings */
> +#ifndef GCC_PRINTFLIKE
> +#ifdef __GNUC__
> +#define GCC_PRINTFLIKE(fmt,var) __attribute__((format(printf,fmt,var)))
> +#else
> +#define GCC_PRINTFLIKE(fmt,var) /*nothing*/
> +#endif
> +#endif
> +
> #ifdef __cplusplus
> extern "C" {
> #endif
>
> +
> // @interface
> // This port range is defined by IANA for dynamic or private ports
> // We use this when choosing a port for dynamic binding.
> @@ -54,12 +64,14 @@ void
> // bind succeeded with the specified port number. Always returns the
> // port number if successful.
> int
> - zsocket_bind (void *socket, const char *format, ...);
> + zsocket_bind (void *socket, const char *format, ...)
> + GCC_PRINTFLIKE(2,3);
>
> // Connect a socket to a formatted endpoint
> // Returns 0 if OK, -1 if the endpoint was invalid.
> int
> - zsocket_connect (void *socket, const char *format, ...);
> + zsocket_connect (void *socket, const char *format, ...)
> + GCC_PRINTFLIKE(2,3);
>
> // Returns socket type as printable constant string
> char *
>
More information about the zeromq-dev
mailing list