Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ func toEnvName(input string) string {
if unicode.IsLower(peek) || unicode.IsLower(rune(input[i-1])) {
output = append(output, underscore)
}
} else if i > 0 && unicode.IsLower(rune(input[i-1])) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Medium: Check the preceding rune, not its final UTF-8 byte

With an exported field ƵB string, UseFieldNameByDefault: true, and Environment: map[string]string{"ƵB": "set"}, this change makes parsing silently leave the field empty instead of loading "set". The generated key changes from ƵB on the base to Ƶ_B here.

The range index i is a byte offset. Ƶ is uppercase U+01B5, encoded as C6 B5; at the terminal B, input[i-1] is 0xB5. Casting that byte to a rune produces lowercase U+00B5 (µ), so the new condition incorrectly splits an uppercase run. I verified the bytes independently, checked Go's Unicode classifications, and traced the generated key through parseFieldParams and the environment lookup; no local Go build or test was run.

Decode the actual preceding rune in this added branch, for example with utf8.DecodeLastRuneInString(input[:i]), before calling unicode.IsLower. Add toEnvName("ƵB") == "ƵB" and a parsing regression with the environment map above, retaining the MyA regression.

output = append(output, underscore)
}
}
output = append(output, unicode.ToUpper(c))
Expand Down
17 changes: 17 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,20 @@ func TestNoEnvKey(t *testing.T) {
isEqual(t, "", cfg.bar)
}

func TestUseFieldNameTrailingCapital(t *testing.T) {
type Config struct {
MyA string
}
var cfg Config
isNoErr(t, ParseWithOptions(&cfg, Options{
UseFieldNameByDefault: true,
Environment: map[string]string{
"MY_A": "set",
},
}))
isEqual(t, "set", cfg.MyA)
}

func TestToEnv(t *testing.T) {
for in, out := range map[string]string{
"Foo": "FOO",
Expand All @@ -1739,6 +1753,9 @@ func TestToEnv(t *testing.T) {
"SSHPort": "SSH_PORT",
"_SSH___Port_": "SSH_PORT",
"_PortHTTP": "PORT_HTTP",
"MyA": "MY_A",
"FooB": "FOO_B",
"AbC": "AB_C",
} {
t.Run(in, func(t *testing.T) {
isEqual(t, out, toEnvName(in))
Expand Down
Loading