[R-pkg-devel] replacements of sprintf in compiled code

Matthias Gondan m@tth|@@-gond@n @end|ng |rom gmx@de
Sat Jan 21 11:46:52 CET 2023


I think the function is more than 20 years old

https://en.cppreference.com/w/c/io/fprintf



Von: Holger Hoefling
Gesendet: Samstag, 21. Januar 2023 11:37
An: Duncan Murdoch
Cc: R Package Development
Betreff: Re: [R-pkg-devel] replacements of sprintf in compiled code

Hi,

thanks for the tip! Is that available everywhere or do I need to set
compiler requirements?

Best

Holger Hoefling

On Sat, Jan 21, 2023 at 11:27 AM Duncan Murdoch <murdoch.duncan using gmail.com>
wrote:

> On 21/01/2023 5:15 a.m., Holger Hoefling wrote:
> > Hi,
> >
> > In my recent re-submission with a bug-fix of the hdf5r package, I got a
> new
> > set of warnings from the compiler, one being that I shouldn't be using
> > 'sprintf'.
> >
> > Is there a simple replacement that I can use?
>
> You should use snprintf() which has an extra argument to state the size
> of the buffer receiving the string.  For example,
>
>   char text[32];
>   sprintf(text, "%.4g", value);
>
> could be written as
>
>   char text[32];
>   snprintf(text, 32, "%.4g", value);
>
> This will write a string with at most 31 characters before the NUL at
> the end, and avoids the possibility of a buffer overrun.
>
> Duncan Murdoch
>

	[[alternative HTML version deleted]]

______________________________________________
R-package-devel using r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list