Skip to content

Commit a609249

Browse files
committed
Makes m_id a shared pointer
1 parent 343b86c commit a609249

File tree

6 files changed

+50
-55
lines changed

6 files changed

+50
-55
lines changed

Diff for: headers/modsecurity/rule_message.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ class RuleMessage {
8888
return log(rm, 0);
8989
}
9090

91-
static std::string _details(const RuleMessage *rm);
92-
static std::string _errorLogTail(const RuleMessage *rm);
91+
static inline void _details(const RuleMessage *rm, std::string *msg);
92+
static inline void _errorLogTail(const RuleMessage *rm, std::string *msg);
9393

9494
int m_accuracy;
9595
std::shared_ptr<std::string> m_clientIpAddress;
9696
std::string m_data;
97-
std::string m_id;
97+
std::shared_ptr<std::string> m_id;
9898
bool m_isDisruptive;
9999
std::string m_match;
100100
int m_maturity;

Diff for: headers/modsecurity/transaction.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ typedef struct Rules_t RulesSet;
5252
#define ms_dbg(b, c) \
5353
do { \
5454
if (m_rules && m_rules->m_debugLog && m_rules->m_debugLog->m_debugLevel >= b) { \
55-
m_rules->debug(b, m_id, m_uri, c); \
55+
m_rules->debug(b, *m_id.get(), m_uri, c); \
5656
} \
5757
} while (0);
5858
#else
@@ -516,7 +516,7 @@ class Transaction : public TransactionAnchoredVariables {
516516
* Contains the unique ID of the transaction. Use by the variable
517517
* `UNIQUE_ID'. This unique id is also saved as part of the AuditLog.
518518
*/
519-
std::string m_id;
519+
std::shared_ptr<std::string> m_id;
520520

521521
/**
522522
* Holds the SecMarker name that this transaction should wait to perform

Diff for: src/audit_log/writer/parallel.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ bool Parallel::write(Transaction *transaction, int parts, std::string *error) {
119119
}
120120

121121
std::string logPath = m_audit->m_storage_dir;
122-
fileName = logPath + fileName + "-" + transaction->m_id;
122+
fileName = logPath + fileName + "-" + *transaction->m_id.get();
123123

124124
if (logPath.empty()) {
125125
error->assign("Log path is not valid.");

Diff for: src/request_body_processor/multipart.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ int Multipart::tmp_file_name(std::string *filename) const {
469469

470470
memset(tstr, '\0', 300);
471471
strftime(tstr, 299, "/%Y%m%d-%H%M%S", &timeinfo);
472-
path = path + tstr + "-" + m_transaction->m_id;
472+
path = path + tstr + "-" + *m_transaction->m_id.get();
473473
path = path + "-file-XXXXXX";
474474

475475
tmp = strdup(path.c_str());

Diff for: src/rule_message.cc

+33-39
Original file line numberDiff line numberDiff line change
@@ -23,69 +23,63 @@
2323
namespace modsecurity {
2424

2525

26-
std::string RuleMessage::_details(const RuleMessage *rm) {
27-
std::string msg;
28-
29-
msg.append(" [file \"" + std::string(*rm->m_ruleFile.get()) + "\"]");
30-
msg.append(" [line \"" + std::to_string(rm->m_ruleLine) + "\"]");
31-
msg.append(" [id \"" + std::to_string(rm->m_ruleId) + "\"]");
32-
msg.append(" [rev \"" + rm->m_rev + "\"]");
33-
msg.append(" [msg \"" + rm->m_message + "\"]");
34-
msg.append(" [data \"" + utils::string::limitTo(200, rm->m_data) + "\"]");
35-
msg.append(" [severity \"" +
36-
std::to_string(rm->m_severity) + "\"]");
37-
msg.append(" [ver \"" + rm->m_ver + "\"]");
38-
msg.append(" [maturity \"" + std::to_string(rm->m_maturity) + "\"]");
39-
msg.append(" [accuracy \"" + std::to_string(rm->m_accuracy) + "\"]");
26+
inline void RuleMessage::_details(const RuleMessage *rm, std::string *msg) {
27+
*msg += " [file \"" + std::string(*rm->m_ruleFile.get()) + "\"]" \
28+
" [line \"" + std::to_string(rm->m_ruleLine) + "\"]" \
29+
" [id \"" + std::to_string(rm->m_ruleId) + "\"]" \
30+
" [rev \"" + rm->m_rev + "\"]" \
31+
" [msg \"" + rm->m_message + "\"]" \
32+
" [data \"" + utils::string::limitTo(200, rm->m_data) + "\"]" \
33+
" [severity \"" + std::to_string(rm->m_severity) + "\"]" \
34+
" [ver \"" + rm->m_ver + "\"]" \
35+
" [maturity \"" + std::to_string(rm->m_maturity) + "\"]" \
36+
" [accuracy \"" + std::to_string(rm->m_accuracy) + "\"]";
37+
4038
for (auto &a : rm->m_tags) {
41-
msg.append(" [tag \"" + a + "\"]");
39+
*msg += " [tag \"" + a + "\"]";
4240
}
43-
msg.append(" [hostname \"" + *rm->m_serverIpAddress.get() \
44-
+ "\"]");
45-
msg.append(" [uri \"" + utils::string::limitTo(200, *rm->m_uriNoQueryStringDecoded.get()) + "\"]");
46-
msg.append(" [unique_id \"" + rm->m_id + "\"]");
47-
msg.append(" [ref \"" + utils::string::limitTo(200, rm->m_reference) + "\"]");
4841

49-
return msg;
42+
*msg += " [hostname \"" + *rm->m_serverIpAddress.get() + "\"]" \
43+
" [uri \"" + *rm->m_uriNoQueryStringDecoded.get() + "\"]" \
44+
" [unique_id \"" + *rm->m_id.get() + "\"]" \
45+
" [ref \"" + utils::string::limitTo(200, rm->m_reference) + "\"]";
5046
}
5147

5248

53-
std::string RuleMessage::_errorLogTail(const RuleMessage *rm) {
54-
std::string msg;
55-
56-
msg.append("[hostname \"" + *rm->m_serverIpAddress.get() + "\"]");
57-
msg.append(" [uri \"" + utils::string::limitTo(200, *rm->m_uriNoQueryStringDecoded.get()) + "\"]");
58-
msg.append(" [unique_id \"" + rm->m_id + "\"]");
59-
60-
return msg;
49+
inline void RuleMessage::_errorLogTail(const RuleMessage *rm,
50+
std::string *msg) {
51+
*msg += " [hostname \"" + *rm->m_serverIpAddress.get() + "\"]" \
52+
" [uri \"" + utils::string::limitTo(200,
53+
*rm->m_uriNoQueryStringDecoded.get()) + "\"]" \
54+
" [unique_id \"" + *rm->m_id.get() + "\"]";
6155
}
6256

6357

6458
std::string RuleMessage::log(const RuleMessage *rm, int props, int code) {
6559
std::string msg("");
60+
msg.reserve(2048);
6661

6762
if (props & ClientLogMessageInfo) {
68-
msg.append("[client " + std::string(*rm->m_clientIpAddress.get()) + "] ");
63+
msg += "[client " + std::string(*rm->m_clientIpAddress.get()) + "] ";
6964
}
7065

7166
if (rm->m_isDisruptive) {
72-
msg.append("ModSecurity: Access denied with code ");
67+
msg += "ModSecurity: Access denied with code ";
7368
if (code == -1) {
74-
msg.append("%d");
69+
msg += "%d";
7570
} else {
76-
msg.append(std::to_string(code));
71+
msg += std::to_string(code);
7772
}
78-
msg.append(" (phase ");
79-
msg.append(std::to_string(rm->m_rule->m_phase - 1) + "). ");
73+
msg += " (phase " + std::to_string(rm->m_rule->m_phase - 1) + "). ";
8074
} else {
81-
msg.append("ModSecurity: Warning. ");
75+
msg += "ModSecurity: Warning. ";
8276
}
8377

84-
msg.append(rm->m_match);
85-
msg.append(_details(rm));
78+
msg += (rm->m_match);
79+
_details(rm, &msg);
8680

8781
if (props & ErrorLogTailLogMessageInfo) {
88-
msg.append(" " + _errorLogTail(rm));
82+
_errorLogTail(rm, &msg);
8983
}
9084

9185
return modsecurity::utils::string::toHexIfNeeded(msg);

Diff for: src/transaction.cc

+10-9
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, void *logCbData)
125125
m_rulesMessages(),
126126
m_requestBody(),
127127
m_responseBody(),
128-
m_id(),
128+
/* m_id(), */
129129
m_marker(""),
130130
m_skip_next(0),
131131
m_allowType(modsecurity::actions::disruptive::NoneAllowType),
@@ -162,8 +162,9 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, void *logCbData)
162162
m_variableTimeYear(""),
163163
m_logCbData(logCbData),
164164
TransactionAnchoredVariables(this) {
165-
m_id = std::to_string(this->m_timeStamp) + \
166-
std::to_string(modsecurity::utils::generate_transaction_unique_id());
165+
m_id = std::unique_ptr<std::string>(
166+
new std::string(
167+
std::to_string(m_timeStamp)));
167168

168169
m_variableUrlEncodedError.set("0", 0);
169170

@@ -198,7 +199,7 @@ Transaction::Transaction(ModSecurity *ms, RulesSet *rules, char *id, void *logCb
198199
m_rulesMessages(),
199200
m_requestBody(),
200201
m_responseBody(),
201-
m_id(std::string(id)),
202+
m_id(std::unique_ptr<std::string>(new std::string(id))),
202203
m_marker(""),
203204
m_skip_next(0),
204205
m_allowType(modsecurity::actions::disruptive::NoneAllowType),
@@ -282,7 +283,7 @@ void Transaction::debug(int level, std::string message) const {
282283
return;
283284
}
284285

285-
m_rules->debug(level, m_id, m_uri, message);
286+
m_rules->debug(level, *m_id.get(), m_uri, message);
286287
}
287288
#endif
288289

@@ -318,7 +319,7 @@ int Transaction::processConnection(const char *client, int cPort,
318319

319320

320321
m_variableRemoteHost.set(*m_clientIpAddress.get(), m_variableOffset);
321-
m_variableUniqueID.set(m_id, m_variableOffset);
322+
m_variableUniqueID.set(*m_id.get(), m_variableOffset);
322323
m_variableRemoteAddr.set(*m_clientIpAddress.get(), m_variableOffset);
323324
m_variableServerAddr.set(*m_serverIpAddress.get(), m_variableOffset);
324325
m_variableServerPort.set(std::to_string(this->m_serverPort),
@@ -1496,7 +1497,7 @@ std::string Transaction::toOldAuditLogFormatIndex(const std::string &filename,
14961497
ss << utils::string::dash_if_empty(
14971498
m_variableRequestHeaders.resolveFirst("User-Agent").get());
14981499
ss << "\" ";
1499-
ss << this->m_id << " ";
1500+
ss << *m_id.get() << " ";
15001501
/** TODO: Check variable */
15011502
ss << utils::string::dash_if_empty(
15021503
m_variableRequestHeaders.resolveFirst("REFERER").get()) << " ";
@@ -1522,7 +1523,7 @@ std::string Transaction::toOldAuditLogFormat(int parts,
15221523
audit_log << "--" << trailer << "-" << "A--" << std::endl;
15231524
strftime(tstr, 299, "[%d/%b/%Y:%H:%M:%S %z]", &timeinfo);
15241525
audit_log << tstr;
1525-
audit_log << " " << this->m_id.c_str();
1526+
audit_log << " " << m_id->c_str();
15261527
audit_log << " " << this->m_clientIpAddress;
15271528
audit_log << " " << this->m_clientPort;
15281529
audit_log << " " << m_serverIpAddress;
@@ -1648,7 +1649,7 @@ std::string Transaction::toJSON(int parts) {
16481649
LOGFY_ADD_NUM("client_port", m_clientPort);
16491650
LOGFY_ADD("host_ip", m_serverIpAddress->c_str());
16501651
LOGFY_ADD_NUM("host_port", m_serverPort);
1651-
LOGFY_ADD("unique_id", this->m_id.c_str());
1652+
LOGFY_ADD("unique_id", m_id->c_str());
16521653

16531654
/* request */
16541655
yajl_gen_string(g, reinterpret_cast<const unsigned char*>("request"),

0 commit comments

Comments
 (0)