Avoids string copy by working with pointers while resolving variables

This commit is contained in:
Felipe Zimmerle
2016-12-27 15:25:11 -03:00
parent 7834cf857b
commit a7f465cf3a
38 changed files with 179 additions and 94 deletions

View File

@@ -29,13 +29,12 @@ namespace Variables {
void Duration::evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) {
std::string res;
double e = utils::cpu_seconds() - transaction->m_creationTimeStamp;
res = std::to_string(e);
transaction->m_variableDuration.assign(std::to_string(e));
l->push_back(new collection::Variable("DURATION", std::string(res)));
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableDuration));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class Duration : public Variable {
public:
explicit Duration(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("DURATION") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};

View File

@@ -34,7 +34,6 @@ namespace Variables {
void Env::evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) {
std::map<std::string, std::string> envs;
for (char **current = environ; *current; current++) {
std::string env = std::string(*current);
size_t pos = env.find_first_of("=");
@@ -43,20 +42,16 @@ void Env::evaluateInternal(Transaction *transaction,
}
std::string key = std::string(env, 0, pos);
std::string value = std::string(env, pos+1, env.length() - (pos + 1));
envs.insert(std::pair<std::string, std::string>("ENV:" + key, value));
if ("env:" + key == m_name) {
l->push_back(new collection::Variable(m_name, value));
return;
}
std::pair<std::string, std::string> a("ENV:" + key, value);
transaction->m_variableEnvs.insert(a);
}
for (auto& x : envs) {
for (auto& x : transaction->m_variableEnvs) {
if ((x.first.substr(0, m_name.size() + 1).compare(m_name + ":") != 0)
&& (x.first != m_name)) {
continue;
}
l->push_back(new collection::Variable(x.first, x.second));
l->push_back(new collection::Variable(&x.first, &x.second));
}
}

View File

@@ -28,8 +28,10 @@ namespace Variables {
void HighestSeverity::evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) {
l->push_back(new collection::Variable("HIGHEST_SEVERITY",
std::to_string(transaction->m_highestSeverityAction)));
transaction->m_variableHighestSeverityAction.assign(
std::to_string(transaction->m_highestSeverityAction));
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableHighestSeverityAction));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class HighestSeverity : public Variable {
public:
explicit HighestSeverity(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("HIGHEST_SEVERITY") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};

View File

@@ -15,27 +15,19 @@
#include "src/variables/modsec_build.h"
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include "modsecurity/transaction.h"
#include "modsecurity/modsecurity.h"
namespace modsecurity {
namespace Variables {
void ModsecBuild::evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) {
std::ostringstream ss;
ss << std::setw(2) << std::setfill('0') << MODSECURITY_MAJOR;
ss << std::setw(2) << std::setfill('0') << MODSECURITY_MINOR;
ss << std::setw(2) << std::setfill('0') << MODSECURITY_PATCHLEVEL;
ss << std::setw(2) << std::setfill('0') << MODSECURITY_TAG_NUM;
l->push_back(new collection::Variable("MODSEC_BUILD", ss.str()));
l->push_back(new collection::Variable(&m_retName, &m_build));
}

View File

@@ -17,11 +17,13 @@
#include <string>
#include <list>
#include <utility>
#include <iostream>
#ifndef SRC_VARIABLES_MODSEC_BUILD_H_
#define SRC_VARIABLES_MODSEC_BUILD_H_
#include "src/variables/variable.h"
#include "modsecurity/modsecurity.h"
namespace modsecurity {
@@ -31,10 +33,21 @@ namespace Variables {
class ModsecBuild : public Variable {
public:
explicit ModsecBuild(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("MODSEC_BUILD") {
std::ostringstream ss;
ss << std::setw(2) << std::setfill('0') << MODSECURITY_MAJOR;
ss << std::setw(2) << std::setfill('0') << MODSECURITY_MINOR;
ss << std::setw(2) << std::setfill('0') << MODSECURITY_PATCHLEVEL;
ss << std::setw(2) << std::setfill('0') << MODSECURITY_TAG_NUM;
m_build = ss.str();
}
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_build;
std::string m_retName;
};

View File

@@ -57,9 +57,10 @@ void RemoteUser::evaluateInternal(Transaction *transaction,
if (pos == std::string::npos) {
return;
}
base64 = std::string(base64, 0, pos);
transaction->m_variableRemoteUser.assign(std::string(base64, 0, pos));
l->push_back(new collection::Variable("REMOTE_USER", base64));
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableRemoteUser));
}

View File

@@ -33,10 +33,12 @@ namespace Variables {
class RemoteUser : public Variable {
public:
explicit RemoteUser(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("REMOTE_USER") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};

View File

@@ -46,7 +46,9 @@ void Time::evaluateInternal(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%H:%M:%S", &timeinfo);
l->push_back(new collection::Variable("TIME", std::string(tstr)));
transaction->m_variableTime.assign(tstr);
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableTime));
}

View File

@@ -32,10 +32,12 @@ namespace Variables {
class Time : public Variable {
public:
explicit Time(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("TIME") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};
} // namespace Variables

View File

@@ -45,7 +45,10 @@ void TimeDay::evaluateInternal(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%d", &timeinfo);
l->push_back(new collection::Variable("TIME_DAY", std::string(tstr)));
transaction->m_variableTimeDay.assign(tstr);
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableTimeDay));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class TimeDay : public Variable {
public:
explicit TimeDay(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("TIME_DAY") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};
} // namespace Variables

View File

@@ -35,8 +35,10 @@ namespace Variables {
void TimeEpoch::evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) {
l->push_back(new collection::Variable("TIME_EPOCH",
std::to_string(std::time(nullptr))));
transaction->m_variableTimeEpoch.assign(
std::to_string(std::time(nullptr)));
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableTimeEpoch));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class TimeEpoch : public Variable {
public:
explicit TimeEpoch(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("TIME_EPOCH") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};
} // namespace Variables

View File

@@ -45,7 +45,10 @@ void TimeHour::evaluateInternal(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%H", &timeinfo);
l->push_back(new collection::Variable("TIME_HOUR", std::string(tstr)));
transaction->m_variableTimeHour.assign(tstr);
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableTimeHour));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class TimeHour : public Variable {
public:
explicit TimeHour(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("TIME_HOUR") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};
} // namespace Variables

View File

@@ -45,7 +45,10 @@ void TimeMin::evaluateInternal(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%M", &timeinfo);
l->push_back(new collection::Variable("TIME_MIN", std::string(tstr)));
transaction->m_variableTimeMin.assign(tstr);
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableTimeMin));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class TimeMin : public Variable {
public:
explicit TimeMin(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("TIME_MIN") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};
} // namespace Variables

View File

@@ -47,7 +47,10 @@ void TimeMon::evaluateInternal(Transaction *transaction,
int a = atoi(tstr);
a--;
l->push_back(new collection::Variable("TIME_MON", std::to_string(a)));
transaction->m_variableTimeMin.assign(std::to_string(a));
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableTimeMin));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class TimeMon : public Variable {
public:
explicit TimeMon(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("TIME_MON") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};
} // namespace Variables

View File

@@ -45,7 +45,10 @@ void TimeSec::evaluateInternal(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%S", &timeinfo);
l->push_back(new collection::Variable("TIME_SEC", std::string(tstr)));
transaction->m_variableTimeSec.assign(tstr);
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableTimeSec));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class TimeSec : public Variable {
public:
explicit TimeSec(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("TIME_SEC") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};
} // namespace Variables

View File

@@ -47,7 +47,10 @@ void TimeWDay::evaluateInternal(Transaction *transaction,
int a = atoi(tstr);
a--;
l->push_back(new collection::Variable("TIME_WDAY", std::to_string(a)));
transaction->m_variableTimeWDay.assign(tstr);
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableTimeWDay));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class TimeWDay : public Variable {
public:
explicit TimeWDay(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("TIME_WDAY") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};
} // namespace Variables

View File

@@ -45,7 +45,10 @@ void TimeYear::evaluateInternal(Transaction *transaction,
localtime_r(&timer, &timeinfo);
strftime(tstr, 200, "%Y", &timeinfo);
l->push_back(new collection::Variable("TIME_YEAR", std::string(tstr)));
transaction->m_variableTimeYear.assign(tstr);
l->push_back(new collection::Variable(&m_retName,
&transaction->m_variableTimeYear));
}

View File

@@ -31,10 +31,12 @@ namespace Variables {
class TimeYear : public Variable {
public:
explicit TimeYear(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_retName("TIME_YEAR") { }
void evaluateInternal(Transaction *transaction,
std::vector<const collection::Variable *> *l) override;
std::string m_retName;
};
} // namespace Variables

View File

@@ -46,10 +46,13 @@ void Count::evaluateInternal(Transaction *transaction,
}
delete reslIn;
std::string res = std::to_string(count);
std::string *res = new std::string(std::to_string(count));
l->push_back(new collection::Variable(std::string(var->m_name),
std::string(res)));
collection::Variable *val = new collection::Variable(&var->m_name,
res);
val->m_dynamic_value = true;
l->push_back(val);
}

View File

@@ -73,8 +73,7 @@ void XML::evaluateInternal(Transaction *t,
/* Invocation without an XPath expression makes sense
* with functions that manipulate the document tree.
*/
l->push_back(new collection::Variable("XML",
std::string("[XML document tree]" + param)));
l->push_back(new collection::Variable(&m_name, &m_plain));
return;
}
@@ -124,8 +123,9 @@ void XML::evaluateInternal(Transaction *t,
content = reinterpret_cast<char *>(
xmlNodeGetContent(nodes->nodeTab[i]));
if (content != NULL) {
l->push_back(new collection::Variable(m_name,
std::string(content)));
// FIXME: Memory leak
l->push_back(new collection::Variable(&m_name,
new std::string(content)));
xmlFree(content);
}
}

View File

@@ -33,11 +33,13 @@ namespace Variables {
class XML : public Variable {
public:
explicit XML(std::string _name)
: Variable(_name) { }
: Variable(_name),
m_plain("[XML document tree]") { }
void evaluateInternal(Transaction *transaction,
Rule *rule,
std::vector<const collection::Variable *> *l) override;
std::string m_plain;
};
} // namespace Variables