Skip to content

Commit c9008f3

Browse files
aanmk8s-publishing-bot
authored andcommitted
client-go/rest: check if url is nil to prevent nil pointer dereference
Signed-off-by: André Martins <aanm90@gmail.com> Kubernetes-commit: fa1d4c327716ae117120311f566d26abff9d91ad
1 parent 1a46dfd commit c9008f3

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

rest/request.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -519,14 +519,17 @@ func (r Request) finalURLTemplate() url.URL {
519519
newParams[k] = v
520520
}
521521
r.params = newParams
522-
url := r.URL()
522+
u := r.URL()
523+
if u == nil {
524+
return url.URL{}
525+
}
523526

524-
segments := strings.Split(url.Path, "/")
527+
segments := strings.Split(u.Path, "/")
525528
groupIndex := 0
526529
index := 0
527530
trimmedBasePath := ""
528-
if url != nil && r.c.base != nil && strings.Contains(url.Path, r.c.base.Path) {
529-
p := strings.TrimPrefix(url.Path, r.c.base.Path)
531+
if r.c.base != nil && strings.Contains(u.Path, r.c.base.Path) {
532+
p := strings.TrimPrefix(u.Path, r.c.base.Path)
530533
if !strings.HasPrefix(p, "/") {
531534
p = "/" + p
532535
}
@@ -537,7 +540,7 @@ func (r Request) finalURLTemplate() url.URL {
537540
groupIndex = 1
538541
}
539542
if len(segments) <= 2 {
540-
return *url
543+
return *u
541544
}
542545

543546
const CoreGroupPrefix = "api"
@@ -555,11 +558,11 @@ func (r Request) finalURLTemplate() url.URL {
555558
// outlet here in case more API groups are added in future if ever possible:
556559
// https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-groups
557560
// if a wrong API groups name is encountered, return the {prefix} for url.Path
558-
url.Path = "/{prefix}"
559-
url.RawQuery = ""
560-
return *url
561+
u.Path = "/{prefix}"
562+
u.RawQuery = ""
563+
return *u
561564
}
562-
//switch segLength := len(segments) - index; segLength {
565+
// switch segLength := len(segments) - index; segLength {
563566
switch {
564567
// case len(segments) - index == 1:
565568
// resource (with no name) do nothing
@@ -582,8 +585,8 @@ func (r Request) finalURLTemplate() url.URL {
582585
segments[index+3] = "{name}"
583586
}
584587
}
585-
url.Path = path.Join(trimmedBasePath, path.Join(segments...))
586-
return *url
588+
u.Path = path.Join(trimmedBasePath, path.Join(segments...))
589+
return *u
587590
}
588591

589592
func (r *Request) tryThrottleWithInfo(ctx context.Context, retryInfo string) error {

0 commit comments

Comments
 (0)