-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed variable type in npc.cpp #3996
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with the expected value uint32_t
we can't use int32_t
since we would lose half of the available numbers, so we should use int64_t
in this case
Or stop passing -1 |
🤔
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Shouldn't we use
and
|
msg.add<uint32_t>(item.buyPrice == std::numeric_limits<uint32_t>::max() ? 0 : item.buyPrice); | ||
msg.add<uint32_t>(item.sellPrice == std::numeric_limits<uint32_t>::max() ? 0 : item.sellPrice); | ||
msg.add<uint32_t>(std::max<uint32_t>(item.buyPrice, 0)); | ||
msg.add<uint32_t>(std::max<uint32_t>(item.sellPrice, 0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not work as you think it does. This first converts both values to the template type (uint32_t). Since the buyPrice/sellPrice can be -1, this will just overflow and return that value instead of 0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not work as you think it does. This first converts both values to the template type (uint32_t). Since the buyPrice/sellPrice can be -1, this will just overflow and return that value instead of 0.
Confirmed!
I guess a more explicit check should be done since buyPrice and sellPrice are of type int64_t
A possible solution:
uint32_t value = std::max<int64_t>(std::min<int64_t>(number, std::numeric_limits<uint32_t>::max()), 0);
Pull Request Prelude
Changes Proposed
Changed from uint to int because we pass -1 at buy/sell
Issues addressed:
Closes #3980