public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95334] New: GCC detect missing signed-integer-overflow when add "const"
@ 2020-05-26 12:53 haoxintu at gmail dot com
  2020-05-26 13:08 ` [Bug c++/95334] " redi at gcc dot gnu.org
  2020-05-27  1:26 ` haoxintu at gmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: haoxintu at gmail dot com @ 2020-05-26 12:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95334

            Bug ID: 95334
           Summary: GCC detect missing signed-integer-overflow when add
                    "const"
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

This code test1.cc

#include <iostream>
#include <climits>

wchar_t g_wchar = 0;
int g_int = -1975564288;
int l_int = -1227761344;

int main () {
    g_wchar = l_int + g_int;
    std::cout << g_wchar << std::endl;
    std::cout << "WCHAR_MIN = " << WCHAR_MIN << std::endl;
    std::cout << "WCHAR_MAX = " << WCHAR_MAX << std::endl;
    return 0;
}

GCC can detect UB correctly

$g++ -w -fsanitize=signed-integer-overflow ; ./a.out
test1.cc:9:21: runtime error: signed integer overflow: -1227761344 +
-1975564288 cannot be represented in type 'int'
1091641664
WCHAR_MIN = -2147483648
WCHAR_MAX = 2147483647

But in this case test2.cc add "const" for int
#include <iostream>
#include <climits>

wchar_t g_wchar = 0;
const int g_int = -1975564288;
const int l_int = -1227761344;

int main () {
    g_wchar = l_int + g_int;
    std::cout << g_wchar << std::endl;
    std::cout << "WCHAR_MIN = " << WCHAR_MIN << std::endl;
    std::cout << "WCHAR_MAX = " << WCHAR_MAX << std::endl;
    return 0;
}

GCC detect nothing.

$g++ -w -fsanitize=signed-integer-overflow test2.cc; ./a.out
1091641664
WCHAR_MIN = -2147483648
WCHAR_MAX = 2147483647

My g++ version

$g++ --version
g++ (GCC) 11.0.0 20200526 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I have tested them in recent GCC versions including GCC-8, GCC-9, GCC-10, they
have the same symptom as well.

I also test them in clang-trunk, above two testcases can be detected as "signed
integer overflow".

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/95334] GCC detect missing signed-integer-overflow when add "const"
  2020-05-26 12:53 [Bug c++/95334] New: GCC detect missing signed-integer-overflow when add "const" haoxintu at gmail dot com
@ 2020-05-26 13:08 ` redi at gcc dot gnu.org
  2020-05-27  1:26 ` haoxintu at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2020-05-26 13:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95334

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
My guess is that G++ folds the constants early and the overflow happens at
compile time, so is never seen by UBsan.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/95334] GCC detect missing signed-integer-overflow when add "const"
  2020-05-26 12:53 [Bug c++/95334] New: GCC detect missing signed-integer-overflow when add "const" haoxintu at gmail dot com
  2020-05-26 13:08 ` [Bug c++/95334] " redi at gcc dot gnu.org
@ 2020-05-27  1:26 ` haoxintu at gmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: haoxintu at gmail dot com @ 2020-05-27  1:26 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95334

--- Comment #2 from Haoxin Tu <haoxintu at gmail dot com> ---
I also find these cases

test1.cc
#include <iostream>

bool g_bool = 0;
long g_long = -4075183478711827874L;
const long l_long = 7122990940771016367L;
int main () {
    g_bool = g_long * l_long;
    std::cout << g_bool << std::endl;
    return 0;
}

GCC detects nothing.

$g++ -w -fsanitize=signed-integer-overflow test1.cc
1

But when remove "const"
test2.cc
#include <iostream>

bool g_bool = 0;
long g_long = -4075183478711827874L;
const long l_long = 7122990940771016367L;
int main () {
    g_bool = g_long * l_long;
    std::cout << g_bool << std::endl;
    return 0;
}

GCC can dectect the signed-integer-overflow.

$g++ -w -fsanitize=signed-integer-overflow test2.cc
test2.cc:7:21: runtime error: signed integer overflow: -4075183478711827874 *
7122990940771016367 cannot be represented in type 'long int'
1

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-05-27  1:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-26 12:53 [Bug c++/95334] New: GCC detect missing signed-integer-overflow when add "const" haoxintu at gmail dot com
2020-05-26 13:08 ` [Bug c++/95334] " redi at gcc dot gnu.org
2020-05-27  1:26 ` haoxintu at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).