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 with non-empty slice assigned through reference and named boolean as guard statement #134

Open
brebeek opened this issue Nov 23, 2023 · 1 comment
Labels
enhancement New feature or request false positive Requires more analysis and support

Comments

@brebeek
Copy link

brebeek commented Nov 23, 2023

Description

Hi team, the following error is reported for the attached code snippet:

nonempty-slice.go:10:7: error: Potential nil panic detected. Observed nil flow from source to dereference point: 
        -> nilaway-test/nonempty-slice.go:18:9: unassigned variable `s` returned from `getSlice()` in position 0
        -> nilaway-test/nonempty-slice.go:10:7: result 0 of `getSlice()` sliced into

code:

const TARGETLEN = 1

func TestNonemptySlice() []string {
	s := getSlice()
	hasMore := len(s) > TARGETLEN
	if hasMore {
		s = s[:TARGETLEN]
	}
	return s
}

func getSlice() []string {
	var s []string
	setSlice(&s)
	return s
}

func setSlice(sp *[]string) {
	if sp != nil {
		*sp = append(*sp, "a")
	}
}

Noticed that if I modify the code to either s be assigned with slice literal in getSlice, or directly use len(s) > TARGETLEN as the if statement condition, no error is reported.

@sonalmahajan15
Copy link
Contributor

@brebeek : ACK. NilAway currently has encoded limited support for analyzing integer arithmetic (e.g., len()), as was also discussed in issue #94 . We do plan to extend support (although with restrictions) for it to avoid reporting such false positives. Thanks for reporting :)

@sonalmahajan15 sonalmahajan15 added enhancement New feature or request false positive Requires more analysis and support labels Dec 5, 2023
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

2 participants