Skip to content

False positive in populating a slice using another slice #336

@sonalmahajan15

Description

@sonalmahajan15

Below tests cases are reporting a false positive error:

Potential nil panic detected. Observed nil flow from source to dereference point:

  • line1: unassigned variable stack returned from foo() in position 0
  • line2: result 0 of foo() sliced into
func foo(sl []int) []*int {
	if len(sl) > 0 {
		var stack []*int
		for _, n := range sl {
			stack = append(stack, &n)
		}
		return stack  // line1
	}
	return []*int{new(int)}
}

func test1() {
	sl := []int{1, 2}
	_ = *foo(sl)[0]  // line2
}
func foo(sl []int) []*int {
	switch len(sl) {
	case 0:
		return []*int{new(int)}
	default:
		var stack []*int
		for _, n := range sl {
			stack = append(stack, &n)
		}
		return stack  // line1
	}
}

func test2() {
	sl := []int{1, 2}
	_ = *foo(sl)[0]  // line2
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    false positiveRequires more analysis and support

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions