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").

(cherry picked from commit 7edad0d601a7ae925cba850c5a23019f99be2a1e)
This commit is contained in:
Peter Keresztes Schmidt
2022-01-26 22:30:13 +01:00
committed by Shauren
parent f2ee365da4
commit d33214c50a
@@ -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;
}