From b222c1a7e5dd7c0fa594190c4d7fcd80e4253910 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Wed, 25 Nov 2020 00:44:38 -0500 Subject: [PATCH 1/2] add a type check to xpath_search --- src/xml2_xpath.cpp | 3 +++ tests/testthat/test-xml_find.R | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/xml2_xpath.cpp b/src/xml2_xpath.cpp index 378dfe82..63504cf0 100644 --- a/src/xml2_xpath.cpp +++ b/src/xml2_xpath.cpp @@ -104,6 +104,9 @@ extern "C" SEXP xpath_search(SEXP node_sxp, SEXP doc_sxp, SEXP xpath_sxp, SEXP n XPtrNode node(node_sxp); XPtrDoc doc(doc_sxp); + if (TYPEOF(xpath_sxp) != STRSXP) { + Rf_error("XPath must be a string, received %s", type2char(TYPEOF(xpath_sxp))); + } const char* xpath = CHAR(STRING_ELT(xpath_sxp, 0)); double num_results = REAL(num_results_sxp)[0]; diff --git a/tests/testthat/test-xml_find.R b/tests/testthat/test-xml_find.R index 93181f0d..f001299a 100644 --- a/tests/testthat/test-xml_find.R +++ b/tests/testthat/test-xml_find.R @@ -150,3 +150,10 @@ test_that("Searches with entities work (#241)", { expect_equal(xml_text(field1), "foo bar Quantitative Consultancy") }) + +test_that("A helpful error is given from non-string xpath in xml_find_first/all", { + node <- read_xml("1") + + expect_error(xml_find_all(node, 1), "XPath must be a string", fixed = TRUE) + expect_error(xml_find_first(node, 1), "XPath must be a string", fixed = TRUE) +}) From e34fa2f95380f89e304ff865799577b84047a3a0 Mon Sep 17 00:00:00 2001 From: Michael Chirico Date: Mon, 30 Nov 2020 09:13:18 -0500 Subject: [PATCH 2/2] add NEWS --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index 61ead47b..5d5519af 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,8 @@ * `write_xml()` and `write_html()` now return NULL invisibly, as they did prior to version 1.3.0 (#307) +* `xml_find_all.xml_node()` fails more informatively the `xpath` parameter is the wrong type (@michaelchirico) + # xml2 1.3.2 * `read_html()` and `read_xml()` now error if passed strings of length greater than one (#121)