Skip to content
Open
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
19 changes: 5 additions & 14 deletions src/operators/pm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void Pm::cleanup(acmp_node_t *n) {
cleanup(n->sibling);
cleanup(n->child);

postOrderTraversal(n->btree);
acmp_btree_free(n->btree);

if (n->text && strlen(n->text) > 0) {
free(n->text);
Expand All @@ -127,18 +127,6 @@ void Pm::cleanup(acmp_node_t *n) {
}


void Pm::postOrderTraversal(acmp_btree_node_t *node) {
if (node == NULL) {
return;
}

postOrderTraversal(node->right);
postOrderTraversal(node->left);

free(node);
}


bool Pm::evaluate(Transaction *transaction, RuleWithActions *rule,
const std::string &input, RuleMessage &ruleMessage) {
int rc;
Expand Down Expand Up @@ -176,7 +164,10 @@ bool Pm::init(const std::string &file, std::string *error) {
});

while (m_p->is_failtree_done == 0) {
acmp_prepare(m_p);
if (acmp_prepare(m_p) != 1) {
if (error) error->assign("Failed to prepare pattern matcher.");
return false;
}
}

return true;
Expand Down
1 change: 0 additions & 1 deletion src/operators/pm.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class Pm : public Operator {


bool init(const std::string &file, std::string *error) override;
void postOrderTraversal(acmp_btree_node_t *node);
void cleanup(acmp_node_t *n);

protected:
Expand Down
5 changes: 4 additions & 1 deletion src/operators/pm_from_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ bool PmFromFile::init(const std::string &config, std::string *error) {
}

while (m_p->is_failtree_done == 0) {
acmp_prepare(m_p);
if (acmp_prepare(m_p) != 1) {
error->assign("Failed to prepare pattern matcher.");
return false;
Comment on lines 83 to +86

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit 8ec30c6

}
}

return true;
Expand Down
67 changes: 49 additions & 18 deletions src/utils/acmp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,30 @@
}
}

void acmp_btree_free(acmp_btree_node_t *node) {
if (node == NULL) {
return;
}

acmp_btree_free(node->right);
acmp_btree_free(node->left);

free(node);

Check failure on line 171 in src/utils/acmp.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this use of "free".

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaCkK1YnEnc7mAPTweNc&open=AaCkK1YnEnc7mAPTweNc&pullRequest=3626
}

/**
* Adds leaves to binary tree, working from sorted array of keyword tree nodes
*/
static void acmp_add_btree_leaves(acmp_btree_node_t *node, acmp_node_t *nodes[],
static bool acmp_add_btree_leaves(acmp_btree_node_t *node, acmp_node_t *nodes[],
int pos, int lb, int rb) {

int left = 0, right = 0;
if ((pos - lb) > 1) {
left = lb + (pos - lb) / 2;
node->left = reinterpret_cast<acmp_btree_node_t *>(calloc(1, sizeof(acmp_btree_node_t)));
if (!(node->left)) {
return false;
}
node->left->node = NULL;
node->left->right = NULL;
node->left->left = NULL;
Expand All @@ -184,6 +198,9 @@
if ((rb - pos) > 1) {
right = pos + (rb - pos) / 2;
node->right = reinterpret_cast<acmp_btree_node_t *>(calloc(1, sizeof(acmp_btree_node_t)));
if (!(node->right)) {
return false;
}
node->right->node = NULL;
node->right->right = NULL;
node->right->left = NULL;
Expand All @@ -195,18 +212,20 @@
fprintf(stderr, "%lc ->right %lc\n", (wint_t)node->node->letter, (wint_t)node->right->node->letter);
#endif
}
if (node->right != NULL) {
acmp_add_btree_leaves(node->right, nodes, right, pos, rb);
if ((node->right != nullptr) && !acmp_add_btree_leaves(node->right, nodes, right, pos, rb)) {
return false;
}
if (node->left != NULL) {
acmp_add_btree_leaves(node->left, nodes, left, lb, pos);
if ((node->left != nullptr) && !acmp_add_btree_leaves(node->left, nodes, left, lb, pos)) {
return false;
}

return true;
}

/**
* Builds balanced binary tree from children nodes of given node.
*/
static void acmp_build_binary_tree(ACMP *parser, acmp_node_t *node) {
static bool acmp_build_binary_tree(ACMP *parser, acmp_node_t *node) {
size_t count, i, j;
acmp_node_t *child = node->child;
acmp_node_t **nodes;
Expand All @@ -215,7 +234,7 @@
/* Build an array big enough */
for (count = 0; child != NULL; child = child->sibling) count++;
nodes = (acmp_node_t **)calloc(1, count * sizeof(acmp_node_t *));
/* ENH: Check alloc succeded */
if (!nodes) return false;

/* ENH: Combine this in the loop below - we do not need two loops */
child = node->child;
Expand All @@ -236,24 +255,34 @@
nodes[i] = nodes[j];
nodes[j] = tmp;
}
}
if (node->btree != NULL) {
free(node->btree);
node->btree = NULL;
}
if (node->btree != nullptr) {
acmp_btree_free(node->btree);
node->btree = nullptr;
}
node->btree = reinterpret_cast<acmp_btree_node_t *>(calloc(1, sizeof(acmp_btree_node_t)));
if (!(node->btree)) {
free(nodes);
return false;
}

/* ENH: Check alloc succeded */
pos = count / 2;
node->btree->node = nodes[pos];
node->btree->letter = nodes[pos]->letter;
acmp_add_btree_leaves(node->btree, nodes, pos, -1, count);
for (i = 0; i < count; i++) {
if (nodes[i]->child != NULL) acmp_build_binary_tree(parser, nodes[i]);
}
if (nodes != NULL) {
if (!acmp_add_btree_leaves(node->btree, nodes, pos, -1, count)){
free(nodes);

Check failure on line 273 in src/utils/acmp.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this use of "free".

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaCgKNqNU3ERR9uQ9110&open=AaCgKNqNU3ERR9uQ9110&pullRequest=3626
return false;
}
for (i = 0; i < count; i++) {
if ((nodes[i]->child != nullptr) && !acmp_build_binary_tree(parser, nodes[i])) {
free(nodes);

Check failure on line 278 in src/utils/acmp.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this use of "free".

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaCgKNqNU3ERR9uQ9111&open=AaCgKNqNU3ERR9uQ9111&pullRequest=3626
return false;
}
}

free(nodes);

Check failure on line 283 in src/utils/acmp.cc

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this use of "free".

See more on https://sonarcloud.io/project/issues?id=owasp-modsecurity_ModSecurity&issues=AaCjuLzsMBtv7rY6AdkR&open=AaCjuLzsMBtv7rY6AdkR&pullRequest=3626
return true;

}

/**
Expand Down Expand Up @@ -305,7 +334,9 @@
}

acmp_connect_other_matches(parser, parser->root_node);
if (parser->root_node->child != NULL) acmp_build_binary_tree(parser, parser->root_node);
if ((parser->root_node->child != nullptr) && !acmp_build_binary_tree(parser, parser->root_node)) {
return 0;
}
parser->is_failtree_done = 1;

return 1;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/acmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ int acmp_process_quick(ACMPT *acmpt, const char **match, const char *data, size_
*/
int acmp_prepare(ACMP *parser);

/**
* Destroying the tree and freeing the memory.
*/
void acmp_btree_free(acmp_btree_node_t *node);


}

#endif /*ACMP_H_*/
Loading