- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
#include <stdio.h>;
void check(int x, int y) {
if (2*x == y && y < 0 && 0 <= 2*x) {
puts("Impossible!");
}
}
int main() {
check(0x7F80007F, 0xFF0000FE);
}
Нашли или выдавили из себя код, который нельзя назвать нормальным, на который без улыбки не взглянешь? Не торопитесь его удалять или рефакторить, — запостите его на говнокод.ру, посмеёмся вместе!
0
#include <stdio.h>;
void check(int x, int y) {
if (2*x == y && y < 0 && 0 <= 2*x) {
puts("Impossible!");
}
}
int main() {
check(0x7F80007F, 0xFF0000FE);
}
https://runtimeverification.com/blog/?p=257
When writing code for a specific compiler you can rely on the implementation-specified behavior, but signed overflow is still problematic. GCC promises that conversions between integer types will be reduced modulo the appropriate power of two when the value is not representable in the target type. This means that with GCC the conversion above will initialize var to -0x112234 on any architecture that GCC supports. However, only initialization and other conversions are safe. GCC still considers signed overflow in arithmetic as undefined behavior, and optimizes under the assumption that there will be no overflow. This can lead to apparently impossible results when signed values do overflow. Compiled with -O3, this program prints “Impossible!”.
By adding apparently-redundant casts to 2*x to give (int)(2*(unsigned int)x), the calculation becomes implementation-specified behavior from an out-of-range conversion instead of undefined behavior. While this code may not be portable between compilers, GCC now guarantees the “impossible” code will not be executed even with -O3.
j123123 05.07.2017 02:58 # 0
Bobik 05.07.2017 12:37 # 0