From 81bcac04dd365e071e889f981eda7d985a3be965 Mon Sep 17 00:00:00 2001 From: wlandau Date: Sun, 10 Nov 2024 04:38:58 -0500 Subject: [PATCH] avoid out of bounds indexing in references --- R/class_reference.R | 14 +++++++++++--- tests/testthat/test-class_reference.R | 13 +++++++++++-- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/R/class_reference.R b/R/class_reference.R index 9a3d221d..b33c897b 100644 --- a/R/class_reference.R +++ b/R/class_reference.R @@ -3,15 +3,23 @@ reference_new <- function(parent, path = NULL, stage = NULL) { } reference_parent <- function(reference) { - .subset(reference, 1L) + reference[1L] } reference_path <- function(reference) { - .subset(reference, 2L) + if (length(reference) > 1L) { + reference[2L] + } else { + NA_character_ + } } reference_stage <- function(reference) { - .subset2(reference, 3L) + if (length(reference) > 2L) { + reference[3L] + } else { + NA_character_ + } } reference_produce_target <- function(reference, pipeline, name) { diff --git a/tests/testthat/test-class_reference.R b/tests/testthat/test-class_reference.R index 5945b5f7..4d36b841 100644 --- a/tests/testthat/test-class_reference.R +++ b/tests/testthat/test-class_reference.R @@ -1,14 +1,20 @@ tar_test("reference with only parent", { out <- reference_new(parent = "my_parent") - expect_equal(out, c("my_parent")) + expect_equal(out, "my_parent") + expect_equal(reference_parent(out), "my_parent") + expect_equal(reference_path(out), NA_character_) + expect_equal(reference_stage(out), NA_character_) }) tar_test("reference with parent and path but no stage", { out <- reference_new(parent = "my_parent", path = "my_path") expect_equal(out, c("my_parent", "my_path")) + expect_equal(reference_parent(out), "my_parent") + expect_equal(reference_path(out), "my_path") + expect_equal(reference_stage(out), NA_character_) }) -tar_test("reference with parent and path", { +tar_test("reference with parent, path, and stage", { out <- reference_new( "my_parent", "my_path", @@ -18,6 +24,9 @@ tar_test("reference with parent and path", { out, c("my_parent", "my_path", "my_stage") ) + expect_equal(reference_parent(out), "my_parent") + expect_equal(reference_path(out), "my_path") + expect_equal(reference_stage(out), "my_stage") }) tar_test("reference_produce_target() and its inverse", {