https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111415
stephematician <stephematician.acct at proton dot me> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |stephematician.acct@proton. | |me --- Comment #3 from stephematician <stephematician.acct at proton dot me> --- I encountered what I believe is the same bug while compiling an R package for CRAN. I reduced the code to the following example which produces errors on 12.4, 13.1-13.3, 14.1-14.2. An array-bounds warning occurs with -O1, -O2, and -O3, but not with -Os. Compiled with flags: -fpic -std=c++20 -Wall #include <cstddef> #include <vector> inline std::vector<double> adjust_pvalues(const std::vector<double> & unadjusted) { const size_t n_pvalue = unadjusted.size(); if (n_pvalue < 2) return unadjusted; /* Include this line and -O1 thru to -O3 give warnings */ const std::vector<size_t> index(n_pvalue); /* NOTE: Can eliminate warning by removing second argument */ std::vector<double> adjusted(n_pvalue, 0.0); return adjusted; } int main(int argc, char ** argv) { std::vector<double> x(2); x = adjust_pvalues(x); return 0; } The above snippet is currently available on godbolt: https://godbolt.org/z/38nnaEcrf