[vala] Fix crash when opening file fails



commit 3587d178d7f612de8862ef23d3ab8e464aea4e16
Author: Jürg Billeter <j bitron ch>
Date:   Sun Feb 7 11:23:48 2010 +0100

    Fix crash when opening file fails
    
    Based on patch by pancake, fixes bug 606837.

 ccode/valaccodewriter.vala       |    8 ++++++--
 codegen/valaccodebasemodule.vala |    4 ++++
 codegen/valagirwriter.vala       |    6 +++++-
 vala/valacodewriter.vala         |    4 ++++
 4 files changed, 19 insertions(+), 3 deletions(-)
---
diff --git a/ccode/valaccodewriter.vala b/ccode/valaccodewriter.vala
index ff0677b..f2ddb7e 100644
--- a/ccode/valaccodewriter.vala
+++ b/ccode/valaccodewriter.vala
@@ -51,7 +51,7 @@ public class Vala.CCodeWriter {
 	private string temp_filename;
 	private bool file_exists;
 
-	private FileStream stream;
+	private FileStream? stream;
 	
 	private int indent;
 	private int current_line_number = 1;
@@ -80,6 +80,10 @@ public class Vala.CCodeWriter {
 			stream = FileStream.open (filename, "w");
 		}
 
+		if (stream == null) {
+			return false;
+		}
+
 		write_string ("/* %s generated by valac, the Vala compiler".printf (Path.get_basename (filename)));
 
 		// Write the file name if known
@@ -92,7 +96,7 @@ public class Vala.CCodeWriter {
 		write_newline ();
 		write_newline ();
 
-		return (stream != null);
+		return true;
 	}
 
 	/**
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index ae358fc..8374812 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -369,6 +369,10 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 		// generate symbols file for public API
 		if (context.symbols_filename != null) {
 			var stream = FileStream.open (context.symbols_filename, "w");
+			if (stream == null) {
+				Report.error (null, "unable to open `%s' for writing".printf (context.symbols_filename));
+				return;
+			}
 
 			foreach (CCodeNode node in header_declarations.type_member_declaration.get_children ()) {
 				if (node is CCodeFunction) {
diff --git a/codegen/valagirwriter.vala b/codegen/valagirwriter.vala
index f9c5f3c..fd90f85 100644
--- a/codegen/valagirwriter.vala
+++ b/codegen/valagirwriter.vala
@@ -1,6 +1,6 @@
 /* valagirwriter.vala
  *
- * Copyright (C) 2008-2009  Jürg Billeter
+ * Copyright (C) 2008-2010  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -87,6 +87,10 @@ public class Vala.GIRWriter : CodeVisitor {
 
 		string filename = "%s%c%s-%s.gir".printf (directory, Path.DIR_SEPARATOR, gir_namespace, gir_version);
 		stream = FileStream.open (filename, "w");
+		if (stream == null) {
+			Report.error (null, "unable to open `%s' for writing".printf (filename));
+			return;
+		}
 
 		stream.printf ("<?xml version=\"1.0\"?>\n");
 
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index eeb01ed..ad69e45 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -70,6 +70,10 @@ public class Vala.CodeWriter : CodeVisitor {
 		this.context = context;
 	
 		stream = FileStream.open (filename, "w");
+		if (stream == null) {
+			Report.error (null, "unable to open `%s' for writing".printf (filename));
+			return;
+		}
 
 		write_string ("/* %s generated by %s, do not modify. */".printf (Path.get_basename (filename), Environment.get_prgname ()));
 		write_newline ();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]