Fix guththila serialization of special chars in attr
JIRA: AXIS2C-1627
diff --git a/guththila/include/guththila_xml_writer.h b/guththila/include/guththila_xml_writer.h
index f57d1be..5bf4c10 100644
--- a/guththila/include/guththila_xml_writer.h
+++ b/guththila/include/guththila_xml_writer.h
@@ -260,6 +260,19 @@
const axutil_env_t * env);
/*
+ * Write a buffer with special chars on it
+ * The special chars will be escaped
+ * @param wr pointer to the writer
+ * @param buff character string
+ * @param env pointer to the environment
+ */
+GUTHTHILA_EXPORT int GUTHTHILA_CALL guththila_write_escaped_buffer(
+ guththila_xml_writer_t * wr,
+ char *buff,
+ const axutil_env_t * env);
+
+
+/*
* Start to write an empty element with the given name.
* @param wr pointer to the writer
* @param name name of the element
diff --git a/guththila/src/guththila_xml_writer.c b/guththila/src/guththila_xml_writer.c
index 8e68f6a..d993a07 100644
--- a/guththila/src/guththila_xml_writer.c
+++ b/guththila/src/guththila_xml_writer.c
@@ -725,7 +725,7 @@
guththila_char_t *buff,
const axutil_env_t * env)
{
- size_t len = strlen(buff);
+
if(wr->status == START)
{
wr->status = BEGINING;
@@ -741,39 +741,7 @@
{
return GUTHTHILA_FAILURE;
}
- while(len > 0)
- {
- size_t i = 0;
- /* scan buffer until the next special character (&, <, >, ', ") these need to be escaped,
- * otherwise XML will not be valid*/
- guththila_char_t *pos = (guththila_char_t*)strpbrk(buff, "&<>'\"");
- if(pos)
- {
- i = pos - buff;
- }
- else
- {
- i = len;
- }
-
- /* write everything until the special character */
- if(i > 0)
- {
- guththila_write(wr, buff, i, env);
- buff += i;
- len -= i;
- }
- /* replace the character with the appropriate sequence */
- if(len > 0)
- {
- if(AXIS2_SUCCESS != guththila_write_escape_character(wr, buff, env))
- return GUTHTHILA_FAILURE;
- /* skip the character */
- buff++;
- len--;
- }
- }
- return GUTHTHILA_SUCCESS;
+ return guththila_write_escaped_buffer(wr,buff,env);
}
GUTHTHILA_EXPORT int GUTHTHILA_CALL
@@ -842,6 +810,50 @@
}
GUTHTHILA_EXPORT int GUTHTHILA_CALL
+guththila_write_escaped_buffer(
+ guththila_xml_writer_t * wr,
+ char *buff,
+ const axutil_env_t * env)
+{
+ size_t len = strlen(buff);
+
+ while(len > 0)
+ {
+ size_t i = 0;
+ /* scan buffer until the next special character (&, <, >, ', ") these need to be escaped,
+ * otherwise XML will not be valid*/
+ guththila_char_t *pos = (guththila_char_t*)strpbrk(buff, "&<>'\"");
+ if(pos)
+ {
+ i = pos - buff;
+ }
+ else
+ {
+ i = len;
+ }
+
+ /* write everything until the special character */
+ if(i > 0)
+ {
+ guththila_write(wr, buff, i, env);
+ buff += i;
+ len -= i;
+ }
+ /* replace the character with the appropriate sequence */
+ if(len > 0)
+ {
+ if(AXIS2_SUCCESS != guththila_write_escape_character(wr, buff, env))
+ return GUTHTHILA_FAILURE;
+ /* skip the character */
+ buff++;
+ len--;
+ }
+ }
+ return GUTHTHILA_SUCCESS;
+
+}
+
+GUTHTHILA_EXPORT int GUTHTHILA_CALL
guththila_write_empty_element(
guththila_xml_writer_t * wr,
guththila_char_t *start_element,
@@ -1087,7 +1099,8 @@
guththila_write(wr, " ", 1u, env);
guththila_write(wr, localname, strlen(localname), env);
guththila_write(wr, "=\"", 2u, env);
- guththila_write(wr, value, strlen(value), env);
+ if(guththila_write_escaped_buffer(wr, value, env)!=GUTHTHILA_SUCCESS)
+ return GUTHTHILA_FAILURE;
guththila_write(wr, "\"", 1u, env);
return GUTHTHILA_SUCCESS;
}
@@ -1147,7 +1160,8 @@
guththila_write(wr, ":", 1u, env);
guththila_write(wr, localname, strlen(localname), env);
guththila_write(wr, "=\"", 2u, env);
- guththila_write(wr, value, strlen(value), env);
+ if(guththila_write_escaped_buffer(wr, value, env)!=GUTHTHILA_SUCCESS)
+ return GUTHTHILA_FAILURE;
guththila_write(wr, "\"", 1u, env);
return GUTHTHILA_SUCCESS;
}
@@ -1194,7 +1208,8 @@
guththila_write(wr, ":", 1u, env);
guththila_write(wr, loc_name, strlen(loc_name), env);
guththila_write(wr, "=\"", 2u, env);
- guththila_write(wr, value, strlen(value), env);
+ if(guththila_write_escaped_buffer(wr, value, env)!=GUTHTHILA_SUCCESS)
+ return GUTHTHILA_FAILURE;
guththila_write(wr, "\"", 1u, env);
return GUTHTHILA_SUCCESS;
}
diff --git a/guththila/tests/Makefile.am b/guththila/tests/Makefile.am
index 959bf9b..857d5b8 100644
--- a/guththila/tests/Makefile.am
+++ b/guththila/tests/Makefile.am
@@ -20,7 +20,8 @@
test_guththila_SOURCES = test_guththila.cc
AM_CPPFLAGS = -I$(top_builddir)/include \
- -I ../../util/include
+ -I ../../util/include \
+ -I ../../axiom/include
test_guththila_LDADD = \
$(top_builddir)/src/libguththila.la \
@@ -33,5 +34,6 @@
test_attribute_LDADD = \
$(top_builddir)/src/libguththila.la \
../../util/src/libaxutil.la \
+ ../../axiom/src/om/libaxis2_axiom.la \
$(top_builddir)/$(GTEST)/libgtest.a \
$(top_builddir)/$(GTEST)/libgtest_main.a
diff --git a/guththila/tests/test_attribute.cc b/guththila/tests/test_attribute.cc
index 1670298..eeea707 100644
--- a/guththila/tests/test_attribute.cc
+++ b/guththila/tests/test_attribute.cc
@@ -18,6 +18,7 @@
#include <gtest/gtest.h>
#include <guththila.h>
+#include <axiom.h>
#define BUF_SIZE 256
@@ -43,15 +44,11 @@
void TearDown()
{
- guththila_reader_free(m_reader, m_env);
- m_reader = nullptr;
- guththila_un_init(m_parser, m_env);
- m_parser = nullptr;
axutil_env_free(m_env);
}
- guththila_reader_t *m_reader;
+ guththila_reader_t *m_reader = nullptr;
axutil_allocator_t *m_allocator;
axutil_env_t *m_env;
guththila_t *m_parser;
@@ -85,6 +82,11 @@
ASSERT_STREQ(guththila_get_attribute_name(m_parser, att, m_env), "color");
ASSERT_STREQ(guththila_get_attribute_value(m_parser, att, m_env), "brown");
+ guththila_reader_free(m_reader, m_env);
+ m_reader = nullptr;
+ guththila_un_init(m_parser, m_env);
+ m_parser = nullptr;
+
}
@@ -129,4 +131,26 @@
ASSERT_STREQ(guththila_get_attribute_name(m_parser, att, m_env), "mustUnderstand");
ASSERT_STREQ(guththila_get_attribute_value(m_parser, att, m_env), "0");
+ guththila_reader_free(m_reader, m_env);
+ m_reader = nullptr;
+ guththila_un_init(m_parser, m_env);
+ m_parser = nullptr;
+
+}
+
+/* AXIS2C-1627 */
+TEST_F(TestAttribute, test_special_char_serialization)
+{
+ axiom_namespace_t * ns = axiom_namespace_create(m_env, "namespace", "ns");
+
+ axiom_node_t * node;
+ axiom_element_t * element = axiom_element_create(m_env, NULL, "el", ns, &node);
+
+ axiom_element_set_text(element, m_env, "T1 & T2", node);
+ axiom_element_add_attribute(element, m_env, axiom_attribute_create(m_env, "name", "A1 & A2", NULL), node);
+
+ axis2_char_t * xml = axiom_node_to_string(node, m_env);
+
+ ASSERT_STREQ(xml, "<ns:el xmlns:ns=\"namespace\" name=\"A1 & A2\">T1 & T2</ns:el>");
+
}