问题描述
在使用libxml解析文件时,我遇到了一个奇怪的错误。此代码在我将其编译为32位应用程序时工作。我改变它为一个64位的应用程序,它停止工作。
seg错误发生在if(xmlStrcmp(cur-> name,(const xmlChar *)服务器))
cur-> name是一个const xmlChar *,它指向一个地址,它说出它的出界。但是当我调试并去那个内存位置时,数据是正确的。
int XmlGetServers()
{
xmlDocPtr doc;
xmlNodePtr cur;
DOC = xmlParseFile(Pin.xml);
if(doc == NULL)
{
std :: cout<< \\\
Pin.xml未成功解析。 << std :: endl;
return -1;
}
cur = xmlDocGetRootElement(doc);
if(cur == NULL)
{
std :: cout< \\\
Pin.xml是空文档。 << std :: endl;
xmlFreeDoc(doc);
return -1;
}
if(xmlStrcmp(cur-> name,(const xmlChar *)servers))
{
std :: cout< \\\
ERROR:Pin.xml的类型错误,根节点!= servers。 << std :: endl;
xmlFreeDoc(doc);
return -1;
}
}
在cur初始化之前,name参数为
名称:name
详细信息:0xed11f72000007fff< Address 0xed11f72000007fff out of bounds>
cur初始化后,name参数为
名称:name
详细信息:0x64c43000000000<地址0x64c43000000000超出范围>
引用的XML文件
<?xml version =1.0?>
< servers>
< server_info>
< server_name> Server1< / server_name>
< server_ip> 127.0.0.1< / server_ip>
< server_data_port> 9000< / server_data_port>
< / server_info>
< server_info>
< server_name> Server2< / server_name>
< server_ip> 127.0.0.1< / server_ip>
< server_data_port> 9001< / server_data_port>
< / server_info>
< / servers>
系统:
操作系统:Redhat Enterprise Linux 6.4 64位
GCC:4.4.7-3
包:libxml2-2.7.6- 8.el6_3.4.x86_64
问题是我们在代码中使用#pragma pack $ b意味着DOMParser中的bool被压缩到1个字节,而Xerces不会#pragma pack并且得到4个字节的默认包装。
I am getting a weird segfault when using libxml to parse a file. This code worked previously when I compiled it as a 32bit application. I changed it to a 64 bit application and it stops working.
The seg fault comes in at "if (xmlStrcmp(cur->name, (const xmlChar *) "servers"))"
cur->name is a const xmlChar * and it points to an address that says its out out bounds. But when I debug and go to that memory location, that data is correct.
int XmlGetServers()
{
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile("Pin.xml");
if (doc == NULL)
{
std::cout << "\n Pin.xml not parsed successfully." << std::endl;
return -1;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL)
{
std::cout << "\n Pin.xml is empty document." << std::endl;
xmlFreeDoc(doc);
return -1;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "servers"))
{
std::cout << "\n ERROR: Pin.xml of the wrong type, root node != servers." << std::endl;
xmlFreeDoc(doc);
return -1;
}
}
Before cur is initialized the name parameter is
Name : name
Details:0xed11f72000007fff <Address 0xed11f72000007fff out of bounds>
After cur is initialized the name parameter is
Name : name
Details:0x64c43000000000 <Address 0x64c43000000000 out of bounds>
Referenced XML file
<?xml version="1.0"?>
<servers>
<server_info>
<server_name>Server1</server_name>
<server_ip>127.0.0.1</server_ip>
<server_data_port>9000</server_data_port>
</server_info>
<server_info>
<server_name>Server2</server_name>
<server_ip>127.0.0.1</server_ip>
<server_data_port>9001</server_data_port>
</server_info>
</servers>
System:
OS: Redhat Enterprise Linux 6.4 64-bit
GCC: 4.4.7-3
packages: libxml2-2.7.6-8.el6_3.4.x86_64
The problem was that we used #pragma pack(1) in our code,which means the bools in the DOMParser are packed down to 1 byte, whereas Xerces doesn't #pragma pack and gets the default packing of 4 bytes.
这篇关于地址读取xml时超出范围错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!