From 1903cbac5819b9999464ad3139aac52fa60a24e8 Mon Sep 17 00:00:00 2001 From: alexverse Date: Tue, 11 Jul 2023 19:54:08 +0000 Subject: [PATCH] Fix xml_add_parent segfault --- R/xml_modify.R | 2 +- tests/testthat/test-modify-xml.R | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/R/xml_modify.R b/R/xml_modify.R index b8cf7034..86016a02 100644 --- a/R/xml_modify.R +++ b/R/xml_modify.R @@ -210,7 +210,7 @@ xml_add_parent <- function(.x, .value, ...) { #' @export xml_add_parent.xml_node <- function(.x, .value, ...) { - new_parent <- xml_replace(.x, .value = .value, ..., .copy = FALSE) + new_parent <- xml_replace(.x, .value = .value, ..., .copy = TRUE) node <- xml_add_child(new_parent, .value = .x, .copy = FALSE) invisible(node) diff --git a/tests/testthat/test-modify-xml.R b/tests/testthat/test-modify-xml.R index 0fa0e8a7..59780f62 100644 --- a/tests/testthat/test-modify-xml.R +++ b/tests/testthat/test-modify-xml.R @@ -194,6 +194,17 @@ test_that("xml_add_parent works with xml_missing input", { expect_equal(xml_name(xml_children(y)), c("em", "em")) }) +test_that("xml_add_parent not generating segfault during iteration", { + add_parent <- function() { + fruits <- read_xml(" + + ") + xml_add_parent(fruits, read_xml("")) + } + + expect_no_error(for(i in 1:100) add_parent()) +}) + test_that("xml_new_document adds a default character encoding", { x <- read_xml("\u00E1\u00FC\u00EE")