Skip to content

Commit 5fc9929

Browse files
authored
Fix cases.Title crash for concurrency (#23885)
Regression of #19676 and #21814 Fix #23872 `cases.Title` is not thread-safe, it has internal state, so it can't be used as a global shared variable.
1 parent cb6ed84 commit 5fc9929

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

modules/util/util.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,16 @@ func ToUpperASCII(s string) string {
186186
return string(b)
187187
}
188188

189-
var (
190-
titleCaser = cases.Title(language.English)
191-
titleCaserNoLower = cases.Title(language.English, cases.NoLower)
192-
)
193-
194189
// ToTitleCase returns s with all english words capitalized
195190
func ToTitleCase(s string) string {
196-
return titleCaser.String(s)
191+
// `cases.Title` is not thread-safe, do not use global shared variable for it
192+
return cases.Title(language.English).String(s)
197193
}
198194

199-
// ToTitleCaseNoLower returns s with all english words capitalized without lowercasing
195+
// ToTitleCaseNoLower returns s with all english words capitalized without lower-casing
200196
func ToTitleCaseNoLower(s string) string {
201-
return titleCaserNoLower.String(s)
197+
// `cases.Title` is not thread-safe, do not use global shared variable for it
198+
return cases.Title(language.English, cases.NoLower).String(s)
202199
}
203200

204201
func logError(msg string, args ...any) {

0 commit comments

Comments
 (0)