Skip to content

Commit ae9118d

Browse files
add a type check to xpath_search (#324)
1 parent 3993c21 commit ae9118d

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
* `write_xml()` and `write_html()` now return NULL invisibly, as they did prior to version 1.3.0 (#307)
88

9+
* `xml_find_all.xml_node()` fails more informatively the `xpath` parameter is the wrong type (@michaelchirico)
10+
911
# xml2 1.3.2
1012

1113
* `read_html()` and `read_xml()` now error if passed strings of length greater than one (#121)

src/xml2_xpath.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ extern "C" SEXP xpath_search(SEXP node_sxp, SEXP doc_sxp, SEXP xpath_sxp, SEXP n
104104

105105
XPtrNode node(node_sxp);
106106
XPtrDoc doc(doc_sxp);
107+
if (TYPEOF(xpath_sxp) != STRSXP) {
108+
Rf_error("XPath must be a string, received %s", type2char(TYPEOF(xpath_sxp)));
109+
}
107110
const char* xpath = CHAR(STRING_ELT(xpath_sxp, 0));
108111

109112
double num_results = REAL(num_results_sxp)[0];

tests/testthat/test-xml_find.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,10 @@ test_that("Searches with entities work (#241)", {
150150

151151
expect_equal(xml_text(field1), "foo bar Quantitative Consultancy")
152152
})
153+
154+
test_that("A helpful error is given from non-string xpath in xml_find_first/all", {
155+
node <- read_xml("<a>1</a>")
156+
157+
expect_error(xml_find_all(node, 1), "XPath must be a string", fixed = TRUE)
158+
expect_error(xml_find_first(node, 1), "XPath must be a string", fixed = TRUE)
159+
})

0 commit comments

Comments
 (0)