88
99using regex_provider = ada::url_pattern_regex::std_regex_provider;
1010
11- std::string bytesToAlphanumeric (const std::string& source) {
12- static const char alphanumeric[] =
13- " abcdefghijklmnopqrstuvwxyz"
14- " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
15- " 0123456789" ;
16-
17- std::string result;
18- result.reserve (source.size ());
19-
20- for (char byte : source) {
21- int index = static_cast <unsigned char >(byte) % (sizeof (alphanumeric) - 1 );
22- result.push_back (alphanumeric[index]);
23- }
24-
25- return result;
26- }
27-
2811void exercise_result (auto result) {
2912 (void )result.get_protocol ();
3013 (void )result.get_username ();
@@ -39,14 +22,21 @@ void exercise_result(auto result) {
3922}
4023
4124extern " C" int LLVMFuzzerTestOneInput (const uint8_t * data, size_t size) {
25+ auto to_ascii = [](const std::string& source) -> std::string {
26+ std::string result;
27+ result.reserve (source.size ());
28+ for (char c : source) {
29+ result.push_back (static_cast <unsigned char >(c) % 128 );
30+ }
31+ return result;
32+ };
4233 FuzzedDataProvider fdp (data, size);
4334 // We do not want to trigger arbitrary regex matching.
44- std::string source_1 =
45- " /" + bytesToAlphanumeric (fdp.ConsumeRandomLengthString (50 )) + " /" +
46- bytesToAlphanumeric (fdp.ConsumeRandomLengthString (50 ));
47- std::string base_source_1 =
48- " /" + bytesToAlphanumeric (fdp.ConsumeRandomLengthString (50 )) + " /" +
49- bytesToAlphanumeric (fdp.ConsumeRandomLengthString (50 ));
35+ std::string source_1 = " /" + to_ascii (fdp.ConsumeRandomLengthString (50 )) +
36+ " /" + to_ascii (fdp.ConsumeRandomLengthString (50 ));
37+ std::string base_source_1 = " /" +
38+ to_ascii (fdp.ConsumeRandomLengthString (50 )) +
39+ " /" + to_ascii (fdp.ConsumeRandomLengthString (50 ));
5040
5141 std::string source_2 = " https://ada-url.com/*" ;
5242 std::string base_source_2 = " https://ada-url.com" ;
@@ -76,14 +66,35 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
7666 exercise_result (*result_with_base_and_options);
7767
7868 // Testing with url_pattern_init and base url.
79- ada::url_pattern_init init{.protocol = source,
80- .username = source,
81- .password = source,
82- .hostname = source,
83- .port = source,
84- .pathname = source,
85- .search = source,
86- .hash = source};
69+ int field_index = fdp.ConsumeIntegralInRange (0 , 7 );
70+ std::string random_value = to_ascii (fdp.ConsumeRandomLengthString (50 ));
71+ ada::url_pattern_init init{};
72+ switch (field_index) {
73+ case 0 :
74+ init.protocol = random_value;
75+ break ;
76+ case 1 :
77+ init.username = random_value;
78+ break ;
79+ case 2 :
80+ init.password = random_value;
81+ break ;
82+ case 3 :
83+ init.hostname = random_value;
84+ break ;
85+ case 4 :
86+ init.port = random_value;
87+ break ;
88+ case 5 :
89+ init.pathname = random_value;
90+ break ;
91+ case 6 :
92+ init.search = random_value;
93+ break ;
94+ case 7 :
95+ init.hash = random_value;
96+ break ;
97+ }
8798 auto result_with_init = ada::parse_url_pattern<regex_provider>(
8899 init, &base_source_view, nullptr );
89100 if (result_with_init) exercise_result (*result_with_init);
0 commit comments