From 8bcf26b575b1c6dc09e4c1acb41a130cec180ec3 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 23 Dec 2022 17:10:42 +0100 Subject: [PATCH] k8s.io/component-base/logs: fix usage through Go flag package api/v1.AddFlags only supports a pflag.FlagSet. The assumption was that code which wants to use flag.FlagSet can use VisitAll to copy the flags. That works, with one caveat: the flag.FlagSet help implementation will call String for the zero value to determine whether the flag has a non-default value. This currently leads to additional warnings at the end of the -help output: panic calling String method on zero v1.verbosityLevelPflag for flag v: runtime error: invalid memory address or nil pointer dereference panic calling String method on zero v1.vmoduleConfigurationPFlag for flag vmodule: runtime error: invalid memory address or nil pointer dereference Supporting usage of methods with the zero value is good practice anyway and thus gets added. This then avoids these panics. --- staging/src/k8s.io/component-base/logs/api/v1/pflags.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/staging/src/k8s.io/component-base/logs/api/v1/pflags.go b/staging/src/k8s.io/component-base/logs/api/v1/pflags.go index 36a98cc81cf..b74e132a722 100644 --- a/staging/src/k8s.io/component-base/logs/api/v1/pflags.go +++ b/staging/src/k8s.io/component-base/logs/api/v1/pflags.go @@ -36,6 +36,9 @@ type vmoduleConfigurationPFlag struct { // String returns the -vmodule parameter (comma-separated list of pattern=N). func (wrapper vmoduleConfigurationPFlag) String() string { + if wrapper.value == nil { + return "" + } var patterns []string for _, item := range *wrapper.value { patterns = append(patterns, fmt.Sprintf("%s=%d", item.FilePattern, item.Verbosity)) @@ -82,10 +85,16 @@ type verbosityLevelPflag struct { } func (wrapper verbosityLevelPflag) String() string { + if wrapper.value == nil { + return "0" + } return strconv.FormatInt(int64(*wrapper.value), 10) } func (wrapper verbosityLevelPflag) Get() interface{} { + if wrapper.value == nil { + return VerbosityLevel(0) + } return *wrapper.value }