from trunk:

r15171@tombo:  nickm | 2007-12-06 12:47:47 -0500
 Use GCC attributes (where available) to verify printf type-correctness.  Fix some bugs this turned up.


svn:r603
This commit is contained in:
Niels Provos 2007-12-19 04:52:28 +00:00
parent 3368cc7922
commit 108ee5f3a6
6 changed files with 43 additions and 27 deletions

View File

@ -19,6 +19,7 @@ Changes in current version:
o prefix {encode,decode}_tag functions with evtag to avoid collisions o prefix {encode,decode}_tag functions with evtag to avoid collisions
o Correctly handle DNS replies with no answers set (Fixes bug 1846282) 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 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: Changes in 1.4.0-beta:

View File

@ -173,7 +173,7 @@ event_base_new(void)
struct event_base *base; struct event_base *base;
if ((base = calloc(1, sizeof(struct event_base))) == NULL) if ((base = calloc(1, sizeof(struct event_base))) == NULL)
event_err(1, "%s: calloc"); event_err(1, "%s: calloc", __func__);
event_sigcb = NULL; event_sigcb = NULL;
event_gotsig = 0; event_gotsig = 0;

View File

@ -986,7 +986,11 @@ int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
@param ... arguments that will be passed to printf(3) @param ... arguments that will be passed to printf(3)
@return 0 if successful, or -1 if an error occurred @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
;
/** /**

35
http.c
View File

@ -1649,12 +1649,12 @@ evhttp_send_done(struct evhttp_connection *evcon, void *arg)
void void
evhttp_send_error(struct evhttp_request *req, int error, const char *reason) evhttp_send_error(struct evhttp_request *req, int error, const char *reason)
{ {
const char *fmt = "<HTML><HEAD>\n" #define ERR_FORMAT "<HTML><HEAD>\n" \
"<TITLE>%d %s</TITLE>\n" "<TITLE>%d %s</TITLE>\n" \
"</HEAD><BODY>\n" "</HEAD><BODY>\n" \
"<H1>Method Not Implemented</H1>\n" "<H1>Method Not Implemented</H1>\n" \
"Invalid method in request<P>\n" "Invalid method in request<P>\n" \
"</BODY></HTML>\n"; "</BODY></HTML>\n"
struct evbuffer *buf = evbuffer_new(); 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); 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); evhttp_send_page(req, buf);
evbuffer_free(buf); evbuffer_free(buf);
#undef ERR_FORMAT
} }
/* Requires that headers and response code are already set up */ /* 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) { if (req->chunked) {
evbuffer_add_printf(req->evcon->output_buffer, "%x\r\n", 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); evbuffer_add_buffer(req->evcon->output_buffer, databuf);
evhttp_write_buffer(req->evcon, NULL, NULL); evhttp_write_buffer(req->evcon, NULL, NULL);
@ -1829,7 +1830,8 @@ evhttp_decode_uri(const char *uri)
ret = malloc(strlen(uri) + 1); ret = malloc(strlen(uri) + 1);
if (ret == NULL) 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++) { for (i = j = 0; uri[i] != '\0'; i++) {
c = uri[i]; c = uri[i];
@ -1947,25 +1949,26 @@ evhttp_handle_request(struct evhttp_request *req, void *arg)
return; return;
} else { } else {
/* We need to send a 404 here */ /* We need to send a 404 here */
const char *fmt = "<html><head>" #define ERR_FORMAT "<html><head>" \
"<title>404 Not Found</title>" "<title>404 Not Found</title>" \
"</head><body>" "</head><body>" \
"<h1>Not Found</h1>" "<h1>Not Found</h1>" \
"<p>The requested URL %s was not found on this server.</p>" "<p>The requested URL %s was not found on this server.</p>"\
"</body></html>\n"; "</body></html>\n"
char *escaped_html = evhttp_htmlescape(req->uri); char *escaped_html = evhttp_htmlescape(req->uri);
struct evbuffer *buf = evbuffer_new(); struct evbuffer *buf = evbuffer_new();
evhttp_response_code(req, HTTP_NOTFOUND, "Not Found"); 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); free(escaped_html);
evhttp_send_page(req, buf); evhttp_send_page(req, buf);
evbuffer_free(buf); evbuffer_free(buf);
#undef ERR_FORMAT
} }
} }

20
log.h
View File

@ -27,12 +27,18 @@
#ifndef _LOG_H_ #ifndef _LOG_H_
#define _LOG_H_ #define _LOG_H_
void event_err(int eval, const char *fmt, ...); #ifdef __GNUC__
void event_warn(const char *fmt, ...); #define EV_CHECK_FMT(a,b) __attribute__((format(printf, a, b)))
void event_errx(int eval, const char *fmt, ...); #else
void event_warnx(const char *fmt, ...); #define EV_CHECK_FMT(a,b)
void event_msgx(const char *fmt, ...); #endif
void _event_debugx(const char *fmt, ...);
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 #ifdef USE_DEBUG
#define event_debug(x) _event_debugx x #define event_debug(x) _event_debugx x
@ -40,4 +46,6 @@ void _event_debugx(const char *fmt, ...);
#define event_debug(x) do {;} while (0) #define event_debug(x) do {;} while (0)
#endif #endif
#undef EV_CHECK_FMT
#endif #endif

View File

@ -249,7 +249,7 @@ evsignal_handler(int sig)
if(evsignal_base == NULL) { if(evsignal_base == NULL) {
event_warn( event_warn(
"%s: received signal %s, but have no base configured", "%s: received signal %d, but have no base configured",
__func__, sig); __func__, sig);
return; return;
} }