compiler: avoid crashing on invalid non-integer array length
Tweak the array type checking code to avoid crashing on array types
whose length expressions are explicit non-integer types (for example,
"float64(10)"). If such constructs are seen, issue an "invalid array
bound" error.
Fixes golang/go#13486.
Change-Id: Iaf8ed8fda23d8624fa4f15e1a6c4f5b53225d190
Reviewed-on: https://go-review.googlesource.com/91975
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/go/types.cc b/go/types.cc
index 11924e6..e5f84c5 100644
--- a/go/types.cc
+++ b/go/types.cc
@@ -7016,6 +7016,16 @@
return false;
}
+ // For array types, the length expression can be an untyped constant
+ // representable as an int, but we don't allow explicitly non-integer
+ // values such as "float64(10)". See issues #13485 and #13486.
+ if (this->length_->type()->integer_type() == NULL
+ && !this->length_->type()->is_error_type())
+ {
+ go_error_at(this->length_->location(), "invalid array bound");
+ return false;
+ }
+
Numeric_constant nc;
if (!this->length_->numeric_constant_value(&nc))
{