Skip to content

Commit 669c76c

Browse files
GiteaBotwxiaoguang
andauthored
Fix cases.Title crash for concurrency (#23885) (#23903)
Backport #23885 by @wxiaoguang 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. Co-authored-by: wxiaoguang <[email protected]>
1 parent ac57ec5 commit 669c76c

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

modules/util/util.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,16 @@ func ToUpperASCII(s string) string {
185185
return string(b)
186186
}
187187

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

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

203200
var (

0 commit comments

Comments
 (0)