diff --git a/reference.go b/reference.go index e07c43c..ae02ee8 100644 --- a/reference.go +++ b/reference.go @@ -35,8 +35,8 @@ import ( ) const ( - // NameTotalLengthMax is the maximum total number of characters in a repository name. - NameTotalLengthMax = 255 + // RepositoryNameTotalLengthMax is the maximum total number of characters in a repository name. + RepositoryNameTotalLengthMax = 255 ) var ( @@ -55,8 +55,8 @@ var ( // ErrNameEmpty is returned for empty, invalid repository names. ErrNameEmpty = errors.New("repository name must have at least one component") - // ErrNameTooLong is returned when a repository name is longer than NameTotalLengthMax. - ErrNameTooLong = fmt.Errorf("repository name must not be more than %v characters", NameTotalLengthMax) + // ErrNameTooLong is returned when a repository name is longer than RepositoryNameTotalLengthMax. + ErrNameTooLong = fmt.Errorf("repository name must not be more than %v characters", RepositoryNameTotalLengthMax) // ErrNameNotCanonical is returned when a name is not canonical. ErrNameNotCanonical = errors.New("repository name must be canonical") @@ -190,10 +190,6 @@ func Parse(s string) (Reference, error) { return nil, ErrReferenceInvalidFormat } - if len(matches[1]) > NameTotalLengthMax { - return nil, ErrNameTooLong - } - var repo repository nameMatch := anchoredNameRegexp.FindStringSubmatch(matches[1]) @@ -205,6 +201,10 @@ func Parse(s string) (Reference, error) { repo.path = matches[1] } + if len(repo.path) > RepositoryNameTotalLengthMax { + return nil, ErrNameTooLong + } + ref := reference{ namedRepository: repo, tag: matches[2], @@ -243,18 +243,21 @@ func ParseNamed(s string) (Named, error) { // WithName returns a named object representing the given string. If the input // is invalid ErrReferenceInvalidFormat will be returned. func WithName(name string) (Named, error) { - if len(name) > NameTotalLengthMax { - return nil, ErrNameTooLong - } - match := anchoredNameRegexp.FindStringSubmatch(name) if match == nil || len(match) != 3 { return nil, ErrReferenceInvalidFormat } - return repository{ + + repo := repository{ domain: match[1], path: match[2], - }, nil + } + + if len(repo.path) > RepositoryNameTotalLengthMax { + return nil, ErrNameTooLong + } + + return repo, nil } // WithTag combines the name from "name" and the tag from "tag" to form a diff --git a/reference_test.go b/reference_test.go index b2f0fe7..42b08d2 100644 --- a/reference_test.go +++ b/reference_test.go @@ -117,7 +117,7 @@ func TestReferenceParse(t *testing.T) { tag: "Uppercase", }, { - input: strings.Repeat("a/", 128) + "a:tag", + input: "domain/" + strings.Repeat("a", 256) + ":tag", err: ErrNameTooLong, }, { @@ -266,6 +266,12 @@ func TestReferenceParse(t *testing.T) { input: "[fe80::1%@invalidzone]:5000/repo", err: ErrReferenceInvalidFormat, }, + { + input: "domain/" + strings.Repeat("a", 255) + ":tag", + domain: "domain", + repository: "domain/" + strings.Repeat("a", 255), + tag: "tag", + }, } for _, tc := range tests { tc := tc