Repository navigation
[mypyc] Fix C types of constant operands in native int shifts and arithmetic - #22557
Open
Jahongir-Qurbonov wants to merge 2 commits into
Open
Jahongir-Qurbonov wants to merge 2 commits into
Jahongir-Qurbonov wants to merge 2 commits into
Conversation
…thmetic A C integer literal has type int, long long or unsigned long long, depending on its value. Constant operands of native int ops were emitted as plain literals, so in a few cases the operation used the wrong C type: * A shift with a constant left operand used int. For example, 3 << n as u64 gave 0 for n = 40, and 1000000 >> n as i64 or u64 shifted an int by a count of up to 63, which is undefined behavior. * A u32 addition, subtraction or multiplication by a constant that is at least 2**31 used long long (e.g. 4000000000LL), so a multiplication could overflow long long, which is undefined behavior. Convert a constant operand to the C type of the result in these cases.
Collaborator
|
Thanks! This looks good, but there's a merge conflict. |
…literal-operands # Conflicts: # mypyc/test-data/run-i64.test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A C integer literal has type
int,long longorunsigned long long, depending on its value. Constant operands of native int ops were emitted as plain literals, so in a few cases the operation was performed using the wrong C type:int. For example,3 << nasu64emitted3 << cpy_r_n, so3 << 40gave 0 (or 6 with-O0) instead of 3298534883328, and3 << 31asu32overflowedint. A right shift such as1000000 >> nasi64oru64shifted anintby a count of up to 63, which is undefined behavior (with-O0,1000000 >> 33gave 500000 instead of 0).u32addition, subtraction or multiplication by a constant that is at least 2**31 usedlong long, since the constant is emitted as e.g.4000000000LL.x * 4000000000could then overflowlong long, which is undefined behavior. With the default CFLAGS (-fno-strict-overflow) the result was correct.Convert a constant operand to the C type of the result in these cases. Other ops already cast their operands (signed arithmetic and signed left shifts use an unsigned type since #22547 and #22556), or the C type of a constant doesn't matter.
I checked the generated C expressions for the examples above with UBSan (
-fsanitize=undefined), which reports the shifts and the multiplication before this change. The new run tests fail without the fix (theu64andi64shift tests, with the default-O0used by the tests).