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 dd947016b3

[1] https://golang.org/pkg/regexp/#MatchString

https://github.com/containers/toolbox/pull/825
This commit is contained in:
Ondřej Míchal 2021-07-07 17:48:25 +02:00 committed by Debarshi Ray
parent 210eded9a3
commit b166a1f13f
2 changed files with 6 additions and 3 deletions

View File

@ -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 {

View File

@ -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 {