cplusplus / CWG

Core Working Group
23 stars 7 forks source link

CWG2912 [expr.new] Array new cannot throw `std::bad_array_new_length` if the expression overflows from being too large #560

Open MitalAshok opened 2 weeks ago

MitalAshok commented 2 weeks ago

Full name of submitter: Mital Ashok

Reference (section label): [expr.new]

Issue description:

Consider a 32-bit size_t. Then the new-expression new int[0x1'0000'0000LL] converts the size expression implicitly to std::size_t, which becomes 0, where 0 would not exceed size limits. If it were new int[-0x1'0000'0000LL] (which would convert to 1), it would become invalid via [expr.new]p(8.1).

Suggested resolution:

Modify [expr.new]p8:

  • the expression is of non-class type and its value before converting to std​::​size_t is less than zero fits into std::size_t;
  • the expression is of class type and its value before application of the second standard conversion is less than zero fits into std::size_t;

This should not be too burdensome to compiler vendors since they already check if the number is greater than their limit for object size. It can be implemented by moving the comparison before the conversion to size_t.

jensmaurer commented 1 week ago

CWG2912