Skip to content

Commit

Permalink
#831 更新pcre正则匹配报错处理
Browse files Browse the repository at this point in the history
  • Loading branch information
zgyzgyhero committed Oct 15, 2024
1 parent 7d68b24 commit 5fb79dd
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 31 deletions.
5 changes: 4 additions & 1 deletion monitor-agent/node_exporter/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Fix log tail chanel closed when handler check active && modify tail reopen

v3.2.5
Fix pcre match panic error
41 changes: 27 additions & 14 deletions monitor-agent/node_exporter/collector/pcre.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package collector
import "C"

import (
"fmt"
"strconv"
"unsafe"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -109,18 +112,21 @@ 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
}

// 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]))))
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 = ""
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion monitor-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions monitor-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion monitor-server/vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5fb79dd

Please sign in to comment.