On Fri, 2014-10-17 at 21:52 +0000, Joseph S. Myers wrote:
[...snip static linkage discussion...]

> The dump file handling appears to have no I/O error checking (no checking 
> for error on fopen, nothing obvious to prevent fwrite to a NULL m_file if 
> fopen did have an error, no checking for error on fclose (or fwrite)).

Thanks.

Does the following look OK?  (I've committed it to branch
dmalcolm/jit)

gcc/jit/ChangeLog.jit:
        * jit-recording.c (gcc::jit::dump::dump): Handle fopen failures
        by emitting an error on the context.
        (gcc::jit::dump::~dump): Likewise for fclose failures.
        (gcc::jit::dump::write): Don't attempt further work if the fopen
        failed.  Handle fwrite failures by emitting an error on the
        context.
---
 gcc/jit/ChangeLog.jit   |  9 +++++++++
 gcc/jit/jit-recording.c | 25 ++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/gcc/jit/ChangeLog.jit b/gcc/jit/ChangeLog.jit
index 9a36dfd..02664f0 100644
--- a/gcc/jit/ChangeLog.jit
+++ b/gcc/jit/ChangeLog.jit
@@ -1,5 +1,14 @@
 2014-10-20  David Malcolm  <dmalc...@redhat.com>
 
+       * jit-recording.c (gcc::jit::dump::dump): Handle fopen failures
+       by emitting an error on the context.
+       (gcc::jit::dump::~dump): Likewise for fclose failures.
+       (gcc::jit::dump::write): Don't attempt further work if the fopen
+       failed.  Handle fwrite failures by emitting an error on the
+       context.
+
+2014-10-20  David Malcolm  <dmalc...@redhat.com>
+
        * Make-lang.in (jit.install-common): Drop installation of
        libgccjit.pc.
        * config-lang.in (outputs): Drop jit/libgccjit.pc.
diff --git a/gcc/jit/jit-recording.c b/gcc/jit/jit-recording.c
index 32ce49b..5a97f23 100644
--- a/gcc/jit/jit-recording.c
+++ b/gcc/jit/jit-recording.c
@@ -47,13 +47,25 @@ dump::dump (recording::context &ctxt,
   m_line (0),
   m_column (0)
 {
-  m_file  = fopen (filename, "w");
+  m_file = fopen (filename, "w");
+  if (!m_file)
+    ctxt.add_error (NULL,
+                   "error opening dump file %s for writing: %s",
+                   filename,
+                   xstrerror (errno));
 }
 
 dump::~dump ()
 {
   if (m_file)
-    fclose (m_file);
+    {
+      int err = fclose (m_file);
+      if (err)
+       m_ctxt.add_error (NULL,
+                         "error closing dump file %s: %s",
+                         m_filename,
+                         xstrerror (errno));
+    }
 }
 
 /* Write the given message to the dump, using printf-formatting
@@ -67,6 +79,11 @@ dump::write (const char *fmt, ...)
   va_list ap;
   char *buf = NULL;
 
+  /* If there was an error opening the file, we've already reported it.
+     Don't attempt further work.  */
+  if (!m_file)
+    return;
+
   va_start (ap, fmt);
   vasprintf (&buf, fmt, ap);
   va_end (ap);
@@ -78,7 +95,9 @@ dump::write (const char *fmt, ...)
       return;
     }
 
-  fwrite (buf, strlen (buf), 1, m_file);
+  if (fwrite (buf, strlen (buf), 1, m_file) != 1)
+    m_ctxt.add_error (NULL, "error writing to dump file %s",
+                     m_filename);
 
   /* Update line/column: */
   for (const char *ptr = buf; *ptr; ptr++)
-- 
1.7.11.7

Reply via email to