tinyxml2 icon indicating copy to clipboard operation
tinyxml2 copied to clipboard

Problem with string->int convert

Open TanahLot opened this issue 8 years ago • 0 comments

When we call IntAttribute method, this method read text attribute, and convert it to int via TIXML_SSCANF( str, "%d", value ). It work well if attribute actually contain integer value. But if attribute contain "1234abcdef"... sscanf read "1234" and simple ignore "abcdef" part. So, tinyxml2 treat non integer attribute as correct integer attribute. Simple test:

    tinyxml2::XMLDocument document;
    document.Parse("<root><node value=\"1234abcd\"/></root>");
    auto test=document.RootElement()->FirstChildElement("node");
    std::cout<<test->IntAttribute("value")<<std::endl;

Show "1234", but should show 0. My simple patch:

bool XMLUtil::ToInt( const char* str, int* value )
{
    char*endPtr;
    errno=0;//because it's not define if strtol reset errno or not
    long newValue=strtol(str,&endPtr,0);
    if(*endPtr || errno)
        return false;
    else
    {
        *value=newValue;
        return true;
    }
}

This patch not only allow filter "1234abcd" attributes, but also allow treat "0x12AB" attributes as integer attributes. Yes, I'm need this when I'm edit xml file in Notepad and need use RGB color codes. PS Sorry if my English is bad.

TanahLot avatar Jan 19 '17 10:01 TanahLot