Bug 2216750

Summary: div-by-zero warning wrongly emitted by g++
Product: Red Hat Developer Toolset Reporter: Paulo Andrade <pandrade>
Component: gccAssignee: Marek Polacek <mpolacek>
gcc sub component: gcc-toolset-12 QA Contact: Martin Cermak <mcermak>
Status: CLOSED UPSTREAM Docs Contact:
Severity: medium    
Priority: unspecified CC: mpolacek
Version: DTS 12.0 RHEL 7   
Target Milestone: alpha   
Target Release: 9.1   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2023-06-28 18:07:30 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

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.