From 96b0d1332430e34219334373aeb0f6a85eb8ad68 Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Wed, 25 Jul 2018 17:04:49 +0000 Subject: Introduce ToXMLElement() checked downcast helper function And use it in place of ASSERT() that the type was correct. Since we can check it, doing so at runtime may help avoid type confusion, and we'll get a nice safe SEGV if the asserted condition ever fails. Change-Id: I46b65a4b70e819a670d7cad7966e0d100f0d9a63 Reviewed-on: https://pdfium-review.googlesource.com/38810 Commit-Queue: Tom Sepez Reviewed-by: Lei Zhang --- core/fxcrt/xml/cfx_xmlelement.h | 6 ++++++ fxjs/xfa/cjx_object.cpp | 14 ++++---------- xfa/fxfa/parser/cxfa_document.cpp | 7 +++---- xfa/fxfa/parser/cxfa_document_parser.cpp | 8 +++----- xfa/fxfa/parser/cxfa_node.cpp | 7 +++---- xfa/fxfa/parser/xfa_utils.cpp | 15 ++++----------- 6 files changed, 23 insertions(+), 34 deletions(-) diff --git a/core/fxcrt/xml/cfx_xmlelement.h b/core/fxcrt/xml/cfx_xmlelement.h index ca5fd5e797..0d3468aba4 100644 --- a/core/fxcrt/xml/cfx_xmlelement.h +++ b/core/fxcrt/xml/cfx_xmlelement.h @@ -54,4 +54,10 @@ class CFX_XMLElement : public CFX_XMLNode { std::map attrs_; }; +inline CFX_XMLElement* ToXMLElement(CFX_XMLNode* pNode) { + return pNode && pNode->GetType() == FX_XMLNODE_Element + ? static_cast(pNode) + : nullptr; +} + #endif // CORE_FXCRT_XML_CFX_XMLELEMENT_H_ diff --git a/fxjs/xfa/cjx_object.cpp b/fxjs/xfa/cjx_object.cpp index 2cf6f6755d..1c0454d425 100644 --- a/fxjs/xfa/cjx_object.cpp +++ b/fxjs/xfa/cjx_object.cpp @@ -480,13 +480,11 @@ void CJX_Object::SetCData(XFA_Attribute eAttr, return; } - auto* elem = static_cast(xfaObj->GetXMLMappingNode()); - ASSERT(elem->GetType() == FX_XMLNODE_Element); - WideString wsAttrName = CXFA_Node::AttributeToName(eAttr); if (eAttr == XFA_Attribute::ContentType) wsAttrName = L"xfa:" + wsAttrName; + CFX_XMLElement* elem = ToXMLElement(xfaObj->GetXMLMappingNode()); elem->SetAttribute(wsAttrName, wsValue); return; } @@ -544,14 +542,10 @@ CFX_XMLElement* CJX_Object::SetValue(XFA_Attribute eAttr, OnChanging(eAttr, bNotify); SetMapModuleValue(pKey, pValue); OnChanged(eAttr, bNotify, false); - if (!ToNode(GetXFAObject())->IsNeedSavingXMLNode()) - return nullptr; - - auto* elem = - static_cast(ToNode(GetXFAObject())->GetXMLMappingNode()); - ASSERT(elem->GetType() == FX_XMLNODE_Element); - return elem; + CXFA_Node* pNode = ToNode(GetXFAObject()); + return pNode->IsNeedSavingXMLNode() ? ToXMLElement(pNode->GetXMLMappingNode()) + : nullptr; } void CJX_Object::SetContent(const WideString& wsContent, diff --git a/xfa/fxfa/parser/cxfa_document.cpp b/xfa/fxfa/parser/cxfa_document.cpp index 5a85482b17..3e8046677a 100644 --- a/xfa/fxfa/parser/cxfa_document.cpp +++ b/xfa/fxfa/parser/cxfa_document.cpp @@ -539,10 +539,9 @@ void CreateDataBinding(CXFA_Node* pFormNode, false); } } else { - CFX_XMLNode* pXMLNode = pDataNode->GetXMLMappingNode(); - ASSERT(pXMLNode->GetType() == FX_XMLNODE_Element); - static_cast(pXMLNode)->SetAttribute( - L"xfa:dataNode", L"dataGroup"); + CFX_XMLElement* pElement = + ToXMLElement(pDataNode->GetXMLMappingNode()); + pElement->SetAttribute(L"xfa:dataNode", L"dataGroup"); } } else if (!wsValue.IsEmpty()) { pDataNode->JSObject()->SetAttributeValue( diff --git a/xfa/fxfa/parser/cxfa_document_parser.cpp b/xfa/fxfa/parser/cxfa_document_parser.cpp index 8e5ff9fbde..27d80cde15 100644 --- a/xfa/fxfa/parser/cxfa_document_parser.cpp +++ b/xfa/fxfa/parser/cxfa_document_parser.cpp @@ -669,11 +669,9 @@ CXFA_Node* CXFA_DocumentParser::ParseAsXDPPacket_Data( if (pParentXMLNode) pParentXMLNode->RemoveChildNode(pXMLDocumentNode); - ASSERT(pXMLDocumentNode->GetType() == FX_XMLNODE_Element); - if (pXMLDocumentNode->GetType() == FX_XMLNODE_Element) { - static_cast(pXMLDocumentNode) - ->RemoveAttribute(L"xmlns:xfa"); - } + CFX_XMLElement* pElement = ToXMLElement(pXMLDocumentNode); + pElement->RemoveAttribute(L"xmlns:xfa"); + // The node was either removed from the parent above, or already has no // parent so we can take ownership. pDataElement->AppendChild(pXMLDocumentNode); diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index 6d45eb8f61..5f919b7b14 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -2712,11 +2712,10 @@ void CXFA_Node::SetImageEdit(const WideString& wsContentType, CXFA_Node* pHrefNode = pBind->GetFirstChild(); if (pHrefNode) { pHrefNode->JSObject()->SetCData(XFA_Attribute::Value, wsHref, false, false); - } else { - CFX_XMLNode* pXMLNode = pBind->GetXMLMappingNode(); - ASSERT(pXMLNode && pXMLNode->GetType() == FX_XMLNODE_Element); - static_cast(pXMLNode)->SetAttribute(L"href", wsHref); + return; } + CFX_XMLElement* pElement = ToXMLElement(pBind->GetXMLMappingNode()); + pElement->SetAttribute(L"href", wsHref); } CXFA_FFWidget* CXFA_Node::GetNextWidget(CXFA_FFWidget* pWidget) { diff --git a/xfa/fxfa/parser/xfa_utils.cpp b/xfa/fxfa/parser/xfa_utils.cpp index b42da0b4a1..54cca88409 100644 --- a/xfa/fxfa/parser/xfa_utils.cpp +++ b/xfa/fxfa/parser/xfa_utils.cpp @@ -497,20 +497,13 @@ void XFA_DataExporter_DealWithDataGroupNode(CXFA_Node* pDataNode) { if (pDataNode->GetElementType() != XFA_Element::DataGroup) return; + CFX_XMLElement* pElement = ToXMLElement(pDataNode->GetXMLMappingNode()); if (iChildNum > 0) { - CFX_XMLNode* pXMLNode = pDataNode->GetXMLMappingNode(); - ASSERT(pXMLNode->GetType() == FX_XMLNODE_Element); - CFX_XMLElement* pXMLElement = static_cast(pXMLNode); - if (pXMLElement->HasAttribute(L"xfa:dataNode")) - pXMLElement->RemoveAttribute(L"xfa:dataNode"); - + if (pElement->HasAttribute(L"xfa:dataNode")) + pElement->RemoveAttribute(L"xfa:dataNode"); return; } - - CFX_XMLNode* pXMLNode = pDataNode->GetXMLMappingNode(); - ASSERT(pXMLNode->GetType() == FX_XMLNODE_Element); - static_cast(pXMLNode)->SetAttribute(L"xfa:dataNode", - L"dataGroup"); + pElement->SetAttribute(L"xfa:dataNode", L"dataGroup"); } void XFA_DataExporter_RegenerateFormFile( -- cgit v1.2.3