Skip to content

data.table corrupts variables by failing to check NAMED before modifying a vector #2984

@radfordneal

Description

@radfordneal

data.table can fail to check that NAMED is 0 before changing a vector, thereby corrupting variables. Below is a specific example, but I suspect that the problem occurs widely in the source code for data.table (based on various attempts with various versions of data.table to get it to work with pqR).

This example is adapted from data.table test number 430 (which crashes when run in pqR because in pqR 3L is a read-only constant). Changing the test to use a variable set to 3L rather than the constant 3L reveals the problem in R-3.5.1 as well (and I expect in all versions of R).

library(data.table)
# data.table 1.11.4  Latest news: http://r-datatable.com
DT <- data.table(a = factor(c("A", "Z")), b = 1:4)
a <- 3L
DT[1,1]<- a 
#  Warning message:
#  In `[<-.data.table`(`*tmp*`, 1, 1, value = NA_integer_) :
#    RHS contains 3 which is outside the levels range ([1,2]) of column 1, NAs generated
a
# [1] NA     ---- This should still be3L, not changed to NA!
sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: OS X El Capitan 10.11.6

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib

locale:
[1] C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] data.table_1.11.4

loaded via a namespace (and not attached):
[1] compiler_3.5.1

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions