[R-pkg-devel] Check warning around sprintf: 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.

Martin Maechler m@ech|er @end|ng |rom @t@t@m@th@ethz@ch
Mon Nov 20 10:09:43 CET 2023


>>>>> Serguei Sokol 
>>>>>     on Mon, 20 Nov 2023 10:04:10 +0100 writes:

    > Le 19/11/2023 à 02:07, Iris Simmons a écrit :
    >> Yes, the reason for the error is the use of sprintf. You can instead use
    >> snprintf where n is the maximum number of bytes to write, including the
    >> terminating nul character. For example:
    >> 
    >> char msg[8191];
    >> snprintf(msg, 8191, "criteria: error (%d) -> %s\n", inErr, errStr);
    > This line should be

    > snprintf(msg, 8190, "criteria: error (%d) -> %s\n", inErr, errStr);

    > i.e. 1-less than 'msg' size, leaving a room for the terminal 0-byte.
    > Otherwise, a recent version of gcc emits a warning caught by CRAN.

    > Best,
    > Serguei.

Yes, indeed  ' - 1 ',

but really  Iago    (and everyone else!) should in such cases
use the much nicer and easier code that  Ivan Krylov proposed
yesterday :

   Rf_error("criteria: error (%d) -> %s", inErr, errStr);

with *no* need of declaring a `msg` buffer and thinking about
its size.

Martin



More information about the R-package-devel mailing list