mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-04 12:43:24 +00:00
Return error when full string cannot be parsed into int
This commit is contained in:
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user