From add3018ce7281d87e4cc2b82c4884858a66eecd3 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 29 Aug 2012 23:22:53 +0200 Subject: [PATCH] Add bounds check to http_method_str(). --- http_parser.c | 12 ++++++++++-- test.c | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/http_parser.c b/http_parser.c index 8fbbab1..b5c9081 100644 --- a/http_parser.c +++ b/http_parser.c @@ -37,6 +37,13 @@ # define MIN(a,b) ((a) < (b) ? (a) : (b)) #endif +#ifndef ARRAY_SIZE +# define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) +#endif + +#ifndef ELEM_AT +# define ELEM_AT(a, i, v) ((unsigned int) (i) < ARRAY_SIZE(a) ? (a)[(i)] : (v)) +#endif #if HTTP_PARSER_DEBUG #define SET_ERRNO(e) \ @@ -1882,9 +1889,10 @@ http_should_keep_alive (const http_parser *parser) } -const char * http_method_str (enum http_method m) +const char * +http_method_str (enum http_method m) { - return method_strings[m]; + return ELEM_AT(method_strings, m, ""); } diff --git a/test.c b/test.c index 1ca8f05..81e0c3b 100644 --- a/test.c +++ b/test.c @@ -2517,6 +2517,13 @@ test_parse_url (void) } } +void +test_method_str (void) +{ + assert(0 == strcmp("GET", http_method_str(HTTP_GET))); + assert(0 == strcmp("", http_method_str(1337))); +} + void test_message (const struct message *message) { @@ -3026,6 +3033,7 @@ main (void) //// API test_preserve_data(); test_parse_url(); + test_method_str(); //// OVERFLOW CONDITIONS