Repository navigation
Closed
Description
We get this warning
❯ checking compiled code ... WARNING
File ‘tskitr/libs/tskitr.so’:
Found ‘___assert_rtn’, possibly from ‘assert’ (C)
Objects: ‘tskit/haplotype_matching.o’, ‘tskit/kastore.o’,
‘tskit/tables.o’, ‘tskit/trees.o’
Found ‘___stderrp’, possibly from ‘stderr’ (C)
Objects: ‘tskit/core.o’, ‘tskit/genotypes.o’,
‘tskit/haplotype_matching.o’, ‘tskit/tables.o’, ‘tskit/trees.o’
Found ‘___stdoutp’, possibly from ‘stdout’ (C)
Object: ‘tskit/core.o’
Found ‘_abort’, possibly from ‘abort’ (C)
Objects: ‘tskit/core.o’, ‘tskit/genotypes.o’,
‘tskit/haplotype_matching.o’, ‘tskit/tables.o’, ‘tskit/trees.o’
Compiled code should not call entry points which might terminate R nor
write to stdout/stderr instead of to the console, nor use Fortran I/O
nor system RNGs nor [v]sprintf.
See ‘Writing portable packages’ in the ‘Writing R Extensions’ manual.
Under no circumstances should your compiled code ever call abort or exit85: these terminate the user’s R process, quite possibly losing all unsaved work. One usage that could call abort is the assert macro in C or C++ functions, which should never be active in production code. The normal way to ensure that is to define the macro NDEBUG, and R CMD INSTALL does so as part of the compilation flags. Beware of including headers (including from other packages) which could undefine it, now or in future versions. If you wish to use assert during development, you can include -UNDEBUG in PKG_CPPFLAGS or #undef it in your headers or code files. Note that your own src/Makefile or makefiles in sub-directories may also need to define NDEBUG.
This applies not only to your own code but to any external software you compile in or link to.
Compiled code should not write to stdout or stderr and C++ and Fortran I/O should not be used. As with the previous item such calls may come from external software and may never be called, but package authors are often mistaken about that.
Compiled code should not call the system random number generators such as rand, drand48 and random86, but rather use the interfaces to R’s RNGs described in Random number generation. In particular, if more than one package initializes a system RNG (e.g. via srand), they will interfere with each other. This applies also to Fortran 90’s random_number and random_seed, and Fortran 2018’s random_init. And to GNU Fortran’s rand, irand and srand. Except for drand48, what PRNG these functions use is implementation-dependent.
Nor should the C++11 random number library be used nor any other third-party random number generators such as those in GSL.
Use of sprintf and vsprintf is regarded as a potential security risk and warned about on some platforms.87 R CMD check reports if any calls are found.
Could use PKG_CFLAGS = -DNDEBUG to remove asserts as per Writing R extensions manual, but this will not address stdout/stderr and print issues!
If the flag TSK_TRACE_ERRORS is defined (by e.g. -DTSK_TRACE_ERRORS to gcc), then error messages will be printed to stderr when errors occur. This also allows breakpoints to be set in the _tsk_trace_error function to break on all errors.
A critical element of producing reliable C programs is consistent error handling and checking of return values. All return values must be checked! In tskit, all functions (except the most trivial accessors) return an integer to indicate success or failure. Any negative value is an error, and must be handled accordingly.
There are two different ways to express assertions in tskit code. The first is using the custom tsk_bug_assert macro, which is used to make inexpensive checks at key points during execution. These assertions are always run, regardless of the compiler settings, and should not contribute significantly to the overall runtime.
More expensive assertions, used, for example, to check pre and post conditions on performance critical loops should be expressed using the standard assert macro from assert.h. These assertions will be checked during the execution of C unit tests, but will not be enabled when compiled into the Python C module.
Metadata
Metadata
Assignees
Labels
No labels
Activity
[-]R CMD check checking compiled code ... WARNING[/-][+]Handle R CMD check regarding assert, stderr, stdout, and abort[/+]Using DNDEBUG to remove asserts #5
Redefining tsk_bug_assert to use Rcpp::stop or Rf_error #5
Redefine kas_bug_assert() and tsk_trace_error() that use stderr #5
gregorgorjanc commentedon Jan 11, 2026
As reported at tskit-dev/tskit#3363, have sorted
assert()andabort()calls, and use ofstderr. Next one is to go after use ofstdout`, but that one is a bit trickier so seeking help - see tskit-dev/tskit#3363.