Skip to content
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

OverflowSafeInt may trigger bug in VC++ Express 2005 #1946

Closed
DorpsGek opened this issue Apr 23, 2008 · 6 comments
Closed

OverflowSafeInt may trigger bug in VC++ Express 2005 #1946

DorpsGek opened this issue Apr 23, 2008 · 6 comments
Labels
flyspray This issue is imported from FlySpray (https://bugs.openttd.org/)

Comments

@DorpsGek
Copy link
Member

PhilSophus opened the ticket and wrote:

Using operator+(byte a, OverflowSafeInt<...> b) (defined in src/core/overflowsafe_type.hpp:141) causes VC++ Express 2005 to complain about an ambiguous overload. This is only a latent problem so far, as apparently the existing code does not use this overload, but patches (as the attached one) show the problem. Most probably, the other operators around there have the same problem.

Possible fixes: Cast the byte operand to int (or uint) or provide an OverflowSafeInt<...>::operator+(byte) (and same for minus).

A detailed discussion on this problem starts here: http://www.tt-forums.net/viewtopic.php?p=684428# p684428

Attachments

Reported version: trunk
Operating system: All


This issue was imported from FlySpray: https://bugs.openttd.org/task/1946
@DorpsGek
Copy link
Member Author

Rubidium wrote:

That'd mean you're adding a byte to the money somewhere. When playing with loans I do not think you want to work with max 255 loans.


This comment was imported from FlySpray: https://bugs.openttd.org/task/1946#comment3962

@DorpsGek
Copy link
Member Author

PhilSophus wrote:

No, I was calculating the current interest rate. And the interest rates are expressed as byte.

The problematic expression was as follows:

return _economy.min_interest_rate + (int64)(_economy.max_interest_rate-_economy.min_interest_rate)*p->current_loan+p->max_loan/2)/p->max_loan;

So the types were:

byte + ((int64)(byte-byte)*Money + Money/int)/Money

So, the sum calculated in the last step was (byte + Money). Of course, the second summand is in the range [0,max_interest_rate-min_interest_rate] so clearly in the byte range.

So, I should probably cast the second summand to byte. The return type of the function is byte anyways.

Nevertheless, if a byte + Money is provided it should work on all supported compilers. This is not the first time, I have read complaints about this compiling problem in the forum. So, patch authors seem to be caught by this problem regularly (e.g.: http://www.tt-forums.net/viewtopic.php?f=33&t=36127&p=664589# p664589 )


This comment was imported from FlySpray: https://bugs.openttd.org/task/1946#comment3963

@DorpsGek
Copy link
Member Author

PhilSophus wrote:

Oops, I just noticed that the just cited occurrence was probably caused by my improved loans patch, too. This was as part of a patchpack, so I didn't notice the relation to my patch before.


This comment was imported from FlySpray: https://bugs.openttd.org/task/1946#comment3964

@DorpsGek
Copy link
Member Author

Rubidium wrote:

It is more by design that adding bytes isn't implemented. Primarily because adding bytes to Money isn't 'normal'. Furthermore your patch should (conceptually) cast the whole result of variable part of the interest calculation to a byte because the interest rate does not need to be 64 bits if you want it to be between 0 and maybe 20-50.


This comment was imported from FlySpray: https://bugs.openttd.org/task/1946#comment3975

@DorpsGek
Copy link
Member Author

PhilSophus wrote:

Well, in fact, it is implemented (you explicitly provide an operator+(byte,OverflowSafeInt<>) implicitly forwarding to OverflowSafeInt<>::operator+(int)). And it works correctly on standard-conforming compilers, so it really can't be by design that it doesn't work on a non-conforming (let's call it buggy) but otherwise supported compiler. Also, why you consider adding integral types of differing bit-length as so abnormal that it should be avoided by design escapes me. Obviously, the author of the code thought differently.

But, if inserting (otherwise harmless) casts to int in operator+(byte,OverflowSafeInt<>) and operator- to also make it work on a bit buggy but supported compiler is such a big deal, just leave it as it is. It's just ridiculous and a waste of time to discuss the abnormality of adding byte and Money any further.

And, as I said: "So, I should probably cast the second summand to byte. The return type of the function is byte anyways." I already did this in my local git repository.


This comment was imported from FlySpray: https://bugs.openttd.org/task/1946#comment3977

@DorpsGek
Copy link
Member Author

Rubidium closed the ticket.

Reason for closing: Fixed


This comment was imported from FlySpray: https://bugs.openttd.org/task/1946

@DorpsGek DorpsGek added Core flyspray This issue is imported from FlySpray (https://bugs.openttd.org/) labels Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flyspray This issue is imported from FlySpray (https://bugs.openttd.org/)
Projects
None yet
Development

No branches or pull requests

1 participant