The following is a segment from a code which supposes to test communication with a server and obtain a picture from it:
bool testPICcomm(){
Poco::Logger &logger = Poco::Logger::get("PictureTester");
logger.setLevel(Poco::Message::PRIO_DEBUG);
HttpClient* h = new HttpClient(host, 80);
std::map<std::string, std::string> headers;
Poco::UTF8Encoding utf;
Encoder encoder(utf);
h->connect();
h->sendHttpRequest(askForPic,"","",0,0);
if((h->getHttpHeaders(headers, encoder)) == false)
{
logger.debug("Couldn't Retrieve Headers "+ false);
h->close();
return false;
}
logger.debug("got headers");
//get head data
std::string mime = headers["Content-Type"];
logger.debug("mime type:" + mime);
logger.debug("length string: " + std::string((headers["Content-Length"]).c_str()));
int length= atoi((headers["Content-Length"]).c_str());
logger.debug("pic length:" + length);
...
I am using eclipse on linux (eclipse doesn’t function too well so im working only on the console).
Now by following logger messages i deduce that atoi (look at the end of the code) causes the segmentation fault.
Which is really weird considering that another function testXMLcomm works fine and it looks similar.
i included stdio and stdlib ofcourse.
Please Help!!!
I expect the crash is in the line
logger.debug("pic length:" + length);. You’ve added an integer offsetlengthto a pointer-to-string-literal, and come up with an address in no-man’s land.You probably wanted to write something like:
Or
logger.debug("pic length: " + boost::lexical_cast<string>(length));, or C++11 hasstd::to_string(length).