@@ -1070,7 +1070,17 @@ func fixArrayType(bad *ast.BadExpr, parent ast.Node, tok *token.File, src []byte
1070
1070
1071
1071
exprBytes := make ([]byte , 0 , int (to - from )+ 3 )
1072
1072
// Avoid doing tok.Offset(to) since that panics if badExpr ends at EOF.
1073
- exprBytes = append (exprBytes , src [tok .Offset (from ):tok .Offset (to - 1 )+ 1 ]... )
1073
+ // It also panics if the position is not in the range of the file, and
1074
+ // badExprs may not necessarily have good positions, so check first.
1075
+ if ! inRange (tok , from ) {
1076
+ return false
1077
+ }
1078
+ if ! inRange (tok , to - 1 ) {
1079
+ return false
1080
+ }
1081
+ fromOffset := tok .Offset (from )
1082
+ toOffset := tok .Offset (to - 1 ) + 1
1083
+ exprBytes = append (exprBytes , src [fromOffset :toOffset ]... )
1074
1084
exprBytes = bytes .TrimSpace (exprBytes )
1075
1085
1076
1086
// If our expression ends in "]" (e.g. "[]"), add a phantom selector
@@ -1102,6 +1112,12 @@ func fixArrayType(bad *ast.BadExpr, parent ast.Node, tok *token.File, src []byte
1102
1112
return replaceNode (parent , bad , at )
1103
1113
}
1104
1114
1115
+ // inRange reports whether the given position is in the given token.File.
1116
+ func inRange (tok * token.File , pos token.Pos ) bool {
1117
+ size := tok .Pos (tok .Size ())
1118
+ return int (pos ) >= tok .Base () && pos <= size
1119
+ }
1120
+
1105
1121
// precedingToken scans src to find the token preceding pos.
1106
1122
func precedingToken (pos token.Pos , tok * token.File , src []byte ) token.Token {
1107
1123
s := & scanner.Scanner {}
0 commit comments