tinyxml 的一个小 bug

来源:互联网 发布:node.js 做信息系统 编辑:程序博客网 时间:2024/05/17 00:51
阅读 tinyxml 的源代码 (版本: 2.5.3),发现一个小小的问题,先上测试代码吧:

void xml_test()
{
    {
        const char* xmlString =
            "<country name='china' name = 'china'>"
            "   <city name='beijing'/>"
            "</country>";

        TiXmlDocument doc;
        doc.Parse( xmlString );

        if ( doc.Error() )
        {
            printf( "Error in %s: %sn", doc.Value(), doc.ErrorDesc() ); //这句未被执行到
        }
    }
    {
        const char* xmlString =
            "<country name='china'>"
            "   <city name='beijing' name='beijing'/>"
            "</country>";

        TiXmlDocument doc;
        doc.Parse( xmlString );

        if ( doc.Error() )
        {
            printf( "Error in %s: %sn", doc.Value(), doc.ErrorDesc() ); //这句执行到
        }
    }
}

在上面的测试字符串中,红色部分代表某个 Element 设了重名的属性。
从测试中发现,第一个字符串解析后未打印错误消息,也就是 tinyxml 认为解析成功了,但事实上 city 并未加载到 xml 模型树里,也就是说,这时一次失败的解析。
而第二个字符串解析后打印了一条错误消息。

分析代码,当 tinyxml 解析发现某一 Element 具有重名的属性时,是这样处理的

const char* TiXmlElement::Parse( const char* p, TiXmlParsingData* data, TiXmlEncoding encoding )
{
    ......
    // Handle the strange case of double attributes:
    #ifdef TIXML_USE_STL
    TiXmlAttribute* node = attributeSet.Find( attrib->NameTStr() );
    #else
    TiXmlAttribute* node = attributeSet.Find( attrib->Name() );
    #endif
    if ( node )
    {
        node->SetValue( attrib->Value() );
        delete attrib;
        return 0;
    }
    ......
    
    return p;
}

也就是直接返回了 0。
而当正常解析时,返回 xml 字符串中的下一个字符的位置。

而当这个 Element 是 Root Element 时, tinyxml 查询到返回 0 或到了 xml 字符串尾会认为是正常解析结束返回,导致错误。
while ( p && *p )
{
    TiXmlNode* node = Identify( p, encoding );
    if ( node )
    {
        p = node->Parse( p, &data, encoding );
        LinkEndChild( node );
    }
    else
    {
        break;
    }
    ......
}

在这里应该对 if (p) 单独处理,如果出现这种情况,应该是解析发生了错误,应该单独处理。

原创粉丝点击