[R] segmentation fault with sprintf(), sometimes

Don MacQueen macq at llnl.gov
Thu May 6 17:56:51 CEST 2004


By mistake I supplied sprintf() with a bad format specification. I
got a segmentation fault. But not every time.

>   version
           _
platform sparc-sun-solaris2.8
arch     sparc
os       solaris2.8
system   sparc, solaris2.8
status   Patched
major    1
minor    9.0
year     2004
month    04
day      30
language R

>   sprintf('%.-0.999f',3)
Segmentation fault

The format string makes no sense, of course, so one should expect
something bad to happen. But preferably not that bad!

Also, the behavior is to some degree indeterminate. Sometimes I get this:

>   sprintf('%.-0.999f',3)
[1]
"0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000!
  0
000000000000"

I haven't identified anything that might determine which the above
responses occurs.

On another platform, note that the formatted string begins with "3",
not "0" as above.

>   version
           _
platform powerpc-apple-darwin6.8.5
arch     powerpc
os       darwin6.8.5
system   powerpc, darwin6.8.5
status   Patched
major    1
minor    9.0
year     2004
month    04
day      30
language R
>    sprintf('%.-0.999f',3)
[1]
"3.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000!
  0
000000000000"
>

The behavior is no doubt dependent on the operating system's sprintf,
for I find in R's sprintf.c:

/* Simple wrapper for C sprintf function: now (1.6.0) checks the
     types and handles the R specials.
*/

-- 
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA




More information about the R-help mailing list