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

Add support for errors.As in trusted functions framework #95

Closed
sonalmahajan15 opened this issue Nov 8, 2023 · 4 comments · Fixed by #281
Closed

Add support for errors.As in trusted functions framework #95

sonalmahajan15 opened this issue Nov 8, 2023 · 4 comments · Fixed by #281
Labels
false positive Requires more analysis and support

Comments

@sonalmahajan15
Copy link
Contributor

error.As reports a false positive as shown below. Add support for errors.As, and other similar functions in the trusted functions framework.

func retErr() error {
	return nil
}

type myErr struct{}

func (myErr) Error() string { return "myErr message" }

func test() {
	err := retErr()
	var x *myErr
	if errors.As(err, &x) {
		print(*x) // error reported here
	}
}
@sonalmahajan15 sonalmahajan15 added the false positive Requires more analysis and support label Dec 5, 2023
@gaby
Copy link

gaby commented Dec 29, 2023

@sonalmahajan15 Is there a timeline for fixing this false positive?

@sonalmahajan15
Copy link
Contributor Author

@gaby, I am working on a few higher priority items at the moment, after which I plan to come to this issue. The implementation of this feature is not as straightforward either. So, unfortunately it might take a little more time than expected.

@yuxincs
Copy link
Contributor

yuxincs commented Feb 6, 2024

The weird thing here is that errors.As could still result in the result error being nil. See https://go.dev/play/p/0aHuuJb2K36:

package main

import (
	"errors"
	"fmt"

	"github.com/go-sql-driver/mysql"
)

func main() {
	var x *mysql.MySQLError
	var err error = x
	var merr *mysql.MySQLError
	fmt.Println(err == nil)
	fmt.Println(errors.As(err, &merr))
	fmt.Println(merr == nil)
}

The doc states that

As finds the first error in err’s tree that matches target, and if one is found, sets target to that error value and returns true. Otherwise, it returns false.

In this case, the first error is a typed nil (with the type *mysql.MySQLError), so errors.As is setting the second arg to the typed nil instead, hence the results.

However, in practice we probably will never encounter such as case, since if we really want to say “there’s no error”, we return an untyped nil (e.g., return nil, where the return type is the interface error). This is also probably why the example in the doc also assumes the result is nonnil (directly dereferencing the field .Path).

Go is a bit weird when it comes to interfaces and typed nil vs untyped nil. But for this particular case I think NilAway can assume the result would be nonnil (just like what the official doc did).

@hurrycaine
Copy link

I have a bit of code w/ custom errors and errors.As sprinkled throughout to get additional info, it would be nice to have a flag to ignore error.As.

As @yuxincs pointed out it technically is possible but very unlikely in real settings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false positive Requires more analysis and support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants