Merge pull request #2871 from nspcc-dev/fix-shadowed-infinite-loop

Fix shadowed infinite loop
This commit is contained in:
Roman Khimov 2023-01-12 12:48:39 +07:00 committed by GitHub
commit babc44bfb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 7 deletions

View file

@ -1301,21 +1301,45 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
emit.Opcodes(c.prog.BinWriter, opcode.OVER, opcode.OVER)
emit.Jmp(c.prog.BinWriter, opcode.JMPLEL, end)
var keyLoaded bool
needValue := n.Value != nil && n.Value.(*ast.Ident).Name != "_"
if n.Key != nil && n.Key.(*ast.Ident).Name != "_" {
var (
haveKey bool
haveVal bool
keyIdent *ast.Ident
keyLoaded bool
valIdent *ast.Ident
)
if n.Key != nil {
keyIdent, haveKey = n.Key.(*ast.Ident)
if !haveKey {
c.prog.Err = errors.New("only simple identifiers can be used for range loop keys (see #2870)")
return nil
}
haveKey = (keyIdent.Name != "_")
}
if n.Value != nil {
valIdent, haveVal = n.Value.(*ast.Ident)
if !haveVal {
c.prog.Err = errors.New("only simple identifiers can be used for range loop values (see #2870)")
return nil
}
haveVal = (valIdent.Name != "_")
}
if haveKey {
if isMap {
c.rangeLoadKey()
if needValue {
if haveVal {
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
keyLoaded = true
}
} else {
emit.Opcodes(c.prog.BinWriter, opcode.DUP)
}
c.emitStoreVar("", n.Key.(*ast.Ident).Name)
if n.Tok == token.DEFINE {
c.scope.newLocal(keyIdent.Name)
}
if needValue {
c.emitStoreVar("", keyIdent.Name)
}
if haveVal {
if !isMap || !keyLoaded {
c.rangeLoadKey()
}
@ -1327,7 +1351,10 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
opcode.SWAP, // key should be on top
opcode.PICKITEM)
}
c.emitStoreVar("", n.Value.(*ast.Ident).Name)
if n.Tok == token.DEFINE {
c.scope.newLocal(valIdent.Name)
}
c.emitStoreVar("", valIdent.Name)
}
ast.Walk(c, n.Body)

View file

@ -714,6 +714,32 @@ var forLoopTestCases = []testCase{
`,
big.NewInt(6),
},
{
"shadow range key",
`func F%d() int {
i := 10
ints := []int{1, 2, 3, 4, 5}
for i := range ints {
_ = i
}
return i
}
`,
big.NewInt(10),
},
{
"shadow range value",
`func F%d() int {
i := 10
ints := []int{1, 2, 3, 4, 5}
for _, i := range ints {
_ = i
}
return i
}
`,
big.NewInt(10),
},
}
func TestForLoop(t *testing.T) {