The following code causes segfauls on R3.4.4.
library(data.table)
tbl <- data.table(A = "a", key = "A")
tbl[J(NULL)]
Update: the segfault is triggered by Cbmerge and I confirm data.table v1.12.3 also has this issue.
It's cause by this line:
When i is NULL, it will be converted to zero column data.table() first. Since i is now a zero column data.table object, the VECTOR_ELT(i,0) doesn't exist and thus LENGTH(VECTOR_ELT(i,0)) returns garbage values.
I don't know why it can't happen in R3.5 or R3.6 but I will file a PR later anyway.
UPDATE: This is because the below code returns 0 on R3.5 or R3.6 but may be any garbage values on R3.4. I don't know the reason.
Rcpp::cppFunction("int getlength(SEXP x) { return LENGTH(VECTOR_ELT(x,0)); }")
getlength(list())
Most helpful comment
It's cause by this line:
https://github.com/Rdatatable/data.table/blob/6f17d180ed52d517118b9df566bf7d4407754956/src/bmerge.c#L54
When i is NULL, it will be converted to zero column data.table() first. Since i is now a zero column data.table object, the
VECTOR_ELT(i,0)doesn't exist and thusLENGTH(VECTOR_ELT(i,0))returns garbage values.I don't know why it can't happen in R3.5 or R3.6 but I will file a PR later anyway.
UPDATE: This is because the below code returns 0 on R3.5 or R3.6 but may be any garbage values on R3.4. I don't know the reason.