Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add bounds check to http_method_str().

  • Loading branch information...
commit add3018ce7281d87e4cc2b82c4884858a66eecd3 1 parent 9f92347
@bnoordhuis bnoordhuis authored
Showing with 18 additions and 2 deletions.
  1. +10 −2 http_parser.c
  2. +8 −0 test.c
View
12 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, "<unknown>");
}
View
8 test.c
@@ -2518,6 +2518,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
test_message (const struct message *message)
{
size_t raw_len = strlen(message->raw);
@@ -3026,6 +3033,7 @@ main (void)
//// API
test_preserve_data();
test_parse_url();
+ test_method_str();
//// OVERFLOW CONDITIONS
Please sign in to comment.
Something went wrong with that request. Please try again.