Skip to content

fix: reject duplicate variants in strict BCP47 tags - #1678

Merged
nodivbyzero merged 2 commits into
go-playground:masterfrom
guocn:fix-bcp47-duplicate-variants
Oct 28, 2026
Merged

nodivbyzero merged 2 commits into
go-playground:masterfrom
guocn:fix-bcp47-duplicate-variants

Conversation

@guocn

@guocn guocn commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes Or Enhances

Fixes #1677. The strict BCP47 validator checks that each variant is registered but did not check whether the same variant already occurred in the tag. RFC5646 section2.2.5 item5 explicitly prohibits repeated variants and gives de-DE-1901-1901 as invalid: https://www.rfc-editor.org/rfc/rfc5646.html#section-2.2.5.

Track variants in a local set inside the existing variant loop. The input has already been lowercased, so mixed-case repeats are rejected too. Six production lines; no changes to the non-strict validator,variant-prefix recommendations,private-use subtags or registry data.

  • Tests exist or have been written that cover this particular change.

Five duplicate-variant subtests fail on unchanged source and five controls pass. All ten pass after the fix. Controls retain distinct variants,mixed-case distinct variants,and repeated content in private-use subtags. Added a focused benchmark and one documentation sentence. There is no repository changelog file/convention requiring a separate entry.

Local validation

Linuxamd64,Go1.27.3 and Go1.26.10,baseline a5843c6 -> head5c0c4d5:

  • go test -run 'TestBCP47.*' -count=1 -v:passes existing strict/non-strict tests and all ten new subtests.
  • go test -race -covermode=atomic -coverprofile=... ./...:passes all packages on Go1.27.3; rootpackage97.5%statementcoverage.
  • go test ./...:passes all packages on Go1.26.10. Untouched baseline Go1.27.3 full suite also passes.
  • Focused race repeat go test -race -run TestBCP47StrictDuplicateVariants -count=20:passes.
  • go vet ./...:passes on Go1.27.3.
  • Project-pinned golangci-lintv2.13.1 on Go1.26.10:0issues. That linter's prebuilt binary cannot decode Go1.27exportdata; rerun on the CI-configured Go1.26toolchain resolves this tooling limitation.
  • gofmt and git diff --check:pass.
  • NativeWindows/macOS and upstreamCI results are not claimed.

Benchmark

Eight interleaved fresh-process baseline/patch samples,200ms each case,Go1.27.3:

  • no-variant en-US:baseline median492.30ns[448.3–551.9] -> patch499.75[487.1–614.0].
  • onevariant sl-rozaj:615.60ns[578.1–665.6] ->644.05[600.0–676.5].
  • twovariants sl-rozaj-biske:692.55ns[631.6–843.3] ->746.55[671.3–831.2].
  • duplicate previouslyaccepted tag:765.55ns[690.4–894.0] ->964.55[904.4–1076.0]; now allocates a validation error,so this is not an equivalent-success performance comparison.

Valid-input ranges overlap,with sample-median increases around1.5%/4.6%/7.8%; this is not a proof of performance neutrality. Allocations for valid one/twovariant cases remain3/op in both arms. The local set does not affect tags without variants. No unresolved API/design choice; the extra check implements the normative MUSTNOT requirement.

AI attribution

Prepared with AI assistance for analysis, implementation, tests, benchmarks and this description. No human code review is claimed. The commit retains the attribution required by this repository.

Co-Authored-By: OpenAI GPT-6.1 <noreply@openai.com>
@guocn
guocn requested a review from a team as a code owner October 22, 2026 11:05
@coveralls

coveralls commented Oct 22, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 72.577% (+0.008%) from 72.569% — guocn:fix-bcp47-duplicate-variants into go-playground:master

Comment thread bcp47_duplicate_bench_test.go Outdated

import "testing"

func BenchmarkBCP47Variants(b *testing.B) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function should be in https://github.com/go-playground/validator/blob/master/benchmarks_test.go

Please move it there.

Comment thread bcp47_duplicate_test.go Outdated

import "testing"

func TestBCP47StrictDuplicateVariants(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be in https://github.com/go-playground/validator/blob/master/validator_test.go

Please move it there

Move TestBCP47StrictDuplicateVariants into validator_test.go and
BenchmarkBCP47Variants into benchmarks_test.go per review feedback.

Prepared with AI assistance.
@guocn

guocn commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Moved TestBCP47StrictDuplicateVariants into validator_test.go and BenchmarkBCP47Variants into benchmarks_test.go, and removed the separate files (commit 231a941).

Verified: go test -run TestBCP47StrictDuplicateVariants -v ./... passes (all 10 subtests), go vet . clean, and the benchmark compiles and runs (go test -run XXX -bench BenchmarkBCP47Variants -benchtime 1x .).

Prepared with AI assistance.

@guocn

guocn commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Moved in 231a941: BenchmarkBCP47Variants now lives in benchmarks_test.go and TestBCP47StrictDuplicateVariants in validator_test.go, with the standalone files removed. Full suite passes locally (go test ./...) and CI is green on that head. Prepared with AI assistance.

@nodivbyzero
nodivbyzero merged commit 563e738 into go-playground:master Oct 28, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bcp47_strict_language_tag accepts repeated variant subtags forbidden by RFC5646

3 participants