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

            Bug ID: 121482
           Summary: Unexpected below array bounds Warning
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 1600277910 at qq dot com
  Target Milestone: ---

My GCC version:

g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/14/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 14.2.0-19'
--with-bugurl=file:///usr/share/doc/gcc-14/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2,rust --prefix=/usr
--with-gcc-major-version-only --program-suffix=-14
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/reproducible-path/gcc-14-14.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/reproducible-path/gcc-14-14.2.0/debian/tmp-gcn/usr
--enable-offload-defaulted --without-cuda-driver --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-serialization=3
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.0 (Debian 14.2.0-19)


lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 13 (trixie)
Release:        13
Codename:       trixie


My code:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL mod;
struct nd{
    int l,r;
    LL tag,taggg,val;
}tree[800005];
int n;
LL arr[200005];
int ls(int p){return (p<<1);}
int rs(int p){return ((p<<1)|1);}
void add(int p,LL mul,LL add){
    tree[p].val=(tree[p].val*mul+add*(tree[p].r-tree[p].l+1))%mod;
    tree[p].tag=(tree[p].tag*mul)%mod;
    tree[p].taggg=(tree[p].taggg*mul+add)%mod;
    return;
}
void pushdown(int p){
    if ((tree[p].tag^1)||tree[p].taggg){
        add(ls(p),tree[p].tag,tree[p].taggg);
        add(rs(p),tree[p].tag,tree[p].taggg);
        tree[p].tag=1;
        tree[p].taggg=0;
    }
    return;
}
void pushup(int p){
    tree[p].val=(tree[ls(p)].val+tree[rs(p)].val)%mod;
    return;
}
void build(int p,int l,int r){
    tree[p]={l,r,1,0,0};
    if (l==r){tree[p].val=arr[l];return;}
    int mid=(l+r)>>1;
    build(ls(p),l,mid);build(rs(p),mid+1,r);
    pushup(p);
    return;
}
void update(int p,int l,int r,LL val,LL val2){
    if (l<=tree[p].l&&tree[p].r<=r){add(p,val,val2);return;}
    int mid=(tree[p].l+tree[p].r)>>1;
    pushdown(p);
    if (l<=mid){update(ls(p),l,r,val,val2);}
    if (r>mid){update(rs(p),l,r,val,val2);}
    pushup(p);
    return;
}
LL query(int p,int l,int r){
    if (l>tree[p].r||r<tree[p].l){return 0;}
    if (l<=tree[p].l&&tree[p].r<=r){return tree[p].val;}
    int mid=(tree[p].l+tree[p].r)>>1;
    pushdown(p);
    LL res=0;
    if (l<=mid){(res+=query(ls(p),l,r))%=mod;}
    if (r>mid){(res+=query(rs(p),l,r))%=mod;}
    return res;
}
int main(){
    int T,l,r;LL x,y;char op;
    scanf("%d%lld",&n,&mod);
    for (int i=1;i<=n;++i){scanf("%lld",arr+i);arr[i]%=mod;}
    build(1,1,n);
    scanf("%d",&T);++T;while (--T){
        scanf("%hhd%d%d",&op,&l,&r);
        if (op^3){scanf("%lld",&x);x%=mod;y=0;if
(op&2){swap(x,y);x=1;}update(1,l,r,x,y);continue;}
        printf("%lld\n",query(1,l,r));
    }
    return 0;
}

Using this command on Debian 13 Trixie:

g++ -m64 -DOFFLINE_JUDGE -lm -O2 -std=c++14 -static -Wall -Wextra
-fsanitize=undefined -o main main.cpp -g
main.cpp: In function ‘void pushdown(int)’:

g++ outputs:

main.cpp:22:37: warning: array subscript -1 is below array bounds of ‘nd
[800005]’ [-Warray-bounds=]
   22 |         add(rs(p),tree[p].tag,tree[p].taggg);
      |                               ~~~~~~^
main.cpp:8:2: note: while referencing ‘tree’
    8 | }tree[800005];
      |  ^~~~
main.cpp:22:25: warning: array subscript -1 is below array bounds of ‘nd
[800005]’ [-Warray-bounds=]
   22 |         add(rs(p),tree[p].tag,tree[p].taggg);
      |                   ~~~~~~^
main.cpp:8:2: note: while referencing ‘tree’
    8 | }tree[800005];


Old GNU GCC(g++-11) has no warning outputs

The warning output is unexpected.

Reply via email to