Modulus operator ignores sign when right-hand-side is const

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.

Post a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: Modulus operator ignores sign when right-hand-side is const

Re: Modulus operator ignores sign when right-hand-side is co

by Graf Zahl » Wed Aug 03, 2022 2:36 am

I'm not sure how much of a problem this may become - but I had to make more changes than I would have liked to compile the internal scripts with this change. One thing that was necessary to add is a warning for signed/unsigned comparisons because they may behave in very unintuitive ways if negative values get in. For that reason I had to change the type of Array::Size to signed, because this flooded the console with warnings and in a few times produced logic errors where checks with -1 were done.

Re: Modulus operator ignores sign when right-hand-side is co

by Graf Zahl » Wed Aug 03, 2022 12:47 am

Done. In the process I discovered another bug, i.e. that constant evaluation never set the engine version and essentially produced undefined behavior when entering version dependent code.

Re: Modulus operator ignores sign when right-hand-side is co

by randi » Tue Aug 02, 2022 5:18 pm

I was worried this might be a risky change after all this time. How widespread is the problem of code using uints in a way where they rely on them being downgraded to ints? It might be best to make this version dependent.

Re: Modulus operator ignores sign when right-hand-side is co

by Nash » Tue Aug 02, 2022 4:17 pm

Looks like these fixes broke the INIFile library - I'm too tired to look into this properly at the moment but I do know that library uses a lot of unsigned ints in its ZScript code. I've at least found which functions fail, have documented it in the other thread.

Re: Modulus operator ignores sign when right-hand-side is co

by randi » Mon Aug 01, 2022 10:22 pm

Re: Modulus operator ignores sign when right-hand-side is co

by Graf Zahl » Sun Oct 03, 2021 2:30 am

Hm. I fear there isn't much I can do now - the problem is so deep in the parser, filtering up several levels from there causing follow-up issues with insufficient type checks, that trying to change it will surely break more things than fix.

The biggest obstacle is that from the token scanner up to the evaluation stage it uses 32 bit constants, but screws up the constant type at the very lowest level already. And it's not just ZScript depending on this but all parsing code in the entire engine.

This entire code will have to be redesigned for 64 bit throughout to avoid these problems, but that's a major task of refactoring.

Modulus operator ignores sign when right-hand-side is const

by Jay0 » Thu Sep 30, 2021 12:11 pm

The Modulus operator in zscript (%) ignores the sign of the left-hand-side operand when the right-hand-side operand is a constant, performing a signed modulus operation even when it should perform an unsigned one.

Example: <uint larger than INT_MAX>%constant will be negative, while <uint larger than INT_MAX>%<another uint> will not

Top