Skip to content

runtime.InstantiateModule returns a "struct-wrapped" nil value #2353

@edman

Description

@edman

Describe the bug
runtime.InstantiateModule sometimes returns a "struct-wrapped" nil value, instead of returning actual nil.

More specifically, this line https://github.com/tetratelabs/wazero/blob/main/runtime.go#L318 assigns mod and returns it implicitly on L324.

This allows code like the following to panic with a nil pointer dereference because m is nil:

m, _ := r.InstantiateModule(ctx, compiled, config)
if m != nil {      // this check passes
  defer m.Close()  // and yet this causes a nil pointer dereference
}

To Reproduce
Here is an example that mimics wazero's behavior: https://go.dev/play/p/66QShXidalq

Expected behavior
From https://go.dev/doc/faq#nil_error, the FAQ recommends:

To return a proper nil error to the caller, the function must return an explicit nil

Additional context
Found on sqlc-dev/sqlc#3759 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions