Skip to content

Commit 5e6c081

Browse files
authored
Fix heap overflow on uri parsing (#1185)
1 parent 34013d5 commit 5e6c081

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

source/uri.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ static void s_parse_scheme(struct uri_parser *parser, struct aws_byte_cursor *st
282282
return;
283283
}
284284

285-
/* make sure we didn't just pick up the port by mistake */
286-
if ((size_t)(location_of_colon - str->ptr) < str->len && *(location_of_colon + 1) != '/') {
285+
/* Ensure location_of_colon is not the last character before checking *(location_of_colon + 1) */
286+
if ((size_t)(location_of_colon - str->ptr) + 1 >= str->len || *(location_of_colon + 1) != '/') {
287+
/* make sure we didn't just pick up the port by mistake */
287288
parser->state = ON_AUTHORITY;
288289
return;
289290
}

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ add_test_case(uri_ipv4_parse)
513513
add_test_case(uri_invalid_scheme_parse)
514514
add_test_case(uri_invalid_port_parse)
515515
add_test_case(uri_port_too_large_parse)
516+
add_test_case(uri_single_colon_parse)
516517
add_test_case(uri_builder)
517518
add_test_case(uri_builder_from_string)
518519
add_test_case(test_uri_encode_path_rfc3986)

tests/uri_test.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,18 @@ static int s_test_uri_port_too_large_parse(struct aws_allocator *allocator, void
593593

594594
AWS_TEST_CASE(uri_port_too_large_parse, s_test_uri_port_too_large_parse);
595595

596+
static int s_test_uri_single_colon_parse(struct aws_allocator *allocator, void *ctx) {
597+
(void)ctx;
598+
const char *str_uri = ":";
599+
struct aws_byte_cursor uri_csr = aws_byte_cursor_from_array(str_uri, 1);
600+
struct aws_uri uri;
601+
ASSERT_SUCCESS(aws_uri_init_parse(&uri, allocator, &uri_csr));
602+
aws_uri_clean_up(&uri);
603+
return AWS_OP_SUCCESS;
604+
}
605+
606+
AWS_TEST_CASE(uri_single_colon_parse, s_test_uri_single_colon_parse);
607+
596608
static int s_test_uri_builder(struct aws_allocator *allocator, void *ctx) {
597609
(void)ctx;
598610
const char *str_uri =

0 commit comments

Comments
 (0)