Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilm3 committed May 16, 2024
1 parent a3e519b commit 164d59e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
26 changes: 10 additions & 16 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,22 @@ namespace ddwaf {
using attribute = object_store::attribute;

// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
DDWAF_RET_CODE context::run(optional_ref<ddwaf_object> persistent,
optional_ref<ddwaf_object> ephemeral, optional_ref<ddwaf_result> res,
std::pmr::memory_resource *alloc, uint64_t timeout)
DDWAF_RET_CODE context::run(owned_object persistent,
owned_object ephemeral, optional_ref<ddwaf_result> res, uint64_t timeout)
{
// This scope ensures that all ephemeral and cached objects are removed
// from the store at the end of the evaluation
auto store_cleanup_scope = store_.get_eval_scope();
auto on_exit = scope_exit([this]() { this->exclusion_policy_.ephemeral.clear(); });

if (persistent.has_value()) {
owned_object owned{reinterpret_cast<detail::object &>(persistent->get()), alloc};
if (!store_.insert(std::move(owned), attribute::none)) {
DDWAF_WARN("Illegal WAF call: parameter structure invalid!");
return DDWAF_ERR_INVALID_OBJECT;
}
if (persistent.is_valid() && !store_.insert(std::move(persistent), attribute::none)) {
DDWAF_WARN("Illegal WAF call: parameter structure invalid!");
return DDWAF_ERR_INVALID_OBJECT;
}

if (ephemeral.has_value()) {
owned_object owned{reinterpret_cast<detail::object &>(ephemeral->get()), alloc};
if (!store_.insert(std::move(owned), attribute::ephemeral)) {
DDWAF_WARN("Illegal WAF call: parameter structure invalid!");
return DDWAF_ERR_INVALID_OBJECT;
}
if (ephemeral.is_valid() && !store_.insert(std::move(ephemeral), attribute::ephemeral)) {
DDWAF_WARN("Illegal WAF call: parameter structure invalid!");
return DDWAF_ERR_INVALID_OBJECT;
}

if (timeout == 0) {
Expand All @@ -60,7 +53,8 @@ DDWAF_RET_CODE context::run(optional_ref<ddwaf_object> persistent,
optional_ref<ddwaf_object> derived;
if (res.has_value()) {
ddwaf_result &output = *res;
ddwaf_object_set_map(&output.derivatives, 32, nullptr);
owned_object derivatives_map = owned_object::make_map(0);
reinterpret_cast<ddwaf::detail::object&>(output.derivatives) = derivatives_map.move();
derived.emplace(output.derivatives);
}

Expand Down
9 changes: 4 additions & 5 deletions src/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class context {
context &operator=(context &&) = delete;
~context() = default;

DDWAF_RET_CODE run(optional_ref<ddwaf_object>, optional_ref<ddwaf_object>,
optional_ref<ddwaf_result>, std::pmr::memory_resource *alloc, uint64_t);
DDWAF_RET_CODE run(owned_object, owned_object, optional_ref<ddwaf_result>, uint64_t);

void eval_preprocessors(optional_ref<ddwaf_object> &derived, ddwaf::timer &deadline);
void eval_postprocessors(optional_ref<ddwaf_object> &derived, ddwaf::timer &deadline);
Expand Down Expand Up @@ -113,11 +112,11 @@ class context_wrapper {
context_wrapper &operator=(context_wrapper &&) noexcept = delete;
context_wrapper &operator=(const context_wrapper &) = delete;

DDWAF_RET_CODE run(optional_ref<ddwaf_object> persistent, optional_ref<ddwaf_object> ephemeral,
optional_ref<ddwaf_result> res, std::pmr::memory_resource *alloc, uint64_t timeout)
DDWAF_RET_CODE run(ddwaf::owned_object persistent, ddwaf::owned_object ephemeral,
optional_ref<ddwaf_result> res, uint64_t timeout)
{
memory::memory_resource_guard guard(&mr_);
return ctx_->run(persistent, ephemeral, res, alloc, timeout);
return ctx_->run(std::move(persistent), std::move(ephemeral), res, timeout);
}

protected:
Expand Down
23 changes: 7 additions & 16 deletions src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include "utils.hpp"
#include "waf.hpp"

#if DDWAF_COMPILE_LOG_LEVEL <= DDWAF_COMPILE_LOG_INFO
namespace {
const char *log_level_to_str(DDWAF_LOG_LEVEL level)
{
Expand Down Expand Up @@ -80,10 +79,7 @@ ddwaf::object_limits limits_from_config(const ddwaf_config *config)

return limits;
}

} // namespace

#endif
// explicit instantiation declaration to suppress warning
extern "C" {
ddwaf::waf *ddwaf_init(
Expand Down Expand Up @@ -198,13 +194,6 @@ ddwaf_context ddwaf_context_init(ddwaf::waf *handle)
false, DDWAF_OBJECT_INITIALISER, DDWAF_OBJECT_INITIALISER, DDWAF_OBJECT_INITIALISER, 0 \
}

// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
namespace {
std::pmr::memory_resource *to_memres(ddwaf_allocator *alloc)
{
return reinterpret_cast<std::pmr::memory_resource *>(alloc);
}
} // namespace

// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
DDWAF_RET_CODE ddwaf_run(ddwaf_context context, ddwaf_object *persistent_data,
Expand All @@ -224,17 +213,19 @@ DDWAF_RET_CODE ddwaf_run(ddwaf_context context, ddwaf_object *persistent_data,
res = *result;
}

optional_ref<ddwaf_object> persistent{std::nullopt};
auto *memres = reinterpret_cast<std::pmr::memory_resource *>(alloc);

ddwaf::owned_object persistent;
if (persistent_data != nullptr) {
persistent = *persistent_data;
persistent = ddwaf::owned_object{reinterpret_cast<ddwaf::detail::object&>(*persistent_data), memres};
}

optional_ref<ddwaf_object> ephemeral{std::nullopt};
ddwaf::owned_object ephemeral;
if (ephemeral_data != nullptr) {
ephemeral = *ephemeral_data;
ephemeral = ddwaf::owned_object{reinterpret_cast<ddwaf::detail::object&>(*ephemeral_data), memres};
}

return context->run(persistent, ephemeral, res, to_memres(alloc), timeout);
return context->run(std::move(persistent), std::move(ephemeral), res, timeout);
} catch (const std::exception &e) {
// catch-all to avoid std::terminate
DDWAF_ERROR("{}", e.what());
Expand Down

0 comments on commit 164d59e

Please sign in to comment.