[Rd] use of buffers in sprintf and snprintf

Henrik Bengtsson henr|k@bengt@@on @end|ng |rom gm@||@com
Fri May 31 04:38:59 CEST 2019


On Thu, May 30, 2019 at 7:21 PM Simon Urbanek
<simon.urbanek using r-project.org> wrote:
>
> No, that will make it even worse since you'll be declaring a lot more memory that you actually have.
>
> The real problem is that you're ignoring the truncation, so you probably want to use something like
>
> if (snprintf(tempname, sizeof(tempname), "%s.%d", of1name, j) >= sizeof(tempname)) Rf_error("file name is too long");
>
> BTW: most OSes systems have a path limits that are no lower than 256 so you should allow at least as much.

On MS Windows, there's actually a limit of 255 characters, cf.
http://www.aroma-project.org/howtos/UseLongFilenamesOnWindows/
(disclaimer: I'm the author).  Note particularly the comment at the
end:

"Unfortunately it is not a solution to try to use relative instead of
absolute pathnames. The limitation is deep down in the file system
itself and it is the absolute pathname that counts."

Admittedly, it's been several years when I last looked into it, but at
the time when I wrote that I spent lots of time investigating it.

/Henrik

>
> Cheers,
> Simon
>
>
>
>
> > On May 29, 2019, at 11:49 AM, jing hua zhao <jinghuazhao using hotmail.com> wrote:
> >
> > Dear R-developers,
> >
> > I am struggling with packaging with sprintf and snprintf() as the following WARNINGS from gcc 9.x,
> >
> >  hap_c.c:380:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
> >  hap_c.c:392:46: warning: �%d� directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 127 [-Wformat-truncation=]
> >
> > Essentially, I have
> >
> > #define MAX_FILENAME_LEN 128
> > char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN];
> >
> > ...
> >
> > snprintf(tempname,sizeof(tempname),"%s.%d", of1name, j);
> >
> > It looks I could get around with
> >
> >
> > #define MAX_FILENAME_LEN 128
> >
> > #define MAX_FILENAME_LEN2 256
> >
> > char of1name[MAX_FILENAME_LEN],of2name[MAX_FILENAME_LEN], tempname[MAX_FILENAME_LEN2];
> >
> > ...
> > snprintf(tempname,2*sizeof(tempname)+1,"%s.%d", of1name, j)
> >
> > It looks a bit waste of resources to me.
> >
> >
> > Any idea will be greatly appreciated,
> >
> >
> >
> > Jing Hua
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-devel using r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list