Skip to content

Commit cf504eb

Browse files
committed
feat: add ada_clear_* methods to c api
1 parent 24d3812 commit cf504eb

File tree

6 files changed

+57
-5
lines changed

6 files changed

+57
-5
lines changed

fuzz/parse.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
7676
length += out_aggregator->get_hash().size();
7777
length += out_aggregator->get_origin().size();
7878
length += out_aggregator->get_port().size();
79+
80+
// clear methods
81+
out_aggregator->clear_port();
82+
out_aggregator->clear_pathname();
83+
out_aggregator->clear_search();
84+
out_aggregator->clear_hash();
7985
}
8086

8187
/**

include/ada/url_aggregator-inl.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,9 @@ inline void url_aggregator::clear_hostname() {
700700
" with " + components.to_string() + "\n" + to_diagram());
701701
#endif
702702
ADA_ASSERT_TRUE(has_authority());
703-
ADA_ASSERT_TRUE(has_empty_hostname());
703+
ADA_ASSERT_EQUAL(has_empty_hostname(), true,
704+
"hostname should have been cleared on buffer=" + buffer +
705+
" with " + components.to_string() + "\n" + to_diagram());
704706
ADA_ASSERT_TRUE(validate());
705707
}
706708

include/ada/url_aggregator.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ struct url_aggregator : url_base {
196196
/** @return true if the URL has a search component */
197197
[[nodiscard]] inline bool has_search() const noexcept override;
198198

199+
inline void clear_port();
200+
inline void clear_hash();
201+
inline void clear_pathname() override;
202+
inline void clear_search() override;
203+
199204
private:
200205
friend ada::url_aggregator ada::parser::parse_url<ada::url_aggregator>(
201206
std::string_view, const ada::url_aggregator *);
@@ -270,11 +275,7 @@ struct url_aggregator : url_base {
270275
inline void update_base_port(uint32_t input);
271276
inline void append_base_pathname(const std::string_view input);
272277
inline uint32_t retrieve_base_port() const;
273-
inline void clear_port();
274278
inline void clear_hostname();
275-
inline void clear_hash();
276-
inline void clear_pathname() override;
277-
inline void clear_search() override;
278279
inline void clear_password();
279280
inline bool has_dash_dot() const noexcept;
280281
void delete_dash_dot();

include/ada_c.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ bool ada_set_pathname(ada_url result, const char* input, size_t length);
8484
void ada_set_search(ada_url result, const char* input, size_t length);
8585
void ada_set_hash(ada_url result, const char* input, size_t length);
8686

87+
// url_aggregator clear methods
88+
void ada_clear_port(ada_url result);
89+
void ada_clear_hash(ada_url result);
90+
void ada_clear_pathname(ada_url result);
91+
void ada_clear_search(ada_url result);
92+
8793
// url_aggregator functions
8894
// if ada_is_valid(result) is false, functions below will return false
8995
bool ada_has_credentials(ada_url result);

src/ada_c.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,34 @@ void ada_set_hash(ada_url result, const char* input, size_t length) noexcept {
301301
}
302302
}
303303

304+
void ada_clear_port(ada_url result) noexcept {
305+
ada::result<ada::url_aggregator>& r = get_instance(result);
306+
if (r) {
307+
r->clear_port();
308+
}
309+
}
310+
311+
void ada_clear_hash(ada_url result) noexcept {
312+
ada::result<ada::url_aggregator>& r = get_instance(result);
313+
if (r) {
314+
r->clear_hash();
315+
}
316+
}
317+
318+
void ada_clear_pathname(ada_url result) noexcept {
319+
ada::result<ada::url_aggregator>& r = get_instance(result);
320+
if (r) {
321+
r->clear_pathname();
322+
}
323+
}
324+
325+
void ada_clear_search(ada_url result) noexcept {
326+
ada::result<ada::url_aggregator>& r = get_instance(result);
327+
if (r) {
328+
r->clear_search();
329+
}
330+
}
331+
304332
bool ada_has_credentials(ada_url result) noexcept {
305333
ada::result<ada::url_aggregator>& r = get_instance(result);
306334
if (!r) {

tests/ada_c.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,14 @@ TEST(ada_c, setters) {
8787

8888
ada_set_port(url, "4242", 4);
8989
ASSERT_EQ(convert_string(ada_get_port(url)), "4242");
90+
ada_clear_port(url);
91+
ASSERT_EQ(convert_string(ada_get_port(url)), "");
92+
ASSERT_FALSE(ada_has_port(url));
9093

9194
ada_set_hash(url, "new-hash", strlen("new-hash"));
9295
ASSERT_EQ(convert_string(ada_get_hash(url)), "#new-hash");
96+
ada_clear_hash(url);
97+
ASSERT_FALSE(ada_has_hash(url));
9398

9499
ada_set_hostname(url, "new-host", strlen("new-host"));
95100
ASSERT_EQ(convert_string(ada_get_hostname(url)), "new-host");
@@ -99,9 +104,13 @@ TEST(ada_c, setters) {
99104

100105
ada_set_pathname(url, "new-pathname", strlen("new-pathname"));
101106
ASSERT_EQ(convert_string(ada_get_pathname(url)), "/new-pathname");
107+
ada_clear_pathname(url);
108+
ASSERT_EQ(convert_string(ada_get_pathname(url)), "");
102109

103110
ada_set_search(url, "new-search", strlen("new-search"));
104111
ASSERT_EQ(convert_string(ada_get_search(url)), "?new-search");
112+
ada_clear_search(url);
113+
ASSERT_EQ(convert_string(ada_get_search(url)), "");
105114

106115
ada_set_protocol(url, "wss", 3);
107116
ASSERT_EQ(convert_string(ada_get_protocol(url)), "wss:");

0 commit comments

Comments
 (0)