diff --git a/ChangeLog b/ChangeLog index a8618189..8fa85e14 100644 --- a/ChangeLog +++ b/ChangeLog @@ -19,6 +19,7 @@ Changes in current version: o prefix {encode,decode}_tag functions with evtag to avoid collisions o Correctly handle DNS replies with no answers set (Fixes bug 1846282) o The configure script now takes an --enable-gcc-warnigns option that turns on many optional gcc warnings. (Nick has been building with these for a while, but they might be useful to other developers.) + o When building with GCC, use the "format" attribute to verify type correctness of calls to printf-like functions. Changes in 1.4.0-beta: diff --git a/event.c b/event.c index dfad9fb6..0b6c55a9 100644 --- a/event.c +++ b/event.c @@ -173,7 +173,7 @@ event_base_new(void) struct event_base *base; if ((base = calloc(1, sizeof(struct event_base))) == NULL) - event_err(1, "%s: calloc"); + event_err(1, "%s: calloc", __func__); event_sigcb = NULL; event_gotsig = 0; diff --git a/event.h b/event.h index c5b16ad3..5afb40cc 100644 --- a/event.h +++ b/event.h @@ -986,7 +986,11 @@ int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *); @param ... arguments that will be passed to printf(3) @return 0 if successful, or -1 if an error occurred */ -int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...); +int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...) +#ifdef __GNUC__ + __attribute__((format(printf, 2, 3))) +#endif +; /** diff --git a/http.c b/http.c index ac45925d..fac2988d 100644 --- a/http.c +++ b/http.c @@ -1649,12 +1649,12 @@ evhttp_send_done(struct evhttp_connection *evcon, void *arg) void evhttp_send_error(struct evhttp_request *req, int error, const char *reason) { - const char *fmt = "\n" - "%d %s\n" - "\n" - "

Method Not Implemented

\n" - "Invalid method in request

\n" - "\n"; +#define ERR_FORMAT "\n" \ + "%d %s\n" \ + "\n" \ + "

Method Not Implemented

\n" \ + "Invalid method in request

\n" \ + "\n" struct evbuffer *buf = evbuffer_new(); @@ -1663,11 +1663,12 @@ evhttp_send_error(struct evhttp_request *req, int error, const char *reason) evhttp_response_code(req, error, reason); - evbuffer_add_printf(buf, fmt, error, reason); + evbuffer_add_printf(buf, ERR_FORMAT, error, reason); evhttp_send_page(req, buf); evbuffer_free(buf); +#undef ERR_FORMAT } /* Requires that headers and response code are already set up */ @@ -1722,7 +1723,7 @@ evhttp_send_reply_chunk(struct evhttp_request *req, struct evbuffer *databuf) { if (req->chunked) { evbuffer_add_printf(req->evcon->output_buffer, "%x\r\n", - EVBUFFER_LENGTH(databuf)); + (unsigned)EVBUFFER_LENGTH(databuf)); } evbuffer_add_buffer(req->evcon->output_buffer, databuf); evhttp_write_buffer(req->evcon, NULL, NULL); @@ -1829,8 +1830,9 @@ evhttp_decode_uri(const char *uri) ret = malloc(strlen(uri) + 1); if (ret == NULL) - event_err(1, "%s: malloc(%d)", __func__, strlen(uri) + 1); - + event_err(1, "%s: malloc(%lu)", __func__, + (unsigned long)(strlen(uri) + 1)); + for (i = j = 0; uri[i] != '\0'; i++) { c = uri[i]; if (c == '?') { @@ -1947,25 +1949,26 @@ evhttp_handle_request(struct evhttp_request *req, void *arg) return; } else { /* We need to send a 404 here */ - const char *fmt = "" - "404 Not Found" - "" - "

Not Found

" - "

The requested URL %s was not found on this server.

" - "\n"; +#define ERR_FORMAT "" \ + "404 Not Found" \ + "" \ + "

Not Found

" \ + "

The requested URL %s was not found on this server.

"\ + "\n" char *escaped_html = evhttp_htmlescape(req->uri); struct evbuffer *buf = evbuffer_new(); evhttp_response_code(req, HTTP_NOTFOUND, "Not Found"); - evbuffer_add_printf(buf, fmt, escaped_html); + evbuffer_add_printf(buf, ERR_FORMAT, escaped_html); free(escaped_html); - + evhttp_send_page(req, buf); evbuffer_free(buf); +#undef ERR_FORMAT } } diff --git a/log.h b/log.h index 1f843cf9..7bc6632b 100644 --- a/log.h +++ b/log.h @@ -27,12 +27,18 @@ #ifndef _LOG_H_ #define _LOG_H_ -void event_err(int eval, const char *fmt, ...); -void event_warn(const char *fmt, ...); -void event_errx(int eval, const char *fmt, ...); -void event_warnx(const char *fmt, ...); -void event_msgx(const char *fmt, ...); -void _event_debugx(const char *fmt, ...); +#ifdef __GNUC__ +#define EV_CHECK_FMT(a,b) __attribute__((format(printf, a, b))) +#else +#define EV_CHECK_FMT(a,b) +#endif + +void event_err(int eval, const char *fmt, ...) EV_CHECK_FMT(2,3); +void event_warn(const char *fmt, ...) EV_CHECK_FMT(1,2); +void event_errx(int eval, const char *fmt, ...) EV_CHECK_FMT(2,3); +void event_warnx(const char *fmt, ...) EV_CHECK_FMT(1,2); +void event_msgx(const char *fmt, ...) EV_CHECK_FMT(1,2); +void _event_debugx(const char *fmt, ...) EV_CHECK_FMT(1,2); #ifdef USE_DEBUG #define event_debug(x) _event_debugx x @@ -40,4 +46,6 @@ void _event_debugx(const char *fmt, ...); #define event_debug(x) do {;} while (0) #endif +#undef EV_CHECK_FMT + #endif diff --git a/signal.c b/signal.c index 682bd1c6..964ae37f 100644 --- a/signal.c +++ b/signal.c @@ -249,7 +249,7 @@ evsignal_handler(int sig) if(evsignal_base == NULL) { event_warn( - "%s: received signal %s, but have no base configured", + "%s: received signal %d, but have no base configured", __func__, sig); return; }