antchfx / htmlquery

htmlquery is golang XPath package for HTML query.
https://github.com/antchfx/xpath
MIT License
738 stars 74 forks source link

Randomly panics ! #50

Closed Jisin0 closed 2 years ago

Jisin0 commented 2 years ago
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x28 pc=0x90f01c]
goroutine 1427 [running]:
github.com/antchfx/htmlquery.(*NodeNavigator).NodeType(0xc0009a0d50?)
/tmp/codon/tmp/cache/go-path/pkg/mod/github.com/antchfx/htmlquery@v1.2.5/query.go:213 +0x1c
github.com/antchfx/xpath.axisPredicate.func1({0xbc3040, 0xc000507ef0})
/tmp/codon/tmp/cache/go-path/pkg/mod/github.com/antchfx/xpath@v1.2.1/build.go:45 +0x47
github.com/antchfx/xpath.(*selfQuery).Select(0xc000507ed8, {0xbbd8e0, 0xc000070d60})
/tmp/codon/tmp/cache/go-path/pkg/mod/github.com/antchfx/xpath@v1.2.1/query.go:523 +0x65
github.com/antchfx/xpath.(*childQuery).Select(0xc0009a1020, {0xbbd8e0, 0xc000070d60})
/tmp/codon/tmp/cache/go-path/pkg/mod/github.com/antchfx/xpath@v1.2.1/query.go:181 +0x62
github.com/antchfx/xpath.(*filterQuery).Select(0xc0009a1050, {0xbbd8e0, 0xc000070d60})
/tmp/codon/tmp/cache/go-path/pkg/mod/github.com/antchfx/xpath@v1.2.1/query.go:578 +0x83
github.com/antchfx/xpath.(*childQuery).Select(0xc0009a1080, {0xbbd8e0, 0xc000070d60}).
/tmp/codon/tmp/cache/go-path/pkg/mod/github.com/antchfx/xpath@v1.2.1/query.go:181 +0x62
 github.com/antchfx/xpath.(*NodeIterator).MoveNext(0xc000070d60)
 /tmp/codon/tmp/cache/go-path/pkg/mod/github.com/antchfx/xpath@v1.2.1/xpath.go:86 +0x38
github.com/antchfx/htmlquery.QuerySelector(0x0, 0xc0000b5340)
 /tmp/codon/tmp/cache/go-path/pkg/mod/github.com/antchfx/htmlquery@v1.2.5/query.go:73 +0xf5
 github.com/antchfx/htmlquery.Query(0xc000917730?, {0xa485b5?, 0xc000917730?})
 /tmp/codon/tmp/cache/go-path/pkg/mod/github.com/antchfx/htmlquery@v1.2.5/query.go:67 +0x47

The library randomly panics killing my whole script, I have no way to reproduce it because its totally random and comes at any time. It comes up when calling the InnerText() method. Please stop it from panicking.

zhengchun commented 2 years ago

Hello, Please checking matching node is not nil before calling InnerText().

node := htmlquery.Find(doc,"//a")
if node != nil {  -- ADD THIS before calling InnerText()
  val := htmlquery.InnerText(node)
}

see https://github.com/antchfx/htmlquery/issues/38