Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ptr typedefs #212

Merged
merged 3 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ std::optional<event> match_rule(rule *rule, const object_store &store,
memory::unordered_map<ddwaf::rule *, rule::cache_type> &cache,
const memory::unordered_map<ddwaf::rule *, exclusion::filter_mode> &rules_to_exclude,
const memory::unordered_map<ddwaf::rule *, collection::object_set> &objects_to_exclude,
const std::unordered_map<std::string, matcher::base::shared_ptr> &dynamic_matchers,
const std::unordered_map<std::string, std::shared_ptr<matcher::base>> &dynamic_matchers,
ddwaf::timer &deadline)
{
const auto &id = rule->get_id();
Expand Down Expand Up @@ -79,7 +79,7 @@ void base_collection<Derived>::match(memory::vector<event> &events, const object
collection_cache &cache,
const memory::unordered_map<ddwaf::rule *, exclusion::filter_mode> &rules_to_exclude,
const memory::unordered_map<rule *, object_set> &objects_to_exclude,
const std::unordered_map<std::string, matcher::base::shared_ptr> &dynamic_matchers,
const std::unordered_map<std::string, std::shared_ptr<matcher::base>> &dynamic_matchers,
ddwaf::timer &deadline) const
{
if (cache.result >= Derived::type()) {
Expand Down
4 changes: 2 additions & 2 deletions src/collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ template <typename Derived> class base_collection {
base_collection &operator=(const base_collection &) = default;
base_collection &operator=(base_collection &&) noexcept = default;

void insert(const rule::ptr &rule) { rules_.emplace_back(rule.get()); }
void insert(const std::shared_ptr<rule> &rule) { rules_.emplace_back(rule.get()); }

void match(memory::vector<event> &events /* output */, const object_store &store,
collection_cache &cache,
const memory::unordered_map<ddwaf::rule *, exclusion::filter_mode> &rules_to_exclude,
const memory::unordered_map<ddwaf::rule *, object_set> &objects_to_exclude,
const std::unordered_map<std::string, matcher::base::shared_ptr> &dynamic_matchers,
const std::unordered_map<std::string, std::shared_ptr<matcher::base>> &dynamic_matchers,
ddwaf::timer &deadline) const;

protected:
Expand Down
6 changes: 3 additions & 3 deletions src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class context {
public:
using object_set = std::unordered_set<const ddwaf_object *>;

explicit context(ruleset::ptr ruleset) : ruleset_(std::move(ruleset))
explicit context(std::shared_ptr<ruleset> ruleset) : ruleset_(std::move(ruleset))
{
rule_filter_cache_.reserve(ruleset_->rule_filters.size());
input_filter_cache_.reserve(ruleset_->input_filters.size());
Expand Down Expand Up @@ -75,7 +75,7 @@ class context {
}
return false;
}
ruleset::ptr ruleset_;
std::shared_ptr<ruleset> ruleset_;
ddwaf::object_store store_;

using input_filter = exclusion::input_filter;
Expand All @@ -96,7 +96,7 @@ class context {

class context_wrapper {
public:
explicit context_wrapper(ruleset::ptr ruleset)
explicit context_wrapper(std::shared_ptr<ruleset> ruleset)
{
memory::memory_resource_guard guard(&mr_);
ctx_ = static_cast<context *>(mr_.allocate(sizeof(context), alignof(context)));
Expand Down
4 changes: 2 additions & 2 deletions src/exclusion/input_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace ddwaf::exclusion {

using excluded_set = input_filter::excluded_set;

input_filter::input_filter(std::string id, expression::ptr expr, std::set<rule *> rule_targets,
std::shared_ptr<object_filter> filter)
input_filter::input_filter(std::string id, std::shared_ptr<expression> expr,
std::set<rule *> rule_targets, std::shared_ptr<object_filter> filter)
: id_(std::move(id)), expr_(std::move(expr)), rule_targets_(std::move(rule_targets)),
filter_(std::move(filter))
{
Expand Down
6 changes: 2 additions & 4 deletions src/exclusion/input_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ namespace ddwaf::exclusion {

class input_filter {
public:
using ptr = std::shared_ptr<input_filter>;

struct excluded_set {
const std::set<rule *> &rules;
memory::unordered_set<const ddwaf_object *> objects;
Expand All @@ -31,7 +29,7 @@ class input_filter {
object_filter::cache_type object_filter_cache;
};

input_filter(std::string id, expression::ptr expr, std::set<rule *> rule_targets,
input_filter(std::string id, std::shared_ptr<expression> expr, std::set<rule *> rule_targets,
std::shared_ptr<object_filter> filter);
input_filter(const input_filter &) = delete;
input_filter &operator=(const input_filter &) = delete;
Expand All @@ -52,7 +50,7 @@ class input_filter {

protected:
std::string id_;
expression::ptr expr_;
std::shared_ptr<expression> expr_;
const std::set<rule *> rule_targets_;
std::shared_ptr<object_filter> filter_;
};
Expand Down
4 changes: 2 additions & 2 deletions src/exclusion/rule_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

namespace ddwaf::exclusion {

rule_filter::rule_filter(
std::string id, expression::ptr expr, std::set<rule *> rule_targets, filter_mode mode)
rule_filter::rule_filter(std::string id, std::shared_ptr<expression> expr,
std::set<rule *> rule_targets, filter_mode mode)
: id_(std::move(id)), expr_(std::move(expr)), mode_(mode)
{
if (!expr_) {
Expand Down
5 changes: 2 additions & 3 deletions src/exclusion/rule_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ enum class filter_mode { bypass, monitor };

class rule_filter {
public:
using ptr = std::shared_ptr<rule_filter>;
using cache_type = expression::cache_type;

rule_filter(std::string id, expression::ptr expr, std::set<rule *> rule_targets,
rule_filter(std::string id, std::shared_ptr<expression> expr, std::set<rule *> rule_targets,
filter_mode mode = filter_mode::bypass);
rule_filter(const rule_filter &) = delete;
rule_filter &operator=(const rule_filter &) = delete;
Expand All @@ -44,7 +43,7 @@ class rule_filter {

protected:
std::string id_;
expression::ptr expr_;
std::shared_ptr<expression> expr_;
std::unordered_set<rule *> rule_targets_;
filter_mode mode_;
};
Expand Down
4 changes: 2 additions & 2 deletions src/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ bool expression::evaluator::eval()
// run, then they having new data will make them match again. The condition
// that failed (and stopped the processing), we can run it again, but only
// on the new data. The subsequent conditions, we need to run with all data.
std::vector<condition::ptr>::const_iterator cond_iter;
std::vector<std::unique_ptr<condition>>::const_iterator cond_iter;
bool run_on_new;
if (cache.last_cond.has_value()) {
cond_iter = *cache.last_cond;
Expand Down Expand Up @@ -203,7 +203,7 @@ bool expression::evaluator::eval()

bool expression::eval(cache_type &cache, const object_store &store,
const std::unordered_set<const ddwaf_object *> &objects_excluded,
const std::unordered_map<std::string, matcher::base::shared_ptr> &dynamic_matchers,
const std::unordered_map<std::string, std::shared_ptr<matcher::base>> &dynamic_matchers,
ddwaf::timer &deadline) const
{
if (cache.result || conditions_.empty()) {
Expand Down
24 changes: 11 additions & 13 deletions src/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,9 @@ namespace ddwaf {

class expression {
public:
using ptr = std::shared_ptr<expression>;

enum class data_source : uint8_t { values, keys };

struct condition {
using ptr = std::unique_ptr<condition>;

struct cache_type {
bool result{false};
Expand All @@ -50,14 +47,14 @@ class expression {
};

std::vector<target_type> targets;
matcher::base::unique_ptr matcher;
std::unique_ptr<matcher::base> matcher;
std::string data_id;
};

struct cache_type {
bool result{false};
memory::vector<event::match> matches{};
std::optional<std::vector<condition::ptr>::const_iterator> last_cond{};
std::optional<std::vector<std::unique_ptr<condition>>::const_iterator> last_cond{};
};

struct evaluator {
Expand All @@ -75,22 +72,23 @@ class expression {

ddwaf::timer &deadline;
const ddwaf::object_limits &limits;
const std::vector<condition::ptr> &conditions;
const std::vector<std::unique_ptr<condition>> &conditions;
const object_store &store;
const std::unordered_set<const ddwaf_object *> &objects_excluded;
const std::unordered_map<std::string, matcher::base::shared_ptr> &dynamic_matchers;
const std::unordered_map<std::string, std::shared_ptr<matcher::base>> &dynamic_matchers;
cache_type &cache;
};

expression() = default;

explicit expression(std::vector<condition::ptr> &&conditions, ddwaf::object_limits limits = {})
explicit expression(
std::vector<std::unique_ptr<condition>> &&conditions, ddwaf::object_limits limits = {})
: limits_(limits), conditions_(std::move(conditions))
{}

bool eval(cache_type &cache, const object_store &store,
const std::unordered_set<const ddwaf_object *> &objects_excluded,
const std::unordered_map<std::string, matcher::base::shared_ptr> &dynamic_matchers,
const std::unordered_map<std::string, std::shared_ptr<matcher::base>> &dynamic_matchers,
ddwaf::timer &deadline) const;

void get_addresses(std::unordered_map<target_index, std::string> &addresses) const
Expand All @@ -114,7 +112,7 @@ class expression {

protected:
ddwaf::object_limits limits_;
std::vector<condition::ptr> conditions_;
std::vector<std::unique_ptr<condition>> conditions_;
};

class expression_builder {
Expand Down Expand Up @@ -152,7 +150,7 @@ class expression_builder {
cond->matcher = std::make_unique<T>(args...);
}

void set_matcher(matcher::base::unique_ptr &&matcher)
void set_matcher(std::unique_ptr<matcher::base> &&matcher)
{
auto &cond = conditions_.back();
cond->matcher = std::move(matcher);
Expand All @@ -162,14 +160,14 @@ class expression_builder {
std::vector<transformer_id> transformers = {},
expression::data_source source = expression::data_source::values);

expression::ptr build()
std::shared_ptr<expression> build()
{
return std::make_shared<expression>(std::move(conditions_), limits_);
}

protected:
ddwaf::object_limits limits_{};
std::vector<expression::condition::ptr> conditions_{};
std::vector<std::unique_ptr<expression::condition>> conditions_{};
};

} // namespace ddwaf
2 changes: 0 additions & 2 deletions src/generator/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace ddwaf::generator {

class base {
public:
using ptr = std::unique_ptr<base>;

base() = default;
virtual ~base() = default;
base(const base &) = default;
Expand Down
5 changes: 0 additions & 5 deletions src/matcher/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ namespace ddwaf::matcher {

class base {
public:
using shared_ptr = std::shared_ptr<base>;
using unique_ptr = std::unique_ptr<base>;

base() = default;
virtual ~base() = default;
base(const base &) = default;
Expand All @@ -43,8 +40,6 @@ class base {

template <typename T> class base_impl : public base {
public:
using ptr = std::shared_ptr<base>;

base_impl() = default;
~base_impl() override = default;
base_impl(const base_impl &) = default;
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parser_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace ddwaf::parser::v1 {

namespace {

expression::ptr parse_expression(parameter::vector &conditions_array,
std::shared_ptr<expression> parse_expression(parameter::vector &conditions_array,
const std::vector<transformer_id> &transformers, ddwaf::object_limits limits)
{
expression_builder builder(conditions_array.size(), limits);
Expand All @@ -38,7 +38,7 @@ expression::ptr parse_expression(parameter::vector &conditions_array,
auto params = at<parameter::map>(cond, "parameters");

parameter::map options;
matcher::base::unique_ptr matcher;
std::unique_ptr<matcher::base> matcher;
if (matcher_name == "phrase_match") {
auto list = at<parameter::vector>(params, "list");

Expand Down
14 changes: 7 additions & 7 deletions src/parser/parser_v2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace ddwaf::parser::v2 {

namespace {

std::pair<std::string, matcher::base::unique_ptr> parse_matcher(
std::pair<std::string, std::unique_ptr<matcher::base>> parse_matcher(
std::string_view name, const parameter::map &params)
{
parameter::map options;
Expand Down Expand Up @@ -145,7 +145,7 @@ std::vector<transformer_id> parse_transformers(
return transformers;
}

expression::ptr parse_expression(const parameter::vector &conditions_array,
std::shared_ptr<expression> parse_expression(const parameter::vector &conditions_array,
std::unordered_map<std::string, std::string> &rule_data_ids, expression::data_source source,
const std::vector<transformer_id> &transformers, const object_limits &limits)
{
Expand Down Expand Up @@ -304,7 +304,7 @@ std::pair<override_spec, reference_type> parse_override(const parameter::map &no
return {current, type};
}

expression::ptr parse_simplified_expression(
std::shared_ptr<expression> parse_simplified_expression(
const parameter::vector &conditions_array, const object_limits &limits)
{
expression_builder builder(conditions_array.size(), limits);
Expand Down Expand Up @@ -457,7 +457,7 @@ std::vector<processor::target_mapping> parse_processor_mappings(const parameter:
return mappings;
}

matcher::base::unique_ptr parse_scanner_matcher(const parameter::map &root)
std::unique_ptr<matcher::base> parse_scanner_matcher(const parameter::map &root)
{
auto matcher_name = at<std::string_view>(root, "operator");
auto matcher_params = at<parameter::map>(root, "parameters");
Expand Down Expand Up @@ -539,7 +539,7 @@ rule_data_container parse_rule_data(parameter::vector &rule_data, base_section_i
matcher_name = it->second;
}

matcher::base::shared_ptr matcher;
std::shared_ptr<matcher::base> matcher;
if (matcher_name == "ip_match") {
using rule_data_type = matcher::ip_match::rule_data_type;
auto parsed_data = parser::parse_rule_data<rule_data_type>(type, data);
Expand Down Expand Up @@ -741,8 +741,8 @@ scanner_container parse_scanners(parameter::vector &scanner_array, base_section_
}
}

matcher::base::unique_ptr key_matcher{};
matcher::base::unique_ptr value_matcher{};
std::unique_ptr<matcher::base> key_matcher{};
std::unique_ptr<matcher::base> value_matcher{};

auto it = node.find("key");
if (it != node.end()) {
Expand Down
12 changes: 6 additions & 6 deletions src/parser/specification.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct rule_spec {
rule::source_type source;
std::string name;
std::unordered_map<std::string, std::string> tags;
expression::ptr expr;
std::shared_ptr<expression> expr;
std::vector<std::string> actions;
};

Expand All @@ -43,20 +43,20 @@ struct override_spec {
};

struct rule_filter_spec {
expression::ptr expr;
std::shared_ptr<expression> expr;
std::vector<reference_spec> targets;
exclusion::filter_mode on_match;
};

struct input_filter_spec {
expression::ptr expr;
std::shared_ptr<expression> expr;
std::shared_ptr<exclusion::object_filter> filter;
std::vector<reference_spec> targets;
};

struct processor_spec {
std::shared_ptr<generator::base> generator;
expression::ptr expr;
std::shared_ptr<expression> expr;
std::vector<processor::target_mapping> mappings;
std::vector<reference_spec> scanners;
bool evaluate{false};
Expand All @@ -65,8 +65,8 @@ struct processor_spec {

// Containers
using rule_spec_container = std::unordered_map<std::string, rule_spec>;
using rule_data_container = std::unordered_map<std::string, matcher::base::shared_ptr>;
using scanner_container = std::unordered_map<std::string_view, scanner::ptr>;
using rule_data_container = std::unordered_map<std::string, std::shared_ptr<matcher::base>>;
using scanner_container = std::unordered_map<std::string_view, std::shared_ptr<scanner>>;

struct processor_container {
[[nodiscard]] bool empty() const { return pre.empty() && post.empty(); }
Expand Down
Loading