我正在运行一段代码,其中包含2个函数并运行valgrind,获取大小为1的无效读取。我们无法识别问题请帮助
RrSSystemIntf_i::RrSSystemIntf_i()
{
RrXmlReader cfgReader;
char* configFile = cfgFile;
char* pss;
U pssId;
if (ROK != cfgReader.readConfig(configFile, (char*)"ABCD", (char*)"RR_NODES",
&pss)) {
RR_ALERT("RrSSystemIntf_i: readConfig failed. exiting...");
_exit();
}
pssId = atoi(pss);
}
int RrXmlReader::readConfig(char*& confFile, char* elem_type, char* val_type,
char** ret_val)
{
reader.getValue(curContext, val_type, value);
if (NULL == value) {
RR_ALERT("XmlFactory::rrNodes : Config Error: %s missing from %s", val_type,
elem_type);
return RFAILED;
}
string returnStr;
returnStr = std::string(value);
*ret_val = (char*)(returnStr.c_str());
return ROK;
}
Valgrind追踪:
==30007== Invalid read of size 1
==30007== at 0x33296345CA: ____strtol_l_internal (in /lib64/libc-2.5.so)
==30007== by 0x52D5A64: RrS7SystemIntf_i::RrS7SystemIntf_i() (stdlib.h:336)
==30007== by 0x52AA9E7: RrObInit::initOb(int, char**) (RrObInit.cpp:360)
==30007== by 0x52ACF6D: RrObInit::getInstance() (RrObInit.cpp:636)
==30007== by 0x52AE909: tst (RrTst.cpp:515)
==30007== by 0x4C12694: Init (gen.c:581)
==30007== by 0x4C1135C: Main (mtss.c:484)
==30007== by 0x52ADD7B: main (RrTst.cpp:225)
==30007== Address 0x8e9dc28 is 24 bytes inside a block of size 28 free'd
==30007== at 0x4A05743: operator delete(void*) (vg_replace_malloc.c:346)
==30007== by 0x52FC330: RrXmlReader::readConfig(char*&, char*, char*, char**) (basic_string.h:233)
==30007== by 0x52D590B: RrS7SystemIntf_i::RrgS7SystemIntf_i() (RrS7System_i.cpp:325)
感谢您的帮助。
答案 0 :(得分:3)
问题从这一行开始:
*ret_val = (char*)(returnStr.c_str());
您将通过ret_val
返回一个从readConfig
返回时无效的地址。
执行时,Valgrind会捕获内存访问问题:
pssId = atoi(pss);
因为pss
指向此时无效的内存。
我的建议:
将readConfig
更改为
int RrXmlReader::readConfig(char*& confFile, char* elem_type, char* val_type,
std::string& ret_val)