Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Anilm3 committed Oct 1, 2024
1 parent 1c76bc6 commit 751a04b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 110 deletions.
1 change: 1 addition & 0 deletions src/condition/scalar_condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "log.hpp"
#include "matcher/base.hpp"
#include "object_store.hpp"
#include "object_type.hpp"
#include "scalar_condition.hpp"
#include "transformer/base.hpp"
#include "transformer/manager.hpp"
Expand Down
21 changes: 15 additions & 6 deletions src/iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "exclusion/common.hpp"
#include "iterator.hpp"
#include "object_type.hpp"
#include "object_view.hpp"
#include "utils.hpp"

namespace ddwaf {
Expand Down Expand Up @@ -100,7 +103,8 @@ void value_iterator::initialise_cursor(object_view obj, const std::span<const st

// Add container to stack and find next scalar
if (limits_.max_container_depth > 0) {
stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0});
const stack_node node{.key = {}, .value = obj, .index = 0};
stack_.emplace_back(node);
set_cursor_to_next_object();
}
} else {
Expand All @@ -121,7 +125,8 @@ void value_iterator::initialise_cursor_with_path(
}

// Add container to stack and find next scalar within the given path
stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0});
const stack_node node{.key = {}, .value = obj, .index = 0};
stack_.emplace_back(node);

for (std::size_t i = 0; i < path.size(); i++) {
const std::string_view key = path[i];
Expand Down Expand Up @@ -238,7 +243,8 @@ void key_iterator::initialise_cursor(object_view obj, const std::span<const std:
if (path.empty()) {
// Add container to stack and find next scalar
if (limits_.max_container_depth > 0) {
stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0});
const stack_node node{.key = {}, .value = obj, .index = 0};
stack_.emplace_back(node);
set_cursor_to_next_object();
}
} else {
Expand All @@ -254,7 +260,8 @@ void key_iterator::initialise_cursor_with_path(
}

// Add container to stack and find next scalar within the given path
stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0});
const stack_node node{.key = {}, .value = obj, .index = 0};
stack_.emplace_back(node);

for (std::size_t i = 0; i < path.size(); i++) {
const std::string_view key = path[i];
Expand Down Expand Up @@ -373,7 +380,8 @@ void kv_iterator::initialise_cursor(object_view obj, const std::span<const std::

// Add container to stack and find next scalar
if (limits_.max_container_depth > 0) {
stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0});
const stack_node node{.key = {}, .value = obj, .index = 0};
stack_.emplace_back(node);
set_cursor_to_next_object();
}
} else {
Expand All @@ -389,7 +397,8 @@ void kv_iterator::initialise_cursor_with_path(
}

// Add container to stack and find next scalar within the given path
stack_.emplace_back(stack_node{.key = {}, .value = obj, .index = 0});
const stack_node node{.key = {}, .value = obj, .index = 0};
stack_.emplace_back(node);

for (std::size_t i = 0; i < path.size(); i++) {
const std::string_view key = path[i];
Expand Down
1 change: 1 addition & 0 deletions src/iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "context_allocator.hpp"
#include "exclusion/common.hpp"
#include "object_type.hpp"
#include "object_view.hpp"
#include "utils.hpp"

Expand Down
113 changes: 9 additions & 104 deletions src/object_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,95 +19,6 @@ namespace ddwaf {

namespace detail {
using object = ddwaf_object;
/*struct object*/
/*{*/
/*const char* parameterName;*/
/*uint64_t parameterNameLength;*/
/*// uintValue should be at least as wide as the widest type on the platform.*/
/*union*/
/*{*/
/*const char* stringValue;*/
/*uint64_t uintValue;*/
/*int64_t intValue;*/
/*object* array;*/
/*bool boolean;*/
/*double f64;*/
/*};*/
/*uint64_t nbEntries;*/
/*object_type type;*/
/*};*/

struct object_iterator {
const object *ptr;
std::size_t index;
std::size_t size;
DDWAF_OBJ_TYPE type;
// 1 byte of padding to spare

// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
static object_iterator construct(
const detail::object *obj, std::size_t index, std::size_t max_size)
{
if (obj->type == DDWAF_OBJ_MAP || obj->type == DDWAF_OBJ_ARRAY) {
return {
.ptr = obj->array,
.index = index < obj->nbEntries ? index : static_cast<std::size_t>(obj->nbEntries),
.size =
obj->nbEntries < max_size ? static_cast<std::size_t>(obj->nbEntries) : max_size,
.type = obj->type,
};
}

return {
.ptr = nullptr,
.index = 0,
.size = 0,
.type = DDWAF_OBJ_INVALID,
};
}

object_iterator &operator++() noexcept
{
if (index < size) {
++index;
}
return *this;
}

std::pair<std::string_view, const detail::object *> operator*() const noexcept
{
if (index < size) {
const auto &slot = ptr[index];
std::string_view key;
if (type == DDWAF_OBJ_MAP) {
key = {slot.parameterName, static_cast<std::size_t>(slot.parameterNameLength)};
}

return {key, &slot};
}

[[unlikely]] return {};
}

[[nodiscard]] bool is_valid() const noexcept { return index < size; }

[[nodiscard]] std::string_view key() const noexcept
{
if (type != DDWAF_OBJ_MAP || index >= size) {
[[unlikely]] return {};
}
const auto &slot = ptr[index];
return {slot.parameterName, static_cast<std::size_t>(slot.parameterNameLength)};
}

[[nodiscard]] const object *value() const noexcept
{
if (index >= size) {
[[unlikely]] return nullptr;
}
return &ptr[index];
}
};
} // namespace detail

template <typename T> struct converter;
Expand All @@ -130,26 +41,20 @@ class object_view {

[[nodiscard]] object_type type() const noexcept
{
if (obj_ == nullptr) {
[[unlikely]] return object_type::invalid;
}
return static_cast<object_type>(obj_->type);
return obj_ != nullptr ? static_cast<object_type>(obj_->type) : object_type::invalid;
}
[[nodiscard]] std::size_t size() const noexcept
{
if (!is_container()) {
[[unlikely]] return 0;
}
return static_cast<std::size_t>(obj_->nbEntries);
return obj_ != nullptr ? static_cast<std::size_t>(obj_->nbEntries) : 0;
}
[[nodiscard]] std::size_t capacity() const noexcept
{
if (!is_container()) {
[[unlikely]] return 0;
}
return static_cast<std::size_t>(obj_->nbEntries);
return obj_ != nullptr ? static_cast<std::size_t>(obj_->nbEntries) : 0;
}
[[nodiscard]] bool empty() const noexcept
{
return obj_ != nullptr ? obj_->nbEntries == 0 : true;
}
[[nodiscard]] bool empty() const noexcept { return size() == 0; }
template <typename T> [[nodiscard]] bool is() const noexcept
{
return is_compatible_type<T>(type());
Expand All @@ -172,7 +77,7 @@ class object_view {

[[nodiscard]] std::pair<std::string_view, object_view> at(std::size_t index) const noexcept
{
if (!is_container() || index > size()) {
if (!is_container() || index > static_cast<std::size_t>(obj_->nbEntries)) {
[[unlikely]] return {};
}
return at_unchecked(index);
Expand All @@ -187,7 +92,6 @@ class object_view {
slot.parameterName, static_cast<std::size_t>(slot.parameterNameLength)};
return {key, object_view{&slot}};
}

return {{}, object_view{&slot}};
}

Expand Down Expand Up @@ -588,6 +492,7 @@ class object_view {
};

} // namespace ddwaf

namespace std {

template <> struct hash<ddwaf::object_view> {
Expand Down

0 comments on commit 751a04b

Please sign in to comment.