Add overflow check for array size (#3194)

Closes #3193
This commit is contained in:
Geoff Romer
2023-09-06 18:51:54 +00:00
committed by GitHub
parent cce013b43e
commit 6f02efe1d3
2 changed files with 49 additions and 1 deletions
+1 -1
View File
@@ -37,7 +37,7 @@ auto HandleArrayExpression(Context& context, Parse::Node parse_node) -> bool {
if (bound_node.kind() == SemIR::NodeKind::IntegerLiteral) {
auto bound_value = context.semantics_ir().GetIntegerLiteral(
bound_node.GetAsIntegerLiteral());
if (!bound_value.isNegative()) {
if (!bound_value.isNegative() && bound_value.getBitWidth() <= 64) {
context.AddNodeAndPush(
parse_node,
SemIR::Node::ArrayType::Make(
@@ -0,0 +1,48 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// AUTOUPDATE
// CHECK:STDERR: fail_bound_overflow.carbon:[[@LINE+3]]:32: Invalid array expression.
// CHECK:STDERR: var a: [1; 39999999999999999993];
// CHECK:STDERR: ^
var a: [1; 39999999999999999993];
// CHECK:STDOUT: cross_reference_irs_size: 1
// CHECK:STDOUT: functions: [
// CHECK:STDOUT: ]
// CHECK:STDOUT: integer_literals: [
// CHECK:STDOUT: 1,
// CHECK:STDOUT: 39999999999999999993,
// CHECK:STDOUT: ]
// CHECK:STDOUT: real_literals: [
// CHECK:STDOUT: ]
// CHECK:STDOUT: strings: [
// CHECK:STDOUT: a,
// CHECK:STDOUT: ]
// CHECK:STDOUT: types: [
// CHECK:STDOUT: nodeIntegerType,
// CHECK:STDOUT: ]
// CHECK:STDOUT: type_blocks: [
// CHECK:STDOUT: ]
// CHECK:STDOUT: nodes: [
// CHECK:STDOUT: {kind: IntegerLiteral, arg0: int0, type: type0},
// CHECK:STDOUT: {kind: IntegerLiteral, arg0: int1, type: type0},
// CHECK:STDOUT: {kind: VarStorage, arg0: str0, type: typeError},
// CHECK:STDOUT: ]
// CHECK:STDOUT: node_blocks: [
// CHECK:STDOUT: [
// CHECK:STDOUT: ],
// CHECK:STDOUT: [
// CHECK:STDOUT: node+0,
// CHECK:STDOUT: node+1,
// CHECK:STDOUT: node+2,
// CHECK:STDOUT: ],
// CHECK:STDOUT: ]
// CHECK:STDOUT:
// CHECK:STDOUT: package {
// CHECK:STDOUT: %.loc10_9: i32 = int_literal 1
// CHECK:STDOUT: %.loc10_12: i32 = int_literal 39999999999999999993
// CHECK:STDOUT: %a: ref <error> = var "a"
// CHECK:STDOUT: }