aboutsummaryrefslogtreecommitdiff
path: root/gcc/go
diff options
context:
space:
mode:
authorIan Lance Taylor <ian@gcc.gnu.org>2012-12-04 09:09:56 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2012-12-04 09:09:56 +0000
commitf2b2ead42ed389a918b586b3f95c26455ae13e7c (patch)
tree54799044fac4141cc62e6e4093ac13f9aeeb36fd /gcc/go
parent44bd7f65250cd0814b346679ef801ae0722e0483 (diff)
downloadgcc-f2b2ead42ed389a918b586b3f95c26455ae13e7c.zip
gcc-f2b2ead42ed389a918b586b3f95c26455ae13e7c.tar.gz
gcc-f2b2ead42ed389a918b586b3f95c26455ae13e7c.tar.bz2
compiler: Give error for constant inverted slice range.
From-SVN: r194124
Diffstat (limited to 'gcc/go')
-rw-r--r--gcc/go/gofrontend/expressions.cc14
1 files changed, 12 insertions, 2 deletions
diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc
index e0690e7..7a5fcf2 100644
--- a/gcc/go/gofrontend/expressions.cc
+++ b/gcc/go/gofrontend/expressions.cc
@@ -9770,8 +9770,10 @@ Array_index_expression::do_check_types(Gogo*)
&& lvalnc.to_int(&lval));
Numeric_constant inc;
mpz_t ival;
+ bool ival_valid = false;
if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
{
+ ival_valid = true;
if (mpz_sgn(ival) < 0
|| mpz_sizeinbase(ival, 2) >= int_bits
|| (lval_valid
@@ -9782,7 +9784,6 @@ Array_index_expression::do_check_types(Gogo*)
error_at(this->start_->location(), "array index out of bounds");
this->set_is_error();
}
- mpz_clear(ival);
}
if (this->end_ != NULL && !this->end_->is_nil_expression())
{
@@ -9797,9 +9798,13 @@ Array_index_expression::do_check_types(Gogo*)
error_at(this->end_->location(), "array index out of bounds");
this->set_is_error();
}
+ else if (ival_valid && mpz_cmp(ival, eval) > 0)
+ this->report_error(_("inverted slice range"));
mpz_clear(eval);
}
}
+ if (ival_valid)
+ mpz_clear(ival);
if (lval_valid)
mpz_clear(lval);
@@ -10180,15 +10185,16 @@ String_index_expression::do_check_types(Gogo*)
Numeric_constant inc;
mpz_t ival;
+ bool ival_valid = false;
if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
{
+ ival_valid = true;
if (mpz_sgn(ival) < 0
|| (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
{
error_at(this->start_->location(), "string index out of bounds");
this->set_is_error();
}
- mpz_clear(ival);
}
if (this->end_ != NULL && !this->end_->is_nil_expression())
{
@@ -10202,9 +10208,13 @@ String_index_expression::do_check_types(Gogo*)
error_at(this->end_->location(), "string index out of bounds");
this->set_is_error();
}
+ else if (ival_valid && mpz_cmp(ival, eval) > 0)
+ this->report_error(_("inverted slice range"));
mpz_clear(eval);
}
}
+ if (ival_valid)
+ mpz_clear(ival);
}
// Get a tree for a string index.