Return error when full string cannot be parsed into int

This commit is contained in:
Sahil Jain
2016-08-25 23:37:30 -04:00
parent d05d114523
commit b6ba322a04
2 changed files with 16 additions and 11 deletions

View File

@@ -219,7 +219,7 @@ CheckedError Parser::ParseHexNum(int nibbles, int64_t *val) {
return Error("escape code must be followed by " + NumToString(nibbles) +
" hex digits");
std::string target(cursor_, cursor_ + nibbles);
*val = StringToUInt(target.c_str(), 16);
*val = StringToUInt(target.c_str(), nullptr, 16);
cursor_ += nibbles;
return NoError();
}
@@ -447,7 +447,7 @@ CheckedError Parser::Next() {
cursor_++;
while (isxdigit(static_cast<unsigned char>(*cursor_))) cursor_++;
attribute_.append(start + 2, cursor_);
attribute_ = NumToString(StringToUInt(attribute_.c_str(), 16));
attribute_ = NumToString(StringToUInt(attribute_.c_str(), nullptr, 16));
token_ = kTokenIntegerConstant;
return NoError();
}
@@ -1093,10 +1093,15 @@ CheckedError Parser::ParseSingleValue(Value &e) {
NEXT();
} else { // Numeric constant in string.
if (IsInteger(e.type.base_type)) {
// TODO(wvo): do we want to check for garbage after the number?
e.constant = NumToString(StringToInt(attribute_.c_str()));
char *end;
e.constant = NumToString(StringToInt(attribute_.c_str(), &end));
if (*end)
return Error("invalid integer: " + attribute_);
} else if (IsFloat(e.type.base_type)) {
e.constant = NumToString(strtod(attribute_.c_str(), nullptr));
char *end;
e.constant = NumToString(strtod(attribute_.c_str(), &end));
if (*end)
return Error("invalid float: " + attribute_);
} else {
assert(0); // Shouldn't happen, we covered all types.
e.constant = "0";