Skip to content

Compile didn't report error in predicate #101

Description

@freesrz93

In some cases Compile didn't report error in the predicate, which caused a runtime panic later during a query.

  • Version:
github.com/antchfx/htmlquery v1.3.2
github.com/antchfx/xpath v1.3.2-0.20240724042431-14e235f5e127
  • A minimal example:
package main

import (
	"strings"

	"github.com/antchfx/htmlquery"
	"github.com/antchfx/xpath"
	"golang.org/x/net/html"
)

func main() {
	h := `<div>
<span></span>
<span>v</span>
</div>
`
	doc, err := html.Parse(strings.NewReader(h))
	if err != nil {
		return
	}

	query(doc, `//*[contains(normalize-space(),"v")]//text()`) // panic
	query(doc, `//*[contains(normalize-space(),"v")]`)         // print error
	query(doc, `//*[contains(normalize-space(.),"v")]`)        // correct
}

func query(n *html.Node, expr string) {
	p, err := xpath.Compile(expr)
	if err != nil {
		println(err.Error())
		return
	}

	htmlquery.QuerySelector(n, p)
}

The first two xpaths miss parameter of function normalize-space, but only the second one report an error, and the first one will panic.

By the way, I think normalize-space() should be equal to normalize-space(.) according to the specification

I didn't dive into the codes, but the debugger reported

xpath/build.go

Line 109 in 14e235f

qyGrandInput, _ = b.processNode(input.Input, flagsEnum.SmartDesc, props)

This assignment discarded the error.

I don't know whether this is expected, but I think it makes Compile unreliable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions