Skip to content

Commit 19b7e98

Browse files
committed
Change node value's parsing to concatenate instead of duplicate it every time
1 parent 4c043a0 commit 19b7e98

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

apache2/msc_xml.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ static void msc_xml_on_start_elementns(
4040
int *new_stack_item = (int *)apr_array_push(xml_parser_state->has_child_stack);
4141
*new_stack_item = 0;
4242
xml_parser_state->depth++;
43+
// set null to the current value
44+
// this is necessary because if there is any text between the tags (new line, etc)
45+
// it will be added to the current value
46+
xml_parser_state->currval = NULL;
4347

4448
// if there is an item before the current one we set that has a child
4549
if (xml_parser_state->depth > 1) {
@@ -104,14 +108,27 @@ static void msc_xml_on_end_elementns(
104108
xml_parser_state->currpath = newpath;
105109

106110
xml_parser_state->depth--;
111+
xml_parser_state->currval = NULL;
107112
}
108113

109114
static void msc_xml_on_characters(void *ctx, const xmlChar *ch, int len) {
110115

111116
modsec_rec * msr = (modsec_rec *)ctx;
112117
msc_xml_parser_state * xml_parser_state = msr->xml->xml_parser_state;
113118

114-
xml_parser_state->currval = apr_pstrndup(msr->mp, (const char *)ch, len);
119+
// libxml2 SAX parser will call this function multiple times
120+
// during the parsing of a single node, if the value has multibyte
121+
// characters, so we need to concatenate the values
122+
xml_parser_state->currval = apr_pstrcat(msr->mp,
123+
((xml_parser_state->currval != NULL) ? xml_parser_state->currval : ""),
124+
apr_pstrndup(msr->mp, (const char *)ch, len),
125+
NULL);
126+
// check if the memory allocation was successful
127+
if (xml_parser_state->currval == NULL) {
128+
msr->xml->xml_error = apr_psprintf(msr->mp, "Failed to allocate memory for XML value.");
129+
xmlStopParser((xmlParserCtxtPtr)msr->xml->parsing_ctx_arg);
130+
}
131+
115132
}
116133

117134

0 commit comments

Comments
 (0)