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

nil can be converted to invalid types (ie. int) #1341

Closed
thehowl opened this issue Nov 7, 2023 · 1 comment · Fixed by #1177
Closed

nil can be converted to invalid types (ie. int) #1341

thehowl opened this issue Nov 7, 2023 · 1 comment · Fixed by #1177
Labels
🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related

Comments

@thehowl
Copy link
Member

thehowl commented Nov 7, 2023

First discovered in #1177 (comment) , I think I have forgotten to make an issue to track it separately.

Sample program:

package main

func main() {
        println(int(nil))
}

// Output: 0

int(nil) makes no sense and should not be accepted.

@thehowl thehowl self-assigned this Nov 7, 2023
@thehowl thehowl added 🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related labels Nov 7, 2023
@thehowl thehowl added this to the 🚀 main.gno.land (required) milestone Nov 7, 2023
@thehowl thehowl removed their assignment Nov 7, 2023
@notJoon
Copy link
Member

notJoon commented Nov 9, 2023

I have written the following test:

package gnolang

func TestNilTypeConversion(t *testing.T) {
    m := NewMachine("test", nil)
    testCases := []struct {
        name string
        code string
    }{
        {
            name: "NilToInt",
            code: `package test
            func main(){
                println(int(nil))
            }`,
        },
       // converting to bool, and other types
    }

    for _, tc := range testCases {
        t.Run(tc.name, function(t *testing.T) {
            n := MustParseFile("main.go", tc.code)
            defer func() {
                if r := recover(); r != nil {
                    t.Logf("Recovered from panic as expected: %v", r)
                } else {
                    t.Errorf("Expected a panic for invalid type conversion")
                }
            }()
            m.RunFiles(n)
            m.RunMain()

            if err := m.CheckEmpty(); err != nil {
                t.Fatal(err)
            }
        })
    }
}

If the code is not changed, no error occurs and the test fails. However, if you make a panic in ConvertTo as below, the test passes.

    if tv.IsUndefined() {
+       panic(fmt.Sprintf(
+           "error: cannot convert undefined to %s",
+           t.String()))
-       tv.T = t
-       return
    }

It seems like the problem arises when a nil value is entered as undefined and then converted to a default type. My guess is, the issue could be resolved by preventing type conversion if tv.T is nil or modify the IsUndefined function.

But I'm still not sure, so I'll have to dig more 🤔🤔

gfanton pushed a commit to moul/gno that referenced this issue Jan 18, 2024
This is a particular case of Go append builtin where the array type is
inferred from the first argument. We detect an untyped const passed as
argument, then we convert it to the array type prior to proceed further.
It fixes further consistency checks and value generation.

Fixes gnolang#1136, fixes gnolang#1149, fixes gnolang#1341.

<!-- please provide a detailed description of the changes made in this
pull request. -->

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>

---------

Co-authored-by: Morgan <git@howl.moe>
Co-authored-by: Morgan <morgan@morganbaz.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working 📦 🤖 gnovm Issues or PRs gnovm related
Projects
Development

Successfully merging a pull request may close this issue.

2 participants