Skip to content

Commit b4afceb

Browse files
committed
fix: incorrect checking of strings.Split return value
strings.Split will never return a slice of length 0 if sep is not empty, so any code that checks if the return value is of length 0 is incorrect and useless.
1 parent 166392f commit b4afceb

File tree

5 files changed

+36
-15
lines changed

5 files changed

+36
-15
lines changed

component/geodata/utils.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package geodata
22

33
import (
4-
"errors"
54
"fmt"
65
"strings"
76

@@ -76,13 +75,13 @@ func LoadGeoSiteMatcher(countryCode string) (router.DomainMatcher, error) {
7675
if countryCode[0] == '!' {
7776
not = true
7877
countryCode = countryCode[1:]
78+
if countryCode == "" {
79+
return nil, fmt.Errorf("country code could not be empty")
80+
}
7981
}
8082
countryCode = strings.ToLower(countryCode)
8183

8284
parts := strings.Split(countryCode, "@")
83-
if len(parts) == 0 {
84-
return nil, errors.New("empty rule")
85-
}
8685
listName := strings.TrimSpace(parts[0])
8786
attrVal := parts[1:]
8887
attrs := parseAttrs(attrVal)

config/config.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,8 +1174,6 @@ func parseNameServer(servers []string, respectRules bool, preferH3 bool) ([]dns.
11741174
for _, s := range strings.Split(u.Fragment, "&") {
11751175
arr := strings.SplitN(s, "=", 2)
11761176
switch len(arr) {
1177-
case 0:
1178-
continue
11791177
case 1:
11801178
proxyName = arr[0]
11811179
case 2:

rules/common/in_name.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package common
22

33
import (
44
"fmt"
5-
C "github.com/metacubex/mihomo/constant"
65
"strings"
6+
7+
C "github.com/metacubex/mihomo/constant"
78
)
89

910
type InName struct {
@@ -35,10 +36,17 @@ func (u *InName) Payload() string {
3536
}
3637

3738
func NewInName(iNames, adapter string) (*InName, error) {
38-
names := strings.Split(iNames, "/")
39-
if len(names) == 0 {
39+
if len(iNames) == 0 {
4040
return nil, fmt.Errorf("in name couldn't be empty")
4141
}
42+
names := strings.Split(iNames, "/")
43+
for i, name := range names {
44+
name = strings.TrimSpace(name)
45+
if len(name) == 0 {
46+
return nil, fmt.Errorf("in name couldn't be empty")
47+
}
48+
names[i] = name
49+
}
4250

4351
return &InName{
4452
Base: &Base{},

rules/common/in_type.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package common
22

33
import (
44
"fmt"
5-
C "github.com/metacubex/mihomo/constant"
65
"strings"
6+
7+
C "github.com/metacubex/mihomo/constant"
78
)
89

910
type InType struct {
@@ -35,10 +36,17 @@ func (u *InType) Payload() string {
3536
}
3637

3738
func NewInType(iTypes, adapter string) (*InType, error) {
38-
types := strings.Split(iTypes, "/")
39-
if len(types) == 0 {
39+
if len(iTypes) == 0 {
4040
return nil, fmt.Errorf("in type couldn't be empty")
4141
}
42+
types := strings.Split(iTypes, "/")
43+
for i, tp := range types {
44+
tp = strings.TrimSpace(tp)
45+
if len(tp) == 0 {
46+
return nil, fmt.Errorf("in type couldn't be empty")
47+
}
48+
types[i] = tp
49+
}
4250

4351
tps, err := parseInTypes(types)
4452
if err != nil {

rules/common/in_user.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package common
22

33
import (
44
"fmt"
5-
C "github.com/metacubex/mihomo/constant"
65
"strings"
6+
7+
C "github.com/metacubex/mihomo/constant"
78
)
89

910
type InUser struct {
@@ -35,10 +36,17 @@ func (u *InUser) Payload() string {
3536
}
3637

3738
func NewInUser(iUsers, adapter string) (*InUser, error) {
38-
users := strings.Split(iUsers, "/")
39-
if len(users) == 0 {
39+
if len(iUsers) == 0 {
4040
return nil, fmt.Errorf("in user couldn't be empty")
4141
}
42+
users := strings.Split(iUsers, "/")
43+
for i, user := range users {
44+
user = strings.TrimSpace(user)
45+
if len(user) == 0 {
46+
return nil, fmt.Errorf("in user couldn't be empty")
47+
}
48+
users[i] = user
49+
}
4250

4351
return &InUser{
4452
Base: &Base{},

0 commit comments

Comments
 (0)