Skip to content

Commit 5226c42

Browse files
WGH-zimmerle
authored andcommitted
1 parent 1e14d64 commit 5226c42

16 files changed

+51
-192
lines changed

CHANGES

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
v3.x.y - YYYY-MMM-DD (to be released)
22
-------------------------------------
3-
3+
4+
- Removed unnecessary while processing the transformations.
5+
[#2368 - @WGH-, @zimmerle]
46
- auditlog: Computes whether or not to save while loading the rules.
57
[@zimmerle]
68
- actions: Computes Rule association while loading the rules given a

src/actions/transformations/css_decode.cc

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,10 @@ namespace transformations {
3232
void CssDecode::execute(const Transaction *t,
3333
const ModSecString &in,
3434
ModSecString &out) noexcept {
35-
size_t s = in.size();
36-
37-
char *tmp = reinterpret_cast<char *>(
38-
malloc(sizeof(char) * s + 1));
39-
memcpy(tmp, in.c_str(), s + 1);
40-
tmp[s] = '\0';
41-
42-
size_t r = CssDecode::css_decode_inplace(
43-
reinterpret_cast<unsigned char *>(tmp),
44-
s);
45-
46-
out.assign(tmp, r);
47-
free(tmp);
35+
out.assign(in);
36+
auto size = CssDecode::css_decode_inplace(
37+
reinterpret_cast<unsigned char *>(&out[0]), out.size());
38+
out.resize(size);
4839
}
4940

5041

src/actions/transformations/escape_seq_decode.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,11 @@ int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input,
135135
void EscapeSeqDecode::execute(const Transaction *t,
136136
const ModSecString &in,
137137
ModSecString &out) noexcept {
138-
unsigned char *tmp = (unsigned char *) malloc(sizeof(char)
139-
* in.size() + 1);
140-
memcpy(tmp, in.c_str(), in.size() + 1);
141-
tmp[in.size()] = '\0';
142-
143-
int size = ansi_c_sequences_decode_inplace(tmp, in.size());
144-
out.assign(reinterpret_cast<char *>(tmp), size);
145-
free(tmp);
138+
139+
out.assign(in);
140+
auto size = ansi_c_sequences_decode_inplace(
141+
reinterpret_cast<unsigned char *>(&out[0]), out.size());
142+
out.resize(size);
146143
}
147144

148145
} // namespace transformations

src/actions/transformations/hex_decode.cc

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,10 @@ namespace transformations {
3434
void HexDecode::execute(const Transaction *t,
3535
const ModSecString &in,
3636
ModSecString &out) noexcept {
37-
unsigned char *input;
38-
int size = 0;
39-
40-
input = reinterpret_cast<unsigned char *>
41-
(malloc(sizeof(char) * in.length()+1));
42-
43-
if (input == NULL) {
44-
return;
45-
}
46-
47-
memcpy(input, in.c_str(), in.length()+1);
48-
49-
size = inplace(input, in.length());
50-
51-
out.assign(reinterpret_cast<char *>(input), size);
52-
free(input);
37+
out.assign(in);
38+
auto size = inplace(reinterpret_cast<unsigned char *>(
39+
&out[0]), out.length());
40+
out.resize(size);
5341
}
5442

5543

src/actions/transformations/html_entity_decode.cc

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,10 @@ namespace transformations {
3232
void HtmlEntityDecode::execute(const Transaction *t,
3333
const ModSecString &in,
3434
ModSecString &out) noexcept {
35-
unsigned char *input;
36-
37-
input = reinterpret_cast<unsigned char *>
38-
(malloc(sizeof(char) * in.length()+1));
39-
40-
if (input == NULL) {
41-
return;
42-
}
43-
44-
memcpy(input, in.c_str(), in.length()+1);
45-
46-
size_t i = inplace(input, in.length());
47-
48-
out.assign(reinterpret_cast<char *>(input), i);
49-
free(input);
35+
out.assign(in);
36+
auto i = inplace(reinterpret_cast<unsigned char *>(
37+
&out[0]), out.length());
38+
out.resize(i);
5039
}
5140

5241

src/actions/transformations/js_decode.cc

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,11 @@ namespace transformations {
3232
void JsDecode::execute(const Transaction *t,
3333
const ModSecString &in,
3434
ModSecString &out) noexcept {
35-
unsigned char *input;
3635

37-
input = reinterpret_cast<unsigned char *>
38-
(malloc(sizeof(char) * in.length()+1));
39-
40-
if (input == NULL) {
41-
return;
42-
}
43-
44-
memcpy(input, in.c_str(), in.length()+1);
45-
46-
size_t i = inplace(input, in.length());
47-
48-
out.assign(reinterpret_cast<char *>(input), i);
49-
free(input);
36+
out.assign(in);
37+
auto i = inplace(reinterpret_cast<unsigned char *>(
38+
&out[0]), out.length());
39+
out.resize(i);
5040
}
5141

5242

src/actions/transformations/normalise_path.cc

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,9 @@ void NormalisePath::execute(const Transaction *t,
3131
const ModSecString &in,
3232
ModSecString &out) noexcept {
3333
int changed = 0;
34-
35-
char *tmp = reinterpret_cast<char *>(
36-
malloc(sizeof(char) * in.size() + 1));
37-
memcpy(tmp, in.c_str(), in.size() + 1);
38-
tmp[in.size()] = '\0';
39-
40-
int i = normalize_path_inplace((unsigned char *)tmp,
41-
in.size(), 0, &changed);
42-
43-
std::string ret("");
44-
out.assign(tmp, i);
45-
free(tmp);
34+
out.assign(in);
35+
auto size = normalize_path_inplace(reinterpret_cast<unsigned char *>(&out[0]), out.length(), 0, &changed);
36+
out.resize(size);
4637
}
4738

4839

src/actions/transformations/normalise_path_win.cc

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,10 @@ void NormalisePathWin::execute(const Transaction *t,
3636
ModSecString &out) noexcept {
3737
int changed;
3838

39-
char *tmp = reinterpret_cast<char *>(
40-
malloc(sizeof(char) * in.size() + 1));
41-
memcpy(tmp, in.c_str(), in.size() + 1);
42-
tmp[in.size()] = '\0';
43-
44-
int i = NormalisePath::normalize_path_inplace(
45-
reinterpret_cast<unsigned char *>(tmp),
46-
in.size(), 1, &changed);
47-
48-
std::string ret("");
49-
out.assign(tmp, i);
50-
free(tmp);
39+
out.assign(in);
40+
auto size = NormalisePath::normalize_path_inplace(
41+
reinterpret_cast<unsigned char *>(&out[0]), out.length(), 1, &changed);
42+
out.resize(size);
5143
}
5244

5345

src/actions/transformations/parity_even_7bit.cc

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,8 @@ namespace transformations {
3030
void ParityEven7bit::execute(const Transaction *t,
3131
const ModSecString &in,
3232
ModSecString &out) noexcept {
33-
unsigned char *input;
34-
35-
input = reinterpret_cast<unsigned char *>
36-
(malloc(sizeof(char) * in.length()+1));
37-
38-
if (input == NULL) {
39-
return;
40-
}
41-
42-
std::memcpy(input, in.c_str(), in.length()+1);
43-
44-
inplace(input, in.length());
45-
46-
out.assign(reinterpret_cast<char *>(input), in.length());
47-
free(input);
33+
out.assign(in);
34+
inplace(reinterpret_cast<unsigned char*>(&out[0]), out.size());
4835
}
4936

5037

src/actions/transformations/parity_odd_7bit.cc

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,8 @@ namespace transformations {
3030
void ParityOdd7bit::execute(const Transaction *t,
3131
const ModSecString &in,
3232
ModSecString &out) noexcept {
33-
unsigned char *input;
34-
35-
input = reinterpret_cast<unsigned char *>
36-
(malloc(sizeof(char) * in.length()+1));
37-
38-
if (input == NULL) {
39-
return;
40-
}
41-
42-
memcpy(input, in.c_str(), in.length()+1);
43-
44-
inplace(input, in.length());
45-
46-
out.assign(reinterpret_cast<char *>(input), in.length());
47-
free(input);
33+
out.assign(in);
34+
inplace(reinterpret_cast<unsigned char *>(&out[0]), out.length());
4835
}
4936

5037

src/actions/transformations/parity_zero_7bit.cc

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,8 @@ namespace transformations {
3030
void ParityZero7bit::execute(const Transaction *t,
3131
const ModSecString &in,
3232
ModSecString &out) noexcept {
33-
unsigned char *input;
34-
35-
input = reinterpret_cast<unsigned char *>
36-
(malloc(sizeof(char) * in.length()+1));
37-
38-
if (input == NULL) {
39-
return;
40-
}
41-
42-
memcpy(input, in.c_str(), in.length()+1);
43-
44-
inplace(input, in.length());
45-
46-
out.assign(reinterpret_cast<char *>(input), in.length());
47-
free(input);
33+
out.assign(in);
34+
inplace(reinterpret_cast<unsigned char *>(&out[0]), out.length());
4835
}
4936

5037

src/actions/transformations/sql_hex_decode.cc

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,9 @@ namespace transformations {
3232
void SqlHexDecode::execute(const Transaction *t,
3333
const ModSecString &in,
3434
ModSecString &out) noexcept {
35-
unsigned char *input;
36-
int size = 0;
37-
38-
input = reinterpret_cast<unsigned char *>
39-
(malloc(sizeof(char) * in.length()+1));
40-
41-
if (input == NULL) {
42-
return;
43-
}
44-
45-
memcpy(input, in.c_str(), in.length()+1);
46-
47-
size = inplace(input, in.length());
48-
49-
out.assign(reinterpret_cast<char *>(input), size);
50-
free(input);
35+
out.assign(in);
36+
auto size = inplace(reinterpret_cast<unsigned char*>(&out[0]), out.size());
37+
out.resize(size);
5138
}
5239

5340

src/actions/transformations/url_decode.cc

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,14 @@ namespace transformations {
3232
void UrlDecode::execute(const Transaction *t,
3333
const ModSecString &in,
3434
ModSecString &out) noexcept {
35-
unsigned char *val(NULL);
3635
int invalid_count = 0;
3736
int changed;
3837

39-
val = (unsigned char *) malloc(sizeof(char) * in.size() + 1);
40-
memcpy(val, in.c_str(), in.size() + 1);
41-
val[in.size()] = '\0';
42-
43-
int size = utils::urldecode_nonstrict_inplace(val, in.size(),
38+
out.assign(in);
39+
int size = utils::urldecode_nonstrict_inplace(
40+
reinterpret_cast<unsigned char *>(&out[0]), out.size(),
4441
&invalid_count, &changed);
45-
out.append((const char *)val, size);
46-
47-
free(val);
42+
out.resize(size);
4843
}
4944

5045

src/actions/transformations/url_decode_uni.cc

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,9 @@ namespace transformations {
3333
void UrlDecodeUni::execute(const Transaction *t,
3434
const ModSecString &in,
3535
ModSecString &out) noexcept {
36-
unsigned char *input;
37-
38-
input = reinterpret_cast<unsigned char *>
39-
(malloc(sizeof(char) * in.length()+1));
40-
41-
if (input == NULL) {
42-
return;
43-
}
44-
45-
memcpy(input, in.c_str(), in.length()+1);
46-
47-
size_t i = inplace(input, in.length(), t);
48-
49-
out.assign(reinterpret_cast<char *>(input), i);
50-
free(input);
36+
out.assign(in);
37+
size_t i = inplace(reinterpret_cast<unsigned char *>(&out[0]), out.length(), t);
38+
out.resize(i);
5139
}
5240

5341

src/actions/transformations/utf8_to_unicode.cc

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,19 @@ namespace transformations {
3232
void Utf8ToUnicode::execute(const Transaction *t,
3333
const ModSecString &in,
3434
ModSecString &out) noexcept {
35-
unsigned char *input;
3635
int changed = 0;
3736
char *out2;
3837

39-
input = reinterpret_cast<unsigned char *>
40-
(malloc(sizeof(char) * in.length()+1));
41-
42-
if (input == NULL) {
43-
return;
44-
}
45-
46-
memset(input, '\0', in.length()+1);
47-
memcpy(input, in.c_str(), in.length()+1);
48-
49-
out2 = inplace(input, in.size() + 1, &changed);
38+
out2 = inplace(reinterpret_cast<const unsigned char *>(&in[0]), in.size() + 1, &changed);
5039
if (out2 != NULL) {
5140
out.assign(reinterpret_cast<char *>(out2),
5241
strlen(reinterpret_cast<char *>(out2)));
5342
free(out2);
5443
}
55-
free(input);
5644
}
5745

5846

59-
char *Utf8ToUnicode::inplace(unsigned char *input,
47+
char *Utf8ToUnicode::inplace(const unsigned char *input,
6048
uint64_t input_len, int *changed) {
6149
unsigned int count = 0;
6250
char *data;
@@ -84,7 +72,7 @@ char *Utf8ToUnicode::inplace(unsigned char *input,
8472
int unicode_len = 0;
8573
unsigned int d = 0;
8674
unsigned char c;
87-
unsigned char *utf = (unsigned char *)&input[i];
75+
const unsigned char *utf = &input[i];
8876

8977
c = *utf;
9078

src/actions/transformations/utf8_to_unicode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Utf8ToUnicode : public Transformation {
5050
UNICODE_ERROR_DECODING_ERROR = -5
5151
};
5252

53-
static char *inplace(unsigned char *input, uint64_t input_len,
53+
static char *inplace(const unsigned char *input, uint64_t input_len,
5454
int *changed);
5555
};
5656

0 commit comments

Comments
 (0)