From 5fb79ddb44f1cc12269e38f472d129fff93b4f35 Mon Sep 17 00:00:00 2001 From: zgyzgyhero <894526647@qq.com> Date: Tue, 15 Oct 2024 10:03:03 +0800 Subject: [PATCH] =?UTF-8?q?#831=20=E6=9B=B4=E6=96=B0pcre=E6=AD=A3=E5=88=99?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E6=8A=A5=E9=94=99=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- monitor-agent/node_exporter/VERSION | 5 ++- monitor-agent/node_exporter/collector/pcre.go | 41 ++++++++++++------- monitor-server/go.mod | 2 +- monitor-server/go.sum | 2 + .../go-common-lib/pcre/regexp.go | 41 ++++++++++++------- monitor-server/vendor/modules.txt | 2 +- 6 files changed, 62 insertions(+), 31 deletions(-) diff --git a/monitor-agent/node_exporter/VERSION b/monitor-agent/node_exporter/VERSION index 7cc19625c..0895ec796 100644 --- a/monitor-agent/node_exporter/VERSION +++ b/monitor-agent/node_exporter/VERSION @@ -40,4 +40,7 @@ v3.1.0 Update control shell to support custom exporter port v3.2.0 -Fix log tail chanel closed when handler check active && modify tail reopen \ No newline at end of file +Fix log tail chanel closed when handler check active && modify tail reopen + +v3.2.5 +Fix pcre match panic error \ No newline at end of file diff --git a/monitor-agent/node_exporter/collector/pcre.go b/monitor-agent/node_exporter/collector/pcre.go index 68b5fd9d5..c5592fc3a 100644 --- a/monitor-agent/node_exporter/collector/pcre.go +++ b/monitor-agent/node_exporter/collector/pcre.go @@ -9,6 +9,7 @@ package collector import "C" import ( + "fmt" "strconv" "unsafe" ) @@ -53,6 +54,8 @@ const ( PARTIAL_SOFT = C.PCRE_PARTIAL_SOFT ) +var pcrePanicErr string = "pcre error: %s\n" + // A reference to a compiled regular expression. // Use Compile or MustCompile to create such objects. type Regexp struct { @@ -109,10 +112,12 @@ func PcreCompile(pattern string, flags int) (Regexp, *CompileError) { } // Compile the pattern. If compilation fails, panic. -func MustPcreCompile(pattern string, flags int) (re Regexp) { - re, err := PcreCompile(pattern, flags) - if err != nil { - panic(err) +func MustPcreCompile(pattern string, flags int) (re Regexp, err error) { + compileRe, compileErr := PcreCompile(pattern, flags) + if compileErr != nil { + err = fmt.Errorf(compileErr.String()) + } else { + re = compileRe } return } @@ -120,7 +125,8 @@ func MustPcreCompile(pattern string, flags int) (re Regexp) { // Returns the number of capture groups in the compiled pattern. func (re Regexp) Groups() int { if re.ptr == nil { - panic("Regexp.Groups: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Groups: uninitialized") + return 0 } return int(pcregroups((*C.pcre)(unsafe.Pointer(&re.ptr[0])))) } @@ -155,7 +161,8 @@ func (re Regexp) MatcherString(subject string, flags int) (m *Matcher) { // Switches the matcher object to the specified pattern and subject. func (m *Matcher) Reset(re Regexp, subject []byte, flags int) { if re.ptr == nil { - panic("Regexp.Matcher: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Matcher: uninitialized") + return } m.init(re) m.Match(subject, flags) @@ -165,7 +172,8 @@ func (m *Matcher) Reset(re Regexp, subject []byte, flags int) { // string. func (m *Matcher) ResetString(re Regexp, subject string, flags int) { if re.ptr == nil { - panic("Regexp.Matcher: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Matcher: uninitialized") + return } m.init(re) m.MatchString(subject, flags) @@ -192,7 +200,8 @@ var nullbyte = []byte{0} // pattern. Returns true if the match succeeds. func (m *Matcher) Match(subject []byte, flags int) bool { if m.re.ptr == nil { - panic("Matcher.Match: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Matcher: uninitialized") + return false } length := len(subject) m.subjects = "" @@ -208,7 +217,8 @@ func (m *Matcher) Match(subject []byte, flags int) bool { // Returns true if the match succeeds. func (m *Matcher) MatchString(subject string, flags int) bool { if m.re.ptr == nil { - panic("Matcher.Match: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Matcher: uninitialized") + return false } length := len(subject) m.subjects = subject @@ -233,10 +243,11 @@ func (m *Matcher) match(subjectptr *C.char, length, flags int) bool { m.matches = false return false case rc == C.PCRE_ERROR_BADOPTION: - panic("PCRE.Match: invalid option flag") + fmt.Printf(pcrePanicErr, "match func invalid option flag") + return false } - panic("unexepected return code from pcre_exec: " + - strconv.Itoa(int(rc))) + fmt.Printf(pcrePanicErr, "unexepected return code from pcre_exec: "+strconv.Itoa(int(rc))) + return false } // Returns true if a previous call to Matcher, MatcherString, Reset, @@ -293,14 +304,16 @@ func (m *Matcher) GroupString(group int) string { func (m *Matcher) name2index(name string) (group int) { if m.re.ptr == nil { - panic("Matcher.Named: uninitialized") + fmt.Printf(pcrePanicErr, "Matcher.Named: uninitialized") + return 0 } name1 := C.CString(name) defer C.free(unsafe.Pointer(name1)) group = int(C.pcre_get_stringnumber( (*C.pcre)(unsafe.Pointer(&m.re.ptr[0])), name1)) if group < 0 { - panic("Matcher.Named: unknown name: " + name) + fmt.Printf(pcrePanicErr, "Matcher.Named: unknown name: "+name) + return 0 } return } diff --git a/monitor-server/go.mod b/monitor-server/go.mod index 37b90803a..622c2a340 100644 --- a/monitor-server/go.mod +++ b/monitor-server/go.mod @@ -4,7 +4,7 @@ go 1.19 require ( github.com/360EntSecGroup-Skylar/excelize v1.4.1 - github.com/WeBankPartners/go-common-lib v1.1.5 + github.com/WeBankPartners/go-common-lib v1.1.6 github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/dlclark/regexp2 v1.10.0 github.com/gin-gonic/gin v1.8.1 diff --git a/monitor-server/go.sum b/monitor-server/go.sum index 6fa214d17..9883bcaee 100644 --- a/monitor-server/go.sum +++ b/monitor-server/go.sum @@ -11,6 +11,8 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/WeBankPartners/go-common-lib v1.1.5 h1:hrFieMX0ptqYRgPv7WM+G5do5x9W9Mp1qI5Kxyi9OVo= github.com/WeBankPartners/go-common-lib v1.1.5/go.mod h1:s6S7m/RloGFfY+Twq0hurp8kNZz9GtlbmKMRYWYdFaI= +github.com/WeBankPartners/go-common-lib v1.1.6 h1:OGja6eqLgVVMLvwMsy94TkWrmfg9flrqMhJyHCNCDKg= +github.com/WeBankPartners/go-common-lib v1.1.6/go.mod h1:s6S7m/RloGFfY+Twq0hurp8kNZz9GtlbmKMRYWYdFaI= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/monitor-server/vendor/github.com/WeBankPartners/go-common-lib/pcre/regexp.go b/monitor-server/vendor/github.com/WeBankPartners/go-common-lib/pcre/regexp.go index 17136dc8d..5ae30eb0e 100644 --- a/monitor-server/vendor/github.com/WeBankPartners/go-common-lib/pcre/regexp.go +++ b/monitor-server/vendor/github.com/WeBankPartners/go-common-lib/pcre/regexp.go @@ -9,6 +9,7 @@ package pcre import "C" import ( + "fmt" "strconv" "unsafe" ) @@ -53,6 +54,8 @@ const ( PARTIAL_SOFT = C.PCRE_PARTIAL_SOFT ) +var pcrePanicErr string = "pcre error: %s\n" + // A reference to a compiled regular expression. // Use Compile or MustCompile to create such objects. type Regexp struct { @@ -109,10 +112,12 @@ func Compile(pattern string, flags int) (Regexp, *CompileError) { } // Compile the pattern. If compilation fails, panic. -func MustCompile(pattern string, flags int) (re Regexp) { - re, err := Compile(pattern, flags) - if err != nil { - panic(err) +func MustCompile(pattern string, flags int) (re Regexp, err error) { + compileRe, compileErr := Compile(pattern, flags) + if compileErr != nil { + err = fmt.Errorf(compileErr.String()) + } else { + re = compileRe } return } @@ -120,7 +125,8 @@ func MustCompile(pattern string, flags int) (re Regexp) { // Returns the number of capture groups in the compiled pattern. func (re Regexp) Groups() int { if re.ptr == nil { - panic("Regexp.Groups: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Groups: uninitialized") + return 0 } return int(pcregroups((*C.pcre)(unsafe.Pointer(&re.ptr[0])))) } @@ -155,7 +161,8 @@ func (re Regexp) MatcherString(subject string, flags int) (m *Matcher) { // Switches the matcher object to the specified pattern and subject. func (m *Matcher) Reset(re Regexp, subject []byte, flags int) { if re.ptr == nil { - panic("Regexp.Matcher: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Matcher: uninitialized") + return } m.init(re) m.Match(subject, flags) @@ -165,7 +172,8 @@ func (m *Matcher) Reset(re Regexp, subject []byte, flags int) { // string. func (m *Matcher) ResetString(re Regexp, subject string, flags int) { if re.ptr == nil { - panic("Regexp.Matcher: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Matcher: uninitialized") + return } m.init(re) m.MatchString(subject, flags) @@ -192,7 +200,8 @@ var nullbyte = []byte{0} // pattern. Returns true if the match succeeds. func (m *Matcher) Match(subject []byte, flags int) bool { if m.re.ptr == nil { - panic("Matcher.Match: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Matcher: uninitialized") + return false } length := len(subject) m.subjects = "" @@ -208,7 +217,8 @@ func (m *Matcher) Match(subject []byte, flags int) bool { // Returns true if the match succeeds. func (m *Matcher) MatchString(subject string, flags int) bool { if m.re.ptr == nil { - panic("Matcher.Match: uninitialized") + fmt.Printf(pcrePanicErr, "Regexp.Matcher: uninitialized") + return false } length := len(subject) m.subjects = subject @@ -233,10 +243,11 @@ func (m *Matcher) match(subjectptr *C.char, length, flags int) bool { m.matches = false return false case rc == C.PCRE_ERROR_BADOPTION: - panic("PCRE.Match: invalid option flag") + fmt.Printf(pcrePanicErr, "match func invalid option flag") + return false } - panic("unexepected return code from pcre_exec: " + - strconv.Itoa(int(rc))) + fmt.Printf(pcrePanicErr, "unexepected return code from pcre_exec: "+strconv.Itoa(int(rc))) + return false } // Returns true if a previous call to Matcher, MatcherString, Reset, @@ -293,14 +304,16 @@ func (m *Matcher) GroupString(group int) string { func (m *Matcher) name2index(name string) (group int) { if m.re.ptr == nil { - panic("Matcher.Named: uninitialized") + fmt.Printf(pcrePanicErr, "Matcher.Named: uninitialized") + return 0 } name1 := C.CString(name) defer C.free(unsafe.Pointer(name1)) group = int(C.pcre_get_stringnumber( (*C.pcre)(unsafe.Pointer(&m.re.ptr[0])), name1)) if group < 0 { - panic("Matcher.Named: unknown name: " + name) + fmt.Printf(pcrePanicErr, "Matcher.Named: unknown name: "+name) + return 0 } return } diff --git a/monitor-server/vendor/modules.txt b/monitor-server/vendor/modules.txt index 0841ca0be..19f7b9f2a 100644 --- a/monitor-server/vendor/modules.txt +++ b/monitor-server/vendor/modules.txt @@ -10,7 +10,7 @@ github.com/PuerkitoBio/purell # github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 ## explicit github.com/PuerkitoBio/urlesc -# github.com/WeBankPartners/go-common-lib v1.1.5 +# github.com/WeBankPartners/go-common-lib v1.1.6 ## explicit; go 1.17 github.com/WeBankPartners/go-common-lib/cipher github.com/WeBankPartners/go-common-lib/guid