cli: make bool parsing case-sensitive

Completes the boolean fix: only literal "true"/"false" are accepted, with no
case folding, so "True"/"TRUE" are rejected. Drop strings.ToLower. Keep
TrimSpace so a config value with surrounding whitespace still parses; only the
case folding was invented leniency.
This commit is contained in:
2026-06-21 15:52:42 +09:00
parent 10672f3816
commit 6c045999f2
2 changed files with 10 additions and 5 deletions

View File

@@ -38,8 +38,11 @@ var boolWords = map[string]bool{
} }
// boolWord reports a value's truth and whether it is a recognised boolean word. // boolWord reports a value's truth and whether it is a recognised boolean word.
// Whitespace is trimmed (aria2 strips config values too), but case is
// significant: aria2 rejects "True"/"TRUE", so the match is against exactly
// "true"/"false".
func boolWord(s string) (val, ok bool) { func boolWord(s string) (val, ok bool) {
val, ok = boolWords[strings.ToLower(strings.TrimSpace(s))] val, ok = boolWords[strings.TrimSpace(s)]
return val, ok return val, ok
} }

View File

@@ -139,14 +139,16 @@ func TestValidateFloatNonNegative(t *testing.T) {
func TestValidateBool(t *testing.T) { func TestValidateBool(t *testing.T) {
b := byLong["enable-dht"] b := byLong["enable-dht"]
// aria2 accepts only true/false; the invented yes/no/1/0/on/off spellings // aria2 accepts only literal true/false (case-sensitive) but trims
// were removed, so they must now fail validation. // surrounding whitespace; match that exactly.
for _, ok := range []string{"true", "false"} { for _, ok := range []string{"true", "false", " true ", "false\t"} {
if err := validate(b, ok); err != nil { if err := validate(b, ok); err != nil {
t.Errorf("validate bool %q: %v", ok, err) t.Errorf("validate bool %q: %v", ok, err)
} }
} }
for _, bad := range []string{"yes", "no", "1", "0", "on", "off", "flase"} { // The invented spellings and any case variant must now fail.
for _, bad := range []string{"yes", "no", "1", "0", "on", "off", "flase",
"True", "TRUE", "tRuE", "False", "FALSE"} {
if err := validate(b, bad); err == nil { if err := validate(b, bad); err == nil {
t.Errorf("validate bool %q: want error (only true/false accepted)", bad) t.Errorf("validate bool %q: want error (only true/false accepted)", bad)
} }