public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/113852] New: -Wsign-compare doesn't warn on unsigned result types
@ 2024-02-09 16:10 admin at computerquip dot com
  2024-02-09 16:15 ` [Bug c++/113852] " admin at computerquip dot com
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: admin at computerquip dot com @ 2024-02-09 16:10 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113852
           Summary: -Wsign-compare doesn't warn on unsigned result types
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: admin at computerquip dot com
  Target Milestone: ---

I haven't quite figured out the pattern here so the title may not be great.
Some code may help explain better: https://godbolt.org/z/d8cqd1WqP

```
#include <iostream>
#include <limits>
#include <cinttypes>

int main()
{
    uint16_t a1 = std::numeric_limits<unsigned short>::max();
    uint16_t a2 = std::numeric_limits<unsigned short>::max();

    /* a1 * a2 should be 4294836225 in math terms */
    uint64_t a3 = 4294836225;

    /*
     * The result of (a1 * a2) is of type int and the result is negative.
     * (a1 * a2) ends up as some bogus high number because the common
     * type here ends up as uint64_t and sign-extension occurs.
     */
    if ((a1 * a2) > a3) {
        std::cout << "this will print\n";
    }
}
```

Some observations I've noticed:
* If either a1 or a2 are constexpr, the warning will occur.
* This used to warn up until 8.1.
* Clang also doesn't warn here but MSVC will with /W3 or higher.
* This seems like a slight variation of
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101645

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

end of thread, other threads:[~2024-02-13  8:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-09 16:10 [Bug c++/113852] New: -Wsign-compare doesn't warn on unsigned result types admin at computerquip dot com
2024-02-09 16:15 ` [Bug c++/113852] " admin at computerquip dot com
2024-02-09 16:38 ` redi at gcc dot gnu.org
2024-02-09 16:40 ` redi at gcc dot gnu.org
2024-02-09 16:42 ` redi at gcc dot gnu.org
2024-02-09 16:43 ` redi at gcc dot gnu.org
2024-02-12  8:31 ` rguenth at gcc dot gnu.org
2024-02-12 16:36 ` admin at computerquip dot com
2024-02-13  8:30 ` rguenther at suse dot de

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).