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

Guard against nil pointers in tryResourceAsString #3982

Merged
merged 1 commit into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion upup/pkg/fi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ go_library(

go_test(
name = "go_default_test",
srcs = ["vfs_castore_test.go"],
srcs = [
"dryruntarget_test.go",
"vfs_castore_test.go",
],
importpath = "k8s.io/kops/upup/pkg/fi",
library = ":go_default_library",
)
14 changes: 12 additions & 2 deletions upup/pkg/fi/dryrun_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ import (
"fmt"
"io"
"reflect"
"sort"
"strings"
"sync"

"sort"

"github.com/golang/glog"
"k8s.io/kops/pkg/assets"
"k8s.io/kops/pkg/diff"
Expand Down Expand Up @@ -340,10 +339,21 @@ func buildChangeList(a, e, changes Task) ([]change, error) {
}

func tryResourceAsString(v reflect.Value) (string, bool) {
if !v.IsValid() {
return "", false
}
if !v.CanInterface() {
return "", false
}

// Guard against nil interface go-tcha
if v.Kind() == reflect.Interface && v.IsNil() {
return "", false
}
if v.Kind() == reflect.Ptr && v.IsNil() {
return "", false
}

intf := v.Interface()
if res, ok := intf.(Resource); ok {
s, err := ResourceAsString(res)
Expand Down
50 changes: 50 additions & 0 deletions upup/pkg/fi/dryruntarget_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copyright 2017 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fi

import (
"reflect"
"testing"
)

func Test_tryResourceAsString(t *testing.T) {
var sr *StringResource
grid := []struct {
Resource interface{}
Expected string
}{
{
Resource: NewStringResource("hello"),
Expected: "hello",
},
{
Resource: sr,
Expected: "",
},
{
Resource: nil,
Expected: "",
},
}
for i, g := range grid {
v := reflect.ValueOf(g.Resource)
actual, _ := tryResourceAsString(v)
if actual != g.Expected {
t.Errorf("unexpected result from %d. Expected=%q, got %q", i, g.Expected, actual)
}
}
}