diff --git a/core/io/xml_parser.cpp b/core/io/xml_parser.cpp
index 154b55f5e72..abae48fdd89 100644
--- a/core/io/xml_parser.cpp
+++ b/core/io/xml_parser.cpp
@@ -137,7 +137,7 @@ bool XMLParser::_parse_cdata() {
next_char();
}
- if (cDataEnd) {
+ if (!cDataEnd) {
cDataEnd = P;
}
node_name = String::utf8(cDataBegin, (int)(cDataEnd - cDataBegin));
diff --git a/tests/core/io/test_xml_parser.h b/tests/core/io/test_xml_parser.h
index 87592b56ce9..35e86d82034 100644
--- a/tests/core/io/test_xml_parser.h
+++ b/tests/core/io/test_xml_parser.h
@@ -66,6 +66,170 @@ TEST_CASE("[XMLParser] End-to-end") {
parser.close();
}
+
+TEST_CASE("[XMLParser] Comments") {
+ XMLParser parser;
+
+ SUBCASE("Missing end of comment") {
+ const String input = "";
+ REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
+ REQUIRE_EQ(parser.read(), OK);
+ CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
+ CHECK_EQ(parser.get_node_name(), " example << ");
+ }
+ SUBCASE("Doctype") {
+ const String input = "]>";
+ REQUIRE_EQ(parser.open_buffer(input.to_utf8_buffer()), OK);
+ REQUIRE_EQ(parser.read(), OK);
+ CHECK_EQ(parser.get_node_type(), XMLParser::NodeType::NODE_COMMENT);
+ CHECK_EQ(parser.get_node_name(), "DOCTYPE greeting []");
+ }
+}
+
+TEST_CASE("[XMLParser] Premature endings") {
+ SUBCASE("Simple cases") {
+ String input;
+ String expected_name;
+ XMLParser::NodeType expected_type;
+
+ SUBCASE("Incomplete Unknown") {
+ input = "