Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent variable initialization inside loop #1381

Closed
cclerget opened this issue Apr 12, 2022 · 0 comments · Fixed by #1391
Closed

Inconsistent variable initialization inside loop #1381

cclerget opened this issue Apr 12, 2022 · 0 comments · Fixed by #1391
Labels
area/core bug Something isn't working
Milestone

Comments

@cclerget
Copy link
Contributor

The following program sample.go triggers an unexpected result

package main

import (
    "fmt"
    "bytes"
)

func main() {
    var bufPtrOne *bytes.Buffer
    var bufPtrTwo *bytes.Buffer

    for i := 0; i < 2; i++ {
        bufOne := bytes.Buffer{}
        bufTwo := &bytes.Buffer{}

        if bufPtrOne == nil {
            bufPtrOne = &bufOne
        } else if bufPtrOne == &bufOne {
            fmt.Println("KO")
        }
        if bufPtrTwo == nil {
            bufPtrTwo = bufTwo
        } else if bufPtrTwo == bufTwo {
            fmt.Println("KO")
        }
    }

    bufPtrOne = nil
    bufPtrTwo = nil

    for i := 0; i < 2; i++ {
        var bufOne bytes.Buffer
        bufTwo := new(bytes.Buffer)

        if bufPtrOne == nil {
            bufPtrOne = &bufOne
        } else if bufPtrOne != &bufOne {
            fmt.Println("OK")
        }
        if bufPtrTwo == nil {
            bufPtrTwo = bufTwo
        } else if bufPtrTwo != bufTwo {
            fmt.Println("OK")
        }
    }
}

Expected result

$ go run ./sample.go
OK
OK

Got

$ yaegi ./sample.go
KO
KO
OK
OK

Yaegi Version

v0.11.3

Additional Notes

No response

@mvertes mvertes added bug Something isn't working area/core labels Apr 13, 2022
@mvertes mvertes added this to the v0.11.x milestone Apr 13, 2022
mvertes added a commit to mvertes/yaegi that referenced this issue Apr 27, 2022
Use direct assignment instead of reflect.Value Set method to
initialize a binary composite type, as for non binary types.
It ensures that a new reflect.Value is stored in the frame
instead of modifying a possibly existing one, which can defeat
the purpose of initializing variables in a body loop.

While there, remove the need to have and use a mutex on types.

Fixes traefik#1381.
@mpl mpl changed the title Inconsitent variable initialization inside loop Inconsistent variable initialization inside loop May 5, 2022
mvertes added a commit that referenced this issue May 5, 2022
* interp: fix creation of binary composite types

Use direct assignment instead of reflect.Value Set method to
initialize a binary composite type, as for non binary types.
It ensures that a new reflect.Value is stored in the frame
instead of modifying a possibly existing one, which can defeat
the purpose of initializing variables in a body loop.

While there, remove the need to have and use a mutex on types.

Fixes #1381.

* review: rework a bit the test

Co-authored-by: mpl <mathieu.lonjaret@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants