Skip to content

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

Description

@MitalAshok

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions