@@ -40,6 +40,10 @@ static void msc_xml_on_start_elementns(
40
40
int * new_stack_item = (int * )apr_array_push (xml_parser_state -> has_child_stack );
41
41
* new_stack_item = 0 ;
42
42
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 ;
43
47
44
48
// if there is an item before the current one we set that has a child
45
49
if (xml_parser_state -> depth > 1 ) {
@@ -104,14 +108,27 @@ static void msc_xml_on_end_elementns(
104
108
xml_parser_state -> currpath = newpath ;
105
109
106
110
xml_parser_state -> depth -- ;
111
+ xml_parser_state -> currval = NULL ;
107
112
}
108
113
109
114
static void msc_xml_on_characters (void * ctx , const xmlChar * ch , int len ) {
110
115
111
116
modsec_rec * msr = (modsec_rec * )ctx ;
112
117
msc_xml_parser_state * xml_parser_state = msr -> xml -> xml_parser_state ;
113
118
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
+
115
132
}
116
133
117
134
0 commit comments