Core/ChatCommands: Do not parse partial strings for numeric paramters (PR #25259)

Check if integral/floating point type arguments were parsed successfully.

std::stoull will happily parse floating point strings until the decimal separator and return the value.
Make sure for all parsing methods that we actually parsed the whole token.

This allows to use handler arguments like Variant<uint32, float> which will be populated with the right type
depending on the token value (e.g "10" vs "10.0").
This commit is contained in:
Peter Keresztes Schmidt
2020-08-16 21:32:31 +02:00
committed by GitHub
parent ca25e8d019
commit 7edad0d601

View File

@@ -51,7 +51,13 @@ struct ArgInfo<T, std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>>
{
char const* next = args;
std::string token(args, tokenize(next));
try { val = std::stoll(token); }
try
{
size_t processedChars = 0;
val = std::stoll(token, &processedChars, 0);
if (processedChars != token.length())
return nullptr;
}
catch (...) { return nullptr; }
return next;
}
@@ -65,7 +71,13 @@ struct ArgInfo<T, std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T
{
char const* next = args;
std::string token(args, tokenize(next));
try { val = std::stoull(token); }
try
{
size_t processedChars = 0;
val = std::stoull(token, &processedChars, 0);
if (processedChars != token.length())
return nullptr;
}
catch (...) { return nullptr; }
return next;
}
@@ -79,7 +91,13 @@ struct ArgInfo<T, std::enable_if_t<std::is_floating_point_v<T>>>
{
char const* next = args;
std::string token(args, tokenize(next));
try { val = std::stold(token); }
try
{
size_t processedChars = 0;
val = std::stold(token, &processedChars);
if (processedChars != token.length())
return nullptr;
}
catch (...) { return nullptr; }
return std::isfinite(val) ? next : nullptr;
}