Integer overflow issue in script

Started by
6 comments, last by WitchLord 3 weeks, 5 days ago

I've recently been looking into AngelScript for use in an upcoming project. I have found what appears to be an integer overflow issue during certain math operation in the script. Consider the following:
uint64 mynum;
mynum = 2147483647 + 1;
print("mynum = " + mynum);

This results in the ouput:
mynum = 18446744071562067968
If I set mynum with a simple assignment mynum = 2147483648;, the behaviour is what I would expect:
mynum = 2147483648

The issue appears to happen when only literal numbers are used in a math operation and they are interpreted as int32 regardless of the type being assigned. If a uint64 variable is part of the math or at least one of the values is cast to uint64, then the problem goes away. Here are a few variants I tried:
mynum = uint64(2147483647) + 1; - Works

mynum = 0;
mynum = mynum + 2147483647 + 1; - Works

mynum = 0;
mynum += 2147483647 + 1; - Overflow

Didn't test it out, but floating point math may have a similar issue. While I can easily work around the overflow issue now that I know about it, I would argue that this should be considered a bug. The overflow condition is not obvious and I could see bizarre/hard to diagnose problems emerging if you do not write your script in a way to avoid the overflow.

Other than that, I do want to say that I'm really liking the language and I am finding a lot of things are easier to do than in other embeddable languages I've used. Thus far I've been able to locate code examples in the add_ons and samples source to get me over any difficulties I've encountered trying to understand the API. Overall, it's been real solid.



Advertisement

I don't know anything about AS, but the behavior you describe is exactly how C/C++ interprets integer literals without suffixes (no suffix → always “int” type), so I doubt it's a bug. In C++ you could add a suffix like “ull" (unsigned long long) to indicate to the compiler that the literal should be given a 64-bit type.

I get that, but at least the C/C++ compiler would throw a warning in the same case. The AS compiler does not seem to detect this, or I have not figured out how to make it if it does.

OK I feel a bit silly, I'm now able to see an AS compiler warning regarding the overflow. I wish I could say it was because I had my msg callback misconfigured, but really I was just outputting a ton of info and the warning got lost in the noise.

Thanks for the compliments on the library. I'm glad you like it.

Also, thanks for confirming that the compiler does indeed generate a warning in this case. It saved me some time to investigate it myself.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Are there suffixes you can use on literals to make them be the type you want, like the “ull” Aressera described above? I use them all the time in C++ and this avoid all of these issues.

No, for integers AngelScript doesn't have the suffixes yet. Maybe I'll add this in the future, but for now explicitly type casting with uint64(x) is the way to do it.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement