Skip to content

abort() called when passing NULL as parameter #1177

@jrgfogh

Description

@jrgfogh

R crashes when I pass NULL to a function taking a StringVector, when the function is compiled as part of a package.

I have tracked the cause to r_cast.h in r_true_cast<>():

#ifndef NDEBUG
            REprintf(fmt, Rf_type2char(TYPEOF(x)));
            abort();
#else
            throw ::Rcpp::not_compatible(fmt, Rf_type2char(TYPEOF(x)));
#endif

The issue does not occur when I compile the same function using cppFunction() or sourceCpp(), presumably because these
functions define NDEBUG.

MCVE:

Run:

library(usethis)

create_package('tmp')
use_rcpp()

Paste the following code into src/code.cpp:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
RObject crash(StringVector v)
{
    auto x = 10;
    return wrap(x);
}

Paste the following code into R/tmp-package.R:

## usethis namespace: start
#' @exportPattern "^[^\\.]"
#' @useDynLib tmp, .registration = TRUE
#' @importFrom Rcpp sourceCpp
## usethis namespace: end
NULL

Now run:

devtools::document()
devtools::load_all('.')
tmp::crash(NULL)

Output from sessionInfo():

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=Danish_Denmark.1252  LC_CTYPE=Danish_Denmark.1252    LC_MONETARY=Danish_Denmark.1252
[4] LC_NUMERIC=C                    LC_TIME=Danish_Denmark.1252    

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

other attached packages:
[1] usethis_1.6.1        RevoUtils_11.0.2     RevoUtilsMath_11.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5       rstudioapi_0.11  knitr_1.29       xml2_1.3.2       magrittr_1.5     roxygen2_7.1.1   R6_2.3.0        
 [8] rlang_0.4.6      fansi_0.4.1      stringr_1.4.0    tools_4.0.2      xfun_0.15        cli_2.0.2        git2r_0.27.1    
[15] ellipsis_0.3.1   rprojroot_1.3-2  assertthat_0.2.1 tibble_3.0.2     lifecycle_0.2.0  crayon_1.3.4     purrr_0.3.4     
[22] vctrs_0.3.1      fs_1.4.2         glue_1.4.1       stringi_1.4.6    compiler_4.0.2   pillar_1.4.6     desc_1.2.0      
[29] backports_1.1.7  pkgconfig_2.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions