From b166a1f13fd7dabf29bea7a12eb6cdc52fdbee99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20M=C3=ADchal?= Date: Wed, 7 Jul 2021 17:48:25 +0200 Subject: [PATCH] cmd/create: pkg/utils: Fix wrong use of regexp.MatchString return value The regexp.MatchString [1] API returns an error only when the regular expression is faulty, and the boolean return value tells if a match was found. In this case, the regular expression is baked into the code as a string literal. So, unless there's a programmer error, it should always be valid. Fallout dd947016b3a9098f23b2a77a3616a4f5891ae8ab [1] https://golang.org/pkg/regexp/#MatchString https://github.com/containers/toolbox/pull/825 --- src/cmd/create.go | 2 +- src/pkg/utils/utils.go | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cmd/create.go b/src/cmd/create.go index e3245b4..ff533c2 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -669,7 +669,7 @@ func isPathReadWrite(path string) (bool, error) { } func pullImage(image, release string) (bool, error) { - if _, err := utils.ImageReferenceCanBeID(image); err == nil { + if ok := utils.ImageReferenceCanBeID(image); ok { logrus.Debugf("Looking for image %s", image) if _, err := podman.ImageExists(image); err == nil { diff --git a/src/pkg/utils/utils.go b/src/pkg/utils/utils.go index b6eecb2..4611efa 100644 --- a/src/pkg/utils/utils.go +++ b/src/pkg/utils/utils.go @@ -500,9 +500,12 @@ func HumanDuration(duration int64) string { } // ImageReferenceCanBeID checks if 'image' might be the ID of an image -func ImageReferenceCanBeID(image string) (bool, error) { +func ImageReferenceCanBeID(image string) bool { matched, err := regexp.MatchString("^[a-f0-9]\\{6,64\\}$", image) - return matched, err + if err != nil { + panic("regular expression for ID reference matching is invalid") + } + return matched } func ImageReferenceGetBasename(image string) string {