Add bounds check to http_method_str().

v0.10
Ben Noordhuis 13 years ago
parent 9f92347851
commit add3018ce7

@ -37,6 +37,13 @@
# define MIN(a,b) ((a) < (b) ? (a) : (b)) # define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif #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 #if HTTP_PARSER_DEBUG
#define SET_ERRNO(e) \ #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, "<unknown>");
} }

@ -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("<unknown>", http_method_str(1337)));
}
void void
test_message (const struct message *message) test_message (const struct message *message)
{ {
@ -3026,6 +3033,7 @@ main (void)
//// API //// API
test_preserve_data(); test_preserve_data();
test_parse_url(); test_parse_url();
test_method_str();
//// OVERFLOW CONDITIONS //// OVERFLOW CONDITIONS

Loading…
Cancel
Save