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

False positive in error handling logic for "always-safe" case #151

Open
sonalmahajan15 opened this issue Dec 8, 2023 · 8 comments
Open
Labels
enhancement New feature or request false positive Requires more analysis and support

Comments

@sonalmahajan15
Copy link
Contributor

sonalmahajan15 commented Dec 8, 2023

NilAway is encoded with the pattern that if the error return is non-nil, then inherently assume non-error returns to be nilable, and hence need guarding. However, this reports a false positive when the non-error return is always returned as non-nil, independent of the status of the error return, and hence does not really need to be guarded.

func retErr() (*int, error) {
	return new(int), errors.New("error")
}

func callRetErr() {
	v, err := retErr()
	if err != nil {
		print(err)
	}
	_ = *v // error: result 0 of `retErr()` lacking guarding; dereferenced
}
@sonalmahajan15 sonalmahajan15 added enhancement New feature or request false positive Requires more analysis and support labels Dec 8, 2023
@Integralist
Copy link

I have a somewhat similar false positive...

// Library code

func (t *Thing) GetFoo() (*string, bool) {
	if t == nil || t.Foo == nil {
		return nil, false
	}
	return t.Foo, true
}

// Application code

if ptr, ok := t.GetFoo(); ok {
	v := *ptr // nilaway reports this deference hasn't been checked, when it has, by the GetFoo method
}

@sonalmahajan15
Copy link
Contributor Author

@Integralist, you're absolutely right. However, the false positive you reported is due to a slightly different reason. It's occurring because NilAway doesn't currently understand the ok form of functions/methods. We have plans to address this and add support, which is being tracked in issue #77 on our roadmap.

@Integralist
Copy link

Amazing, thanks @sonalmahajan15 for clarifying 👍🏻

@kfcss
Copy link

kfcss commented Apr 8, 2024

@sonalmahajan15 I think this has been resolved?

@sonalmahajan15
Copy link
Contributor Author

@kfcss , the problem in the second code snippet has been resolved by PR #157 , but the first code snippet (for which the issue was created) is still work in progress. It's proving to be a little tricky to handle this "always-safe" case.

@sonalmahajan15 sonalmahajan15 changed the title False positive from error handling logic False positive in error handling logic for "always-safe" case Apr 8, 2024
@sonalmahajan15
Copy link
Contributor Author

(Linking related issue #199)

@sircelsius
Copy link

@sonalmahajan15 any idea if this will be addressed soon?

@sonalmahajan15
Copy link
Contributor Author

@sircelsius: I am working on addressing this issue at the moment. Hopefully I should have the support for "always-safe" case ready in a couple of weeks.

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

No branches or pull requests

4 participants