Bug 2216750 - div-by-zero warning wrongly emitted by g++
Summary: div-by-zero warning wrongly emitted by g++
Keywords:
Status: CLOSED UPSTREAM
Alias: None
Product: Red Hat Developer Toolset
Classification: Red Hat
Component: gcc
Version: DTS 12.0 RHEL 7
Hardware: All
OS: Linux
unspecified
medium
Target Milestone: alpha
: 9.1
Assignee: Marek Polacek
QA Contact: Martin Cermak
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2023-06-22 12:49 UTC by Paulo Andrade
Modified: 2023-06-28 18:07 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2023-06-28 18:07:30 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)


Links
System ID Private Priority Status Summary Last Updated
GNU Compiler Collection 110464 0 P3 UNCONFIRMED Improve -Wdiv-by-zero 2023-06-28 18:07:30 UTC
Red Hat Issue Tracker DTS-604 0 None None None 2023-06-22 12:52:50 UTC

Description Paulo Andrade 2023-06-22 12:49:37 UTC
Sample reproducer:

"""
#include <vector>

int main()
{
    std::vector<int> bits(128, 1);
    const std::size_t errorCount = 0;
    // constexpr std::size_t errorCount = 0;
    if (errorCount != 0)
    // if constexpr (errorCount != 0)
    {
        for (std::size_t i = 0; i < bits.size(); i += bits.size() / errorCount)
        {
            bits[i] = 0;
        }
    }
}
"""

Will error with -Wall -Wextra -Werror. Issue happens with 4.1.2 and newer.

""""
<source>: In function 'int main()':
<source>:9:67: error: division by zero [-Werror=div-by-zero]
    9 |         for (std::size_t i = 0; i < bits.size(); i += bits.size() / errorCount)
        |                                                       ~~~~~~~~~~~~^~~~~~~~~~~~
cc1plus: all warnings being treated as errors
Compiler returned: 1
"""

Comment 2 Marek Polacek 2023-06-28 18:07:30 UTC
I've opened an upstream PR for it: https://gcc.gnu.org/PR110464
Clang handles it correctly, so perhaps we should too.


Note You need to log in before you can comment on or make changes to this bug.