From f19ae5dcd0b618cdeb80d6a1df5b13610d0ff7da Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Sat, 28 Jul 2018 00:00:24 +0000 Subject: Add ToXML{Instruction,Text,CharData}() checked conversion functions All usages were previously checked correctly, but this consolidates some code as well. Change-Id: I63711748b31b698a3f21f98fdb536db1e9e0b1cf Reviewed-on: https://pdfium-review.googlesource.com/39010 Commit-Queue: Lei Zhang Reviewed-by: Lei Zhang --- core/fxcrt/xml/cfx_xmlchardata.h | 6 ++++++ core/fxcrt/xml/cfx_xmlchardata_unittest.cpp | 2 +- core/fxcrt/xml/cfx_xmlelement.cpp | 8 +++----- core/fxcrt/xml/cfx_xmlelement_unittest.cpp | 2 +- core/fxcrt/xml/cfx_xmlinstruction.h | 6 ++++++ core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp | 13 ++++--------- core/fxcrt/xml/cfx_xmlparser.cpp | 8 +++----- core/fxcrt/xml/cfx_xmlparser_unittest.cpp | 3 +-- core/fxcrt/xml/cfx_xmltext.h | 9 +++++++++ core/fxcrt/xml/cfx_xmltext_unittest.cpp | 2 +- 10 files changed, 35 insertions(+), 24 deletions(-) (limited to 'core') diff --git a/core/fxcrt/xml/cfx_xmlchardata.h b/core/fxcrt/xml/cfx_xmlchardata.h index 013414bc82..9e5669a464 100644 --- a/core/fxcrt/xml/cfx_xmlchardata.h +++ b/core/fxcrt/xml/cfx_xmlchardata.h @@ -25,4 +25,10 @@ class CFX_XMLCharData : public CFX_XMLText { void Save(const RetainPtr& pXMLStream) override; }; +inline CFX_XMLCharData* ToXMLCharData(CFX_XMLNode* pNode) { + return pNode && pNode->GetType() == FX_XMLNODE_CharData + ? static_cast(pNode) + : nullptr; +} + #endif // CORE_FXCRT_XML_CFX_XMLCHARDATA_H_ diff --git a/core/fxcrt/xml/cfx_xmlchardata_unittest.cpp b/core/fxcrt/xml/cfx_xmlchardata_unittest.cpp index 4f0c49eaac..41906edbfd 100644 --- a/core/fxcrt/xml/cfx_xmlchardata_unittest.cpp +++ b/core/fxcrt/xml/cfx_xmlchardata_unittest.cpp @@ -26,7 +26,7 @@ TEST(CFX_XMLCharDataTest, Clone) { EXPECT_TRUE(clone != nullptr); EXPECT_NE(&data, clone); ASSERT_EQ(FX_XMLNODE_CharData, clone->GetType()); - EXPECT_EQ(L"My Data", static_cast(clone)->GetText()); + EXPECT_EQ(L"My Data", ToXMLCharData(clone)->GetText()); } TEST(CFX_XMLCharDataTest, Save) { diff --git a/core/fxcrt/xml/cfx_xmlelement.cpp b/core/fxcrt/xml/cfx_xmlelement.cpp index 4bb4eae1bd..bd24091205 100644 --- a/core/fxcrt/xml/cfx_xmlelement.cpp +++ b/core/fxcrt/xml/cfx_xmlelement.cpp @@ -76,13 +76,11 @@ WideString CFX_XMLElement::GetNamespaceURI() const { WideString CFX_XMLElement::GetTextData() const { CFX_WideTextBuf buffer; - for (CFX_XMLNode* pChild = GetFirstChild(); pChild; pChild = pChild->GetNextSibling()) { - if (pChild->GetType() == FX_XMLNODE_Text || - pChild->GetType() == FX_XMLNODE_CharData) { - buffer << static_cast(pChild)->GetText(); - } + CFX_XMLText* pText = ToXMLText(pChild); + if (pText) + buffer << pText->GetText(); } return buffer.MakeString(); } diff --git a/core/fxcrt/xml/cfx_xmlelement_unittest.cpp b/core/fxcrt/xml/cfx_xmlelement_unittest.cpp index dfc60a10c3..a1c63589ec 100644 --- a/core/fxcrt/xml/cfx_xmlelement_unittest.cpp +++ b/core/fxcrt/xml/cfx_xmlelement_unittest.cpp @@ -104,7 +104,7 @@ TEST(CFX_XMLElementTest, Clone) { EXPECT_TRUE(inst->GetFirstChild()->GetNextSibling() == nullptr); ASSERT_EQ(FX_XMLNODE_Text, inst->GetFirstChild()->GetType()); - auto* text = static_cast(inst->GetFirstChild()); + auto* text = ToXMLText(inst->GetFirstChild()); EXPECT_EQ(L"Text Child", text->GetText()); } diff --git a/core/fxcrt/xml/cfx_xmlinstruction.h b/core/fxcrt/xml/cfx_xmlinstruction.h index 153ef2b625..8962d73ac1 100644 --- a/core/fxcrt/xml/cfx_xmlinstruction.h +++ b/core/fxcrt/xml/cfx_xmlinstruction.h @@ -36,4 +36,10 @@ class CFX_XMLInstruction : public CFX_XMLNode { std::vector m_TargetData; }; +inline CFX_XMLInstruction* ToXMLInstruction(CFX_XMLNode* pNode) { + return pNode && pNode->GetType() == FX_XMLNODE_Instruction + ? static_cast(pNode) + : nullptr; +} + #endif // CORE_FXCRT_XML_CFX_XMLINSTRUCTION_H_ diff --git a/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp index b25a18be9c..0109ab2985 100644 --- a/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp +++ b/core/fxcrt/xml/cfx_xmlinstruction_unittest.cpp @@ -53,8 +53,7 @@ TEST(CFX_XMLInstructionTest, Clone) { EXPECT_TRUE(clone != nullptr); ASSERT_EQ(FX_XMLNODE_Instruction, clone->GetType()); - CFX_XMLInstruction* inst = static_cast(clone); - + CFX_XMLInstruction* inst = ToXMLInstruction(clone); EXPECT_TRUE(inst->IsAcrobat()); auto& data = inst->GetTargetData(); @@ -98,8 +97,7 @@ TEST(CFX_XMLInstructionTest, ParseAndReSave) { ASSERT_TRUE(root->GetFirstChild() != nullptr); ASSERT_EQ(FX_XMLNODE_Instruction, root->GetFirstChild()->GetType()); - CFX_XMLInstruction* node = - static_cast(root->GetFirstChild()); + CFX_XMLInstruction* node = ToXMLInstruction(root->GetFirstChild()); ASSERT_TRUE(node != nullptr); EXPECT_TRUE(node->IsAcrobat()); @@ -136,12 +134,9 @@ TEST(CFX_XMLInstructionTest, ParseAndReSaveInnerInstruction) { EXPECT_EQ(L"node", node->GetName()); CFX_XMLInstruction* instruction = nullptr; - for (auto* elem = node->GetFirstChild(); elem; + for (auto* elem = node->GetFirstChild(); elem && !instruction; elem = elem->GetNextSibling()) { - if (elem->GetType() == FX_XMLNODE_Instruction) { - instruction = static_cast(elem); - break; - } + instruction = ToXMLInstruction(elem); } ASSERT_TRUE(instruction != nullptr); EXPECT_TRUE(instruction->IsAcrobat()); diff --git a/core/fxcrt/xml/cfx_xmlparser.cpp b/core/fxcrt/xml/cfx_xmlparser.cpp index dd28cf8adc..094daac889 100644 --- a/core/fxcrt/xml/cfx_xmlparser.cpp +++ b/core/fxcrt/xml/cfx_xmlparser.cpp @@ -518,12 +518,10 @@ void CFX_XMLParser::ProcessTargetData() { WideString target_data = GetTextData(); if (target_data.IsEmpty()) return; - if (!current_node_) - return; - if (current_node_->GetType() != FX_XMLNODE_Instruction) - return; - static_cast(current_node_)->AppendData(target_data); + CFX_XMLInstruction* instruction = ToXMLInstruction(current_node_); + if (instruction) + instruction->AppendData(target_data); } WideString CFX_XMLParser::GetTextData() { diff --git a/core/fxcrt/xml/cfx_xmlparser_unittest.cpp b/core/fxcrt/xml/cfx_xmlparser_unittest.cpp index 300131db95..c1236ce182 100644 --- a/core/fxcrt/xml/cfx_xmlparser_unittest.cpp +++ b/core/fxcrt/xml/cfx_xmlparser_unittest.cpp @@ -325,8 +325,7 @@ TEST_F(CFX_XMLParserTest, ParseInstruction) { ASSERT_TRUE(root->GetFirstChild() != nullptr); ASSERT_EQ(FX_XMLNODE_Instruction, root->GetFirstChild()->GetType()); - CFX_XMLInstruction* instruction = - static_cast(root->GetFirstChild()); + CFX_XMLInstruction* instruction = ToXMLInstruction(root->GetFirstChild()); EXPECT_TRUE(instruction->IsOriginalXFAVersion()); } diff --git a/core/fxcrt/xml/cfx_xmltext.h b/core/fxcrt/xml/cfx_xmltext.h index c5c168059d..f7feae669e 100644 --- a/core/fxcrt/xml/cfx_xmltext.h +++ b/core/fxcrt/xml/cfx_xmltext.h @@ -31,4 +31,13 @@ class CFX_XMLText : public CFX_XMLNode { WideString m_wsText; }; +inline bool IsXMLText(CFX_XMLNode* pNode) { + FX_XMLNODETYPE type = pNode->GetType(); + return type == FX_XMLNODE_Text || type == FX_XMLNODE_CharData; +} + +inline CFX_XMLText* ToXMLText(CFX_XMLNode* pNode) { + return pNode && IsXMLText(pNode) ? static_cast(pNode) : nullptr; +} + #endif // CORE_FXCRT_XML_CFX_XMLTEXT_H_ diff --git a/core/fxcrt/xml/cfx_xmltext_unittest.cpp b/core/fxcrt/xml/cfx_xmltext_unittest.cpp index 6e53f66612..04b1ee8571 100644 --- a/core/fxcrt/xml/cfx_xmltext_unittest.cpp +++ b/core/fxcrt/xml/cfx_xmltext_unittest.cpp @@ -25,7 +25,7 @@ TEST(CFX_XMLTextTest, Clone) { CFX_XMLNode* clone = data.Clone(&doc); EXPECT_TRUE(clone != nullptr); ASSERT_EQ(FX_XMLNODE_Text, clone->GetType()); - EXPECT_EQ(L"My Data", static_cast(clone)->GetText()); + EXPECT_EQ(L"My Data", ToXMLText(clone)->GetText()); } TEST(CFX_XMLTextTest, Save) { -- cgit v1.2.3