http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56336
Bug #: 56336
Summary: Buggy implementation of stoi, stol, stoll
Classification: Unclassified
Product: gcc
Version: 4.7.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
AssignedTo: [email protected]
ReportedBy: [email protected]
The following code-snippet should throw std::invalid_argument exception as per
the Standard, but they don't, instead they print `8978`:
//demo : http://stacked-crooked.com/view?id=4853f6694dc11f93ed9a687567e5166d
int main()
{
std::cout << std::stoi("8978xyz") << std::endl; //invalid argument
std::cout << std::stol("8978xyz") << std::endl; //invalid argument
std::cout << std::stoll("8978xyz") << std::endl; //invalid argument
}
The buggy part of the implementation is this:
//file : /4.7.2/include/c++/ext/string_conversions.h
//line : 60-61
//function : __stoa
if (__endptr == __str)
std::__throw_invalid_argument(__name);
Easy fix:
if (__endptr != (__str + std::strlen(__str)) )
std::__throw_invalid_argument(__name);
That should fix the bug. But it would be better to pass the length of the
string to the function. After all, `std::stoi` family takes the argument as
`std::string` which returns the size in O(1).