From 2ea7de8a17b759204f7b581a6f322610e9b1e139 Mon Sep 17 00:00:00 2001 From: pk19604014 <95385881+pk19604014@users.noreply.github.com> Date: Fri, 4 Mar 2022 14:45:50 -0500 Subject: [PATCH] Fixed a couple crashes surfaced by running a structured fuzzer on executable_semantics parse/type-check/execute logic. (#1106) * Fixed a couple simple crashes surfaced by running a structured fuzzer on executable_semantics parse/type-check/execute logic. * var a: b: auto; -> var a: auto; --- .../interpreter/type_checker.cpp | 4 ++++ executable_semantics/syntax/lexer.lpp | 6 ++++-- .../basic_syntax/fail_invalid_integer.carbon | 16 ++++++++++++++++ .../fail_invalid_var_expression.carbon | 16 ++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 executable_semantics/testdata/basic_syntax/fail_invalid_integer.carbon create mode 100644 executable_semantics/testdata/basic_syntax/fail_invalid_var_expression.carbon diff --git a/executable_semantics/interpreter/type_checker.cpp b/executable_semantics/interpreter/type_checker.cpp index 9a3681f7f282..5e43cc767236 100644 --- a/executable_semantics/interpreter/type_checker.cpp +++ b/executable_semantics/interpreter/type_checker.cpp @@ -1421,6 +1421,10 @@ void TypeChecker::DeclareDeclaration(Nonnull d, auto& var = cast(*d); // Associate the variable name with it's declared type in the // compile-time symbol table. + if (!llvm::isa(var.binding().type())) { + FATAL_COMPILATION_ERROR(var.binding().type().source_loc()) + << "Expected expression for variable type"; + } Expression& type = cast(var.binding().type()).expression(); TypeCheckPattern(&var.binding(), std::nullopt, impl_scope); diff --git a/executable_semantics/syntax/lexer.lpp b/executable_semantics/syntax/lexer.lpp index e282e77fe76e..462f1666d74b 100644 --- a/executable_semantics/syntax/lexer.lpp +++ b/executable_semantics/syntax/lexer.lpp @@ -266,8 +266,10 @@ string_literal \"([^\\\"\n\v\f\r]|\\.)*\" {integer_literal} { BEGIN(AFTER_OPERAND); - int val; - CHECK(llvm::to_integer(yytext, val)); + int val = 0; + if (!llvm::to_integer(yytext, val)) { + FATAL_SYNTAX_ERROR(context) << "Invalid integer literal: " << yytext; + } return ARG_TOKEN(integer_literal, val); } diff --git a/executable_semantics/testdata/basic_syntax/fail_invalid_integer.carbon b/executable_semantics/testdata/basic_syntax/fail_invalid_integer.carbon new file mode 100644 index 000000000000..0d2389a25a65 --- /dev/null +++ b/executable_semantics/testdata/basic_syntax/fail_invalid_integer.carbon @@ -0,0 +1,16 @@ +// 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 +// +// RUN: %{not} %{executable_semantics} %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s +// RUN: %{not} %{executable_semantics} --trace %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: %{executable_semantics} %s +// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/basic_syntax/fail_invalid_integer.carbon:15: Invalid integer literal: 11111111111111111111111111 + +package ExecutableSemanticsTest api; + +fn Main() -> i32 { + return 11111111111111111111111111; +} diff --git a/executable_semantics/testdata/basic_syntax/fail_invalid_var_expression.carbon b/executable_semantics/testdata/basic_syntax/fail_invalid_var_expression.carbon new file mode 100644 index 000000000000..dccd4f1ef803 --- /dev/null +++ b/executable_semantics/testdata/basic_syntax/fail_invalid_var_expression.carbon @@ -0,0 +1,16 @@ +// 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 +// +// RUN: %{not} %{executable_semantics} %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes=false %s +// RUN: %{not} %{executable_semantics} --trace %s 2>&1 | \ +// RUN: %{FileCheck} --match-full-lines --allow-unused-prefixes %s +// AUTOUPDATE: %{executable_semantics} %s +// CHECK: COMPILATION ERROR: {{.*}}/executable_semantics/testdata/basic_syntax/fail_invalid_var_expression.carbon:14: Expected expression for variable type + +package ExecutableSemanticsTest api; + +var a: auto; + +fn Main() -> i32 { return 0; }