Skip to content

Commit

Permalink
Merge pull request #3 from nvksie/main
Browse files Browse the repository at this point in the history
fix version check with warning message output
  • Loading branch information
kiyonlin authored Nov 1, 2021
2 parents 4930e50 + 6905156 commit dfec198
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
12 changes: 8 additions & 4 deletions ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"io"
"os/exec"
"regexp"
)

// Version of current package
Expand Down Expand Up @@ -220,11 +221,14 @@ func isSupported() (bool, error) {
}

func getMajorVersion(version []byte) int {
vIndex := bytes.IndexByte(version, 'v')
dotIndex := bytes.IndexByte(version, '.')
vVer := regexp.MustCompile(" v\\d+(\\.\\d+)+").Find(version)
if vVer == nil {
return 0
}
dotIndex := bytes.IndexByte(vVer, '.')
var majorVersion int
for i := vIndex + 1; i < dotIndex; i++ {
if c := version[i]; c >= '0' && c <= '9' {
for i := 2; i < dotIndex; i++ {
if c := vVer[i]; c >= '0' && c <= '9' {
majorVersion = majorVersion*10 + int(c-'0')
} else {
return 0
Expand Down
1 change: 1 addition & 0 deletions ipset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func Test_GetMajorVersion(t *testing.T) {
{[]byte("ipset v6.29, protocol version: 6"), 6},
{[]byte("ipset v7.1, protocol version: 7"), 7},
{[]byte("ipset v10.0, protocol version: 10"), 10},
{[]byte("Warning: Kernel support protocol versions 6-6 while userspace supports protocol versions 6-7\nipset v7.1, protocol version: 7"), 7},
}

for _, tc := range tt {
Expand Down

0 comments on commit dfec198

Please sign in to comment.