Fixes regarding memory management

Fixes assorted issues identified by valgrind.
This commit is contained in:
Felipe Zimmerle
2016-06-15 23:52:26 -03:00
parent cb91af537c
commit 9919026620
46 changed files with 234 additions and 73 deletions

View File

@@ -569,4 +569,4 @@ int acmp_process_quick(ACMPT *acmpt, const char **match, const char *data, size_
return 0;
}
}
}

View File

@@ -39,50 +39,57 @@ void IpTree::postOrderTraversal(TreeNode *node) {
postOrderTraversal(node->right);
if (node->netmasks) {
delete node->netmasks;
free(node->netmasks);
node->netmasks = NULL;
}
if (node->prefix) {
if (node->prefix->buffer) {
delete node->prefix->buffer;
free(node->prefix->buffer);
node->prefix->buffer = NULL;
}
if (node->prefix->prefix_data) {
delete node->prefix->prefix_data;
free(node->prefix->prefix_data);
node->prefix->prefix_data = NULL;
}
delete node->prefix;
free(node->prefix);
node->prefix = NULL;
}
delete node;
free(node);
node = NULL;
}
IpTree::IpTree() {
// FIXME: deal with possible error.
char *error;
create_radix_tree(&m_tree, &error);
}
IpTree::~IpTree() {
if (m_tree != NULL) {
if (m_tree->ipv4_tree != NULL) {
// Tree_traversal: Post-order to delete all the items.
postOrderTraversal(m_tree->ipv4_tree->head);
delete m_tree->ipv4_tree;
free(m_tree->ipv4_tree);
m_tree->ipv4_tree = NULL;
}
if (m_tree->ipv6_tree != NULL) {
// Tree_traversal: Post-order to delete all the items.
postOrderTraversal(m_tree->ipv6_tree->head);
delete m_tree->ipv6_tree;
free(m_tree->ipv6_tree);
m_tree->ipv6_tree = NULL;
}
delete m_tree;
free(m_tree);
m_tree = NULL;
}
}
bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
char *error_msg = NULL;
for (std::string line; std::getline(*ss, line); ) {
int res = ip_tree_from_param(line.c_str(), &m_tree, &error_msg);
int res = add_ip_from_param(line.c_str(), &m_tree, &error_msg);
if (res != 0) {
if (error_msg != NULL) {
error->assign(error_msg);
@@ -98,7 +105,6 @@ bool IpTree::addFromBuffer(std::istream *ss, std::string *error) {
bool IpTree::addFromBuffer(const std::string& buffer, std::string *error) {
std::stringstream ss;
ss << buffer;
return addFromBuffer(&ss, error);
}

View File

@@ -30,8 +30,7 @@ namespace Utils {
class IpTree {
public:
IpTree()
: m_tree(NULL) { }
IpTree();
~IpTree();
bool contains(const std::string &ip);

View File

@@ -958,6 +958,44 @@ int tree_contains_ip(TreeRoot *rtree,
return 0;
}
int add_ip_from_param(
const char *param, TreeRoot **rtree, char **error_msg)
{
char *param_copy = strdup(param);
char *saved = NULL;
char *str = NULL;
TreeNode *tnode = NULL;
str = strtok_r(param_copy, ",", &saved);
while (str != NULL)
{
if (strchr(str, ':') == NULL)
{
tnode = TreeAddIP(str, (*rtree)->ipv4_tree, IPV4_TREE);
}
else
{
tnode = TreeAddIP(str, (*rtree)->ipv6_tree, IPV6_TREE);
}
if (tnode == NULL)
{
//*error_msg = apr_psprintf("Could not add entry " \
// "\"%s\" from: %s.", str, param);
free(param_copy);
return -1;
}
str = strtok_r(NULL, ",", &saved);
}
free(param_copy);
return 0;
}
int ip_tree_from_param(
const char *param, TreeRoot **rtree, char **error_msg)
{

View File

@@ -97,6 +97,7 @@ unsigned char is_netmask_v6(char *ip_strv6);
int tree_contains_ip(TreeRoot *rtree,
const char *value, char **error_msg);
int add_ip_from_param(const char *param, TreeRoot **rtree, char **error_msg);
int ip_tree_from_param(const char *param, TreeRoot **rtree, char **error_msg);
int create_radix_tree(TreeRoot **rtree, char **error_msg);
}

View File

@@ -45,6 +45,19 @@ Regex::Regex(const std::string& pattern_)
m_pce = pcre_study(m_pc, PCRE_STUDY_JIT_COMPILE, &errptr);
}
Regex::~Regex() {
if (m_pc != NULL) {
pcre_free(m_pc);
m_pc = NULL;
}
if (m_pce != NULL) {
pcre_free_study(m_pce);
m_pce = NULL;
}
}
int regex_search(const std::string& s, SMatch *match,
const Regex& regex) {
int ovector[OVECCOUNT];

View File

@@ -31,6 +31,7 @@ namespace Utils {
class Regex {
public:
explicit Regex(const std::string& pattern_);
~Regex();
std::string pattern;
pcre *m_pc = NULL;
pcre_extra *m_pce = NULL;