Fix crash when opening output file fails, fixes bug 466573
authorJuerg Billeter <j@bitron.ch>
Sun, 11 May 2008 13:57:44 +0000 (13:57 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Sun, 11 May 2008 13:57:44 +0000 (13:57 +0000)
2008-05-11  Juerg Billeter  <j@bitron.ch>

* ccode/valaccodewriter.vala:
* gobject/valaccodegeneratorsourcefile.vala:

Fix crash when opening output file fails, fixes bug 466573

svn path=/trunk/; revision=1370

ChangeLog
ccode/valaccodewriter.vala
gobject/valaccodegeneratorsourcefile.vala

index 7a0e1e5..9e62bd1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2008-05-11  Jürg Billeter  <j@bitron.ch>
 
+       * ccode/valaccodewriter.vala:
+       * gobject/valaccodegeneratorsourcefile.vala:
+
+       Fix crash when opening output file fails, fixes bug 466573
+
+2008-05-11  Jürg Billeter  <j@bitron.ch>
+
        * gobject/valaccodegenerator.vala: fix freeing nested GLists and
        GSLists, fixes bug 443514
 
index c011c81..4330e7a 100644 (file)
@@ -35,13 +35,6 @@ public class Vala.CCodeWriter : Object {
                }
                construct {
                        _filename = value;
-                       file_exists = FileUtils.test (_filename, FileTest.EXISTS);
-                       if (file_exists) {
-                               temp_filename = "%s.valatmp".printf (_filename);
-                               stream = FileStream.open (temp_filename, "w");
-                       } else {
-                               stream = FileStream.open (_filename, "w");
-                       }
                }
        }
 
@@ -71,7 +64,25 @@ public class Vala.CCodeWriter : Object {
        public CCodeWriter (string _filename) {
                filename = _filename;
        }
-       
+
+       /**
+        * Opens the file.
+        *
+        * @return true if the file has been opened successfully,
+        *         false otherwise
+        */
+       public bool open () {
+               file_exists = FileUtils.test (_filename, FileTest.EXISTS);
+               if (file_exists) {
+                       temp_filename = "%s.valatmp".printf (_filename);
+                       stream = FileStream.open (temp_filename, "w");
+               } else {
+                       stream = FileStream.open (_filename, "w");
+               }
+
+               return (stream != null);
+       }
+
        /**
         * Closes the file.
         */
index 3c36ad2..c77d495 100644 (file)
@@ -297,6 +297,10 @@ public class Vala.CCodeGenerator {
                }
 
                var writer = new CCodeWriter (source_file.get_cheader_filename ());
+               if (!writer.open ()) {
+                       Report.error (null, "unable to open `%s' for writing".printf (writer.filename));
+                       return;
+               }
                if (comment != null) {
                        comment.write (writer);
                }
@@ -323,6 +327,10 @@ public class Vala.CCodeGenerator {
                writer.close ();
                
                writer = new CCodeWriter (source_file.get_csource_filename ());
+               if (!writer.open ()) {
+                       Report.error (null, "unable to open `%s' for writing".printf (writer.filename));
+                       return;
+               }
                writer.line_directives = context.debug;
                if (comment != null) {
                        comment.write (writer);