Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

fix: Missing nil checks #3096

Merged
merged 2 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions npc/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@ func newRuleSpec(proto *string, srcHost *selectorSpec, dstHost *selectorSpec, ds
args = append(args, "--dport", *dstPort)
}
args = append(args, "-j", "ACCEPT")
srcComment := ""
if srcHost.nsName != "" {
srcComment = fmt.Sprintf("pods: namespace: %s, selector: %s", srcHost.nsName, srcHost.key)
} else {
srcComment = fmt.Sprintf("namespaces: selector: %s", srcHost.key)
srcComment := "anywhere"
if srcHost != nil {
if srcHost.nsName != "" {
srcComment = fmt.Sprintf("pods: namespace: %s, selector: %s", srcHost.nsName, srcHost.key)
} else {
srcComment = fmt.Sprintf("namespaces: selector: %s", srcHost.key)
}
}
dstComment := "anywhere"
if dstHost != nil {
dstComment = fmt.Sprintf("pods: namespace: %s, selector: %s", dstHost.nsName, dstHost.key)

This comment was marked as abuse.

This comment was marked as abuse.

}
args = append(args, "-m", "comment", "--comment", fmt.Sprintf("%s -> namespace: %s, key: %s", srcComment, dstHost.nsName, dstHost.key))
args = append(args, "-m", "comment", "--comment", fmt.Sprintf("%s -> %s", srcComment, dstComment))
key := strings.Join(args, " ")

return &ruleSpec{key, args}
Expand Down
16 changes: 16 additions & 0 deletions npc/rule_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package npc

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestRegressionHandleNil3095(t *testing.T) {
// Test for handling nil values
// https://github.com/weaveworks/weave/issues/3095

rs := newRuleSpec(nil, nil, nil, nil)

require.NotEqual(t, rs.key, "")

}