Skip to content

[SAST] Unreachable code related to error checking #2858

@Anchels

Description

@Anchels

What steps did you take and what happened:

I've been analyzing the source code using the Svace static analyzer and it has found some inconsistent error-checking issues that I believe are confirmed.

What did you expect to happen:

Redundant error checks are removed and incorrect ones are adjusted

Anything else you would like to add:

The err variable is double-checked:
(consider removing the second check)

  1. config, err := getConfig(ctx)
    if err != nil {
    return corev1.PodSpec{}, nil, err
    }
    command := config.GetCommand()
    if err != nil {
    return corev1.PodSpec{}, nil, err
    }
  2. config, err := getConfig(ctx)
    if err != nil {
    return corev1.PodSpec{}, nil, err
    }
    command := config.GetCommand()
    if err != nil {
    return corev1.PodSpec{}, nil, err
    }
  3. config, err := getConfig(ctx)
    if err != nil {
    return vulnReport, secretReport, nil, err
    }
    cmd := config.GetCommand()
    if err != nil { // TODO: condition seems incorrect
    return vulnReport, secretReport, nil, err
    }

Incorrect check of the ok variable:
(also I guess trivyoperator.LabelResourceSpecHash should be trivyoperator.LabelReusedReport in the error message)

  1. _, reused := job.Labels[trivyoperator.LabelReusedReport]
    if !ok {
    return VulnerabilityReports{}, nil, nil, fmt.Errorf("expected label %s not set", trivyoperator.LabelResourceSpecHash)
    }

which is obtained from:

podSpecHash, ok := job.Labels[trivyoperator.LabelResourceSpecHash]
if !ok {
return VulnerabilityReports{}, nil, nil, fmt.Errorf("expected label %s not set", trivyoperator.LabelResourceSpecHash)
}

So I believe the fix would be:

 reused, ok := job.Labels[trivyoperator.LabelReusedReport] 
 if !ok { 
 	return VulnerabilityReports{}, nil, nil, fmt.Errorf("expected label %s not set", trivyoperator.LabelReusedReport) 
 } 

Environment:

  • Trivy-Operator version (use trivy-operator version): v0.27.3 (also checked that problem persists in main branch)

Found by Linux Verification Center with SVACE

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugCategorizes issue or PR as related to a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions