Skip to content

Bugfix examples/uint63z/prims.c#5

Open
eladrion wants to merge 1 commit intoCertiCoq:mainfrom
eladrion:uint63z_fix
Open

Bugfix examples/uint63z/prims.c#5
eladrion wants to merge 1 commit intoCertiCoq:mainfrom
eladrion:uint63z_fix

Conversation

@eladrion
Copy link

…gfixes)

Here, the sub-expression "(1 << i) is interpreted as shifting a signed integer by the unsigned integer value i, according to the ISO/IEC C standard [11, §6.4.4.1]. But when the size of value is 8 byte, i.e. 64 bits, which is the default case, the expression “unsigned int i = sizeof(value) * 8 - 1” evaluates to “unsigned int i = 63”. Thus, in the first iteration of the loop, the integer value 1 (which has 32 bits) is shifted left by 63 bits which is a shift over- flow. However, no error is provoked when compiling the file with the GCC for user space with the proposed compilation flags. But when adding the compiler flag -fsanitize=shift which adds runtime instrumentation for recognising shift overflows and is used by default for compiling Kernel modules, a runtime error (shift-out-of-bounds) is generated. The solution is here to add the suffix “ULL” to the integer constant 1.

…gfixes)

Here, the sub-expression "(1 << i) is interpreted as shifting a signed integer by the unsigned integer value i, according to the ISO/IEC C standard [11, §6.4.4.1]. But when the size of value is 8 byte, i.e. 64 bits, which is the default case, the expression “unsigned int i = sizeof(value) * 8 - 1” evaluates to “unsigned int i = 63”. Thus, in the first iteration of the loop, the integer value 1 (which has 32 bits) is shifted left by 63 bits which is a shift over-
flow. However, no error is provoked when compiling the file with the GCC for user space with the proposed compilation flags. But when adding the compiler flag -fsanitize=shift which adds runtime instrumentation for recognising shift overflows and is used by default for compiling Kernel modules, a runtime error (shift-out-of-bounds) is generated. The solution is here to add the suffix “ULL” to the integer constant 1.

[11]: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant