Skip to content

Commit

Permalink
Core/ChatCommands: Do not parse partial strings for numeric paramters…
Browse files Browse the repository at this point in the history
… (PR TrinityCore#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 7edad0d)
  • Loading branch information
Carbenium authored and Shauren committed Jan 26, 2022
1 parent f2ee365 commit d33214c
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/server/game/Chat/ChatCommands/ChatCommandArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit d33214c

Please sign in to comment.