https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121529
Bug ID: 121529
Summary: std::valarray fails size assertion when using multiple
chained operator+ calls
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: 2023091106 at cauc dot edu.cn
Target Milestone: ---
Product / Version:
GCC trunk / 15.0 / 16.0.0
g++ -std=c++20 -O0 test.cpp -o test
GCC 15/16 added length checks for `std::valarray`, but the checks only apply to
two operands at a time, allowing multiple chained operators to bypass the
length check.see as https://godbolt.org/z/3ezEnT3zb
========== code ==========
#include <valarray>
#include <iostream>
using namespace std;
int main() {
typedef std::valarray<int> A;
A a(3, 2), b(3, 2), e(1, 3);
// This line does not trigger assertion failure
A c = a + b + e;
// This line triggers assertion failure
// A c = a + e;
A d = c;
printf("Result array size: %zu\n", d.size());
return 0;
}
========== output ==========
when A c = a + b + e is executed, the program is normal;
When a + e is executed, the program crashes:
/opt/compiler-explorer/gcc-trunk-20250813/include/c++/16.0.0/valarray:1198:
std::_Expr<std::__detail::_BinClos<std::__plus, std::_ValArray, std::_ValArray,
_Tp, _Tp>, typename std::__fun<std::__plus, _Tp>::result_type>
std::operator+(const valarray<_Tp>&, const valarray<_Tp>&) [with _Tp = int;
typename __fun<std::__plus, _Tp>::result_type = int]: Assertion '__v.size() ==
__w.size()' failed.
Program terminated with signal: SIGSEGV