Skip to content

Commit 59d7236

Browse files
committed
Perform EscapeSeqDecode transformation in-place
- Removed ansi_c_sequences_decode_inplace helper function from the class, as it's only referenced by the implementation.
1 parent fbdae2a commit 59d7236

File tree

2 files changed

+11
-25
lines changed

2 files changed

+11
-25
lines changed

src/actions/transformations/escape_seq_decode.cc

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ EscapeSeqDecode::EscapeSeqDecode(const std::string &action)
2626
}
2727

2828

29-
int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input,
30-
int input_len) {
31-
unsigned char *d = input;
32-
int i, count;
29+
static inline int ansi_c_sequences_decode_inplace(std::string &value) {
30+
auto d = reinterpret_cast<unsigned char *>(value.data());
31+
const unsigned char* input = d;
32+
const auto input_len = value.length();
3333

34-
i = count = 0;
34+
bool changed = false;
35+
std::string::size_type i = 0;
3536
while (i < input_len) {
3637
if ((input[i] == '\\') && (i + 1 < input_len)) {
3738
int c = -1;
@@ -109,42 +110,28 @@ int EscapeSeqDecode::ansi_c_sequences_decode_inplace(unsigned char *input,
109110
if (c == -1) {
110111
/* Didn't recognise encoding, copy raw bytes. */
111112
*d++ = input[i + 1];
112-
count++;
113113
i += 2;
114114
} else {
115115
/* Converted the encoding. */
116116
*d++ = c;
117-
count++;
118117
}
118+
119+
changed = true;
119120
} else {
120121
/* Input character not a backslash, copy it. */
121122
*d++ = input[i++];
122-
count++;
123123
}
124124
}
125125

126126
*d = '\0';
127127

128-
return count;
128+
value.resize(d - input);
129+
return changed;
129130
}
130131

131132

132133
bool EscapeSeqDecode::transform(std::string &value, const Transaction *trans) const {
133-
134-
unsigned char *tmp = (unsigned char *) malloc(sizeof(char)
135-
* value.size() + 1);
136-
memcpy(tmp, value.c_str(), value.size() + 1);
137-
tmp[value.size()] = '\0';
138-
139-
int size = ansi_c_sequences_decode_inplace(tmp, value.size());
140-
141-
std::string ret("");
142-
ret.assign(reinterpret_cast<char *>(tmp), size);
143-
free(tmp);
144-
145-
const auto changed = ret != value;
146-
value = ret;
147-
return changed;
134+
return ansi_c_sequences_decode_inplace(value);
148135
}
149136

150137

src/actions/transformations/escape_seq_decode.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class EscapeSeqDecode : public Transformation {
2525
explicit EscapeSeqDecode(const std::string &action);
2626

2727
bool transform(std::string &value, const Transaction *trans) const override;
28-
static int ansi_c_sequences_decode_inplace(unsigned char *input, int input_len);
2928
};
3029

3130
} // namespace modsecurity::actions::transformations

0 commit comments

Comments
 (0)