Bug 35729 - Неправильная работа сравнения int
Summary: Неправильная работа сравнения int
Status: CLOSED NOTABUG
Alias: None
Product: Sisyphus
Classification: Development
Component: gcc8 (show other bugs)
Version: unstable
Hardware: all Linux
: P3 normal
Assignee: Gleb F-Malinovskiy
QA Contact: qa-sisyphus
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-12-05 14:11 MSK by Fr. Br. George
Modified: 2018-12-15 04:19 MSK (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fr. Br. George 2018-12-05 14:11:44 MSK
$ gcc-8 -x c - <<< 'void main() { int x=0x80000000; printf("%d,%d,%d,%d\n",x,-x,(x-1),x<(x-1)); }'
$ ./a.out
-2147483648,-2147483648,2147483647,0

Должно быть 
-2147483648,-2147483648,2147483647,1
Comment 1 Anton Farygin 2018-12-05 14:17:22 MSK
На x86_64 это тоже вылезает.

$  gcc-8 -x c - <<< 'void main() { int x=0x80000000;
> printf("%d,%d,%d,%d\n",x,-x,(x-1),x<(x-1)); }'
<stdin>: In function ‘main’:
<stdin>:2:1: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
<stdin>:2:1: warning: incompatible implicit declaration of built-in function ‘printf’
<stdin>:2:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’

$ ./a.out 
-2147483648,-2147483648,2147483647,0


$  gcc-7 -x c - <<< 'void main() { int x=0x80000000;
printf("%d,%d,%d,%d\n",x,-x,(x-1),x<(x-1)); }'
<stdin>: In function ‘main’:
<stdin>:2:1: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
<stdin>:2:1: warning: incompatible implicit declaration of built-in function ‘printf’
<stdin>:2:1: note: include ‘<stdio.h>’ or provide a declaration of ‘printf’
$ ./a.out 
-2147483648,-2147483648,2147483647,1
Comment 2 Gleb F-Malinovskiy 2018-12-05 16:30:25 MSK
Не похоже чтобы это от архитектуры зависело -- на aarch64 и ppc64le результат тот же.
Comment 3 Dmitry V. Levin 2018-12-05 16:40:56 MSK
А это точно не ub?
Comment 4 Dmitry V. Levin 2018-12-05 16:52:01 MSK
сравните с -fno-strict-overflow :)
Comment 5 Fr. Br. George 2018-12-05 17:26:18 MSK
(В ответ на комментарий №4)
> сравните с -fno-strict-overflow :)

Скорее с -fwrapv
Вот бага в Питоне соответствующая, закрыта как not a bug:
https://bugs.python.org/issue34096
Comment 6 Dmitry V. Levin 2018-12-05 18:06:51 MSK
(In reply to comment #5)
> (В ответ на комментарий №4)
> > сравните с -fno-strict-overflow :)
> 
> Скорее с -fwrapv

Между -fno-strict-overflow и -fwrapv есть очень тесная связь. :)

Вопрос в том, какое значение по умолчанию мы хотим.
Comment 7 Fr. Br. George 2018-12-05 18:29:27 MSK
Конкретно при сборке Python эта опция выставляется явно, но в нашем пакете она перебивалась, наша вина.
Comment 8 Dmitry V. Levin 2018-12-15 04:19:54 MSK
Это изменение в gcc было документировано,
см. https://gcc.gnu.org/gcc-8/changes.html:

-fno-strict-overflow is now mapped to -fwrapv -fwrapv-pointer and signed integer overflow is now undefined by default at all optimization levels.

Если нас не устраивает новое документированное поведение gcc, можно обсудить, что с этим делать, но не здесь.