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 when a deep read uses an expression #256

Open
cgetzen opened this issue Jun 12, 2024 · 1 comment
Open

False positive when a deep read uses an expression #256

cgetzen opened this issue Jun 12, 2024 · 1 comment

Comments

@cgetzen
Copy link

cgetzen commented Jun 12, 2024

func false_positive() {
	outer := make(map[string]map[string]int)
	inner := []string{"a"}

	if _, ok := outer[inner[0]]; !ok {
		outer[inner[0]] = map[string]int{}
	}

	outer[inner[0]]["value"] = 1
}
// error: Potential nil panic detected. Observed nil flow from source to dereference point:
//        - deep read from local variable `outer` lacking guarding; written to at an index


// When the expression is assigned to a variable, it succeeds
func true_negative() {
	outer := make(map[string]map[string]int)
	inner := []string{"a"}
	x := inner[0]

	if _, ok := outer[x]; !ok {
		outer[x] = map[string]int{}
	}

	outer[x]["value"] = 1
}
@cgetzen
Copy link
Author

cgetzen commented Jun 12, 2024

I'm hesitant to create another issue, as I think the root cause may be similar:

func false_positive() error {
	var slice []int
	sliceLen := len(slice)

	for idx := 0; idx < sliceLen; idx += 1 {
		_ = slice[0:idx]
	}
	return nil
}
// error: Potential nil panic detected. Observed nil flow from source to dereference point:
//        -  unassigned variable `slice` sliced into


func true_negative() error {
	var slice []int

	for idx := 0; idx < len(slice); idx += 1 {
		_ = slice[0:idx]
	}
	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant