Bug 17191 - Bug in reporting error opening a PDF output file in temp directory
Summary: Bug in reporting error opening a PDF output file in temp directory
Alias: None
Product: R
Classification: Unclassified
Component: Graphics (show other bugs)
Version: R 3.3.*
Hardware: x86_64/x64/amd64 (64-bit) Linux-RHEL
: P5 normal
Assignee: R-core
Depends on:
Reported: 2016-12-07 17:00 UTC by Alec Wysoker
Modified: 2016-12-08 00:33 UTC (History)
1 user (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Alec Wysoker 2016-12-07 17:00:11 UTC
It's hard to reproduce this problem, but looking at the code it's pretty clear what the problem is.

In R-3.3.2/src/library/grDevices/src/devPS.c, function PDF_NewPage, around line 7221:

	pd->pdffp = fopen(tmp, "w+b");
	if(! pd->pdffp) error("cannot open file '%s', reason %s", 
			      tmp, strerror(errno));

Note that tmp is freed before the error message is created.  On my system, the memory manager appears to write a null byte at the beginning of the buffer when free() is called, resulting in this error message:

Error in plot.new() : 
  cannot open file '', reason No such file or directory

The original problem is not R's bug -- I think the file system TMPDIR is pointing to gets unmounted by a long-running R job.  However, the fact that the error message is broken made it harder for me to diagnose the problem.  I don't know what kind of try/catch/finally mechanism R uses, but ideally free(tmp) would happen after error() is called.

Note also that R eventually dumps core, which I care about less than the broken error message.

#0  0x00000037ff467934 in fwrite () from /lib64/libc.so.6
#1  0x00002ae33b8c10e2 in PDF_endpage () at devPS.c:6509
#2  0x00002ae33b8c284b in PDF_Close () at devPS.c:7257
#3  0x00002ae338dbf05e in removeDevice.part.0 ()
   from /broad/software/free/Linux/redhat_6_x86_64/pkgs/r_3.3.0/lib64/R/lib/libR.so
#4  0x00002ae338dbf4c9 in Rf_KillAllDevices ()
   from /broad/software/free/Linux/redhat_6_x86_64/pkgs/r_3.3.0/lib64/R/lib/libR.so
#5  0x00002ae338ee42e4 in Rstd_CleanUp ()
   from /broad/software/free/Linux/redhat_6_x86_64/pkgs/r_3.3.0/lib64/R/lib/libR.so
#6  0x00002ae338e21947 in run_Rmainloop ()
   from /broad/software/free/Linux/redhat_6_x86_64/pkgs/r_3.3.0/lib64/R/lib/libR.so
#7  0x00000000004007bb in main () at Rmain.c:29

Comment 1 Paul Murrell 2016-12-08 00:14:37 UTC
Here's how I could reliably trigger the problem (on Ubuntu 14.04) ...

## Make tempdir() unwritable
system(paste("chmod a-w", tempdir()))

## compression is on by default
## Incorrect error message is in PDF_NewPage()
## This triggers a segfault (for me at least)

I have committed a fix to r-devel (r71752) so that, in addition to giving a better message (and not segfaulting), the error is now a warning and compression is turned off for the PDF device.
Comment 2 Alec Wysoker 2016-12-08 00:33:29 UTC