FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com),

I'd like you to review this change to
https://code.google.com/p/go/


Description:
cmd/gc: do simple bounds checking of constant indices/slices in
typecheck.

This should make the compiler emit errors specific to the bounds
checking instead of overflow errors on the underlying types.

Updates issue 4232.

Please review this at http://codereview.appspot.com/6783054/

Affected files:
M src/cmd/gc/typecheck.c


Index: src/cmd/gc/typecheck.c
===================================================================
--- a/src/cmd/gc/typecheck.c
+++ b/src/cmd/gc/typecheck.c
@@ -817,9 +817,17 @@

case TARRAY:
defaultlit(&n->right, T);
- if(n->right->type != T && !isint[n->right->type->etype])
+ n->type = t->type;
+ if(n->right->type != T && !isint[n->right->type->etype]) {
yyerror("non-integer array index %N", n->right);
- n->type = t->type;
+ break;
+ }
+ if(n->right->op == OLITERAL) {
+ if(mpgetfix(n->right->val.u.xval) < 0)
+ yyerror("slice/array index must be non-negative");
+ else if(isfixedarray(t) && t->bound > 0 &&
mpgetfix(n->right->val.u.xval) >= t->bound)
+ yyerror("array index out of bounds");
+ }
break;

case TMAP:
@@ -912,6 +920,8 @@
yyerror("invalid slice index %N (type %T)", n->right->left, t);
goto error;
}
+ if(n->right->left->op == OLITERAL &&
mpgetfix(n->right->left->val.u.xval) < 0)
+ yyerror("invalid slice index %N (index must be non-negative)",
n->right->left);
}
if(n->right->right != N) {
if((t = n->right->right->type) == T)
@@ -920,6 +930,8 @@
yyerror("invalid slice index %N (type %T)", n->right->right, t);
goto error;
}
+ if(n->right->right->op == OLITERAL &&
mpgetfix(n->right->right->val.u.xval) < 0)
+ yyerror("invalid slice index %N (index must be non-negative)",
n->right->right);
}
l = n->left;
if((t = l->type) == T)

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedOct 26, '12 at 9:42p
activeOct 26, '12 at 9:42p
posts1
users1
websitegolang.org

1 user in discussion

Daniel Morsing: 1 post

People

Translate

site design / logo © 2021 Grokbase