[glib] Don't segfault when <default> is missing



commit 4a605693fce718a81f809245be2ebad9978031ac
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Apr 19 11:31:21 2010 -0400

    Don't segfault when <default> is missing
    
    Also add a framework for schema compiler tests.
    
    Fixes bug 616086.

 gio/gschema-compile.c                              |   10 +++++-
 gio/tests/Makefile.am                              |   20 +++++++++--
 gio/tests/gschema-compile.c                        |   35 ++++++++++++++++++++
 .../schema-tests/no-default/no-default.gschema.xml |    6 +++
 4 files changed, 67 insertions(+), 4 deletions(-)
---
diff --git a/gio/gschema-compile.c b/gio/gschema-compile.c
index da86106..3a657bb 100644
--- a/gio/gschema-compile.c
+++ b/gio/gschema-compile.c
@@ -390,6 +390,14 @@ end_element (GMarkupParseContext  *context,
 
   else if (strcmp (element_name, "key") == 0)
     {
+      if (state->value == NULL)
+        {
+          g_set_error_literal (error,
+                               G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
+                               "Element <default> is required in <key>\n");
+          return;
+        }
+
       gvdb_item_set_value (state->key, state->value);
       gvdb_item_set_options (state->key,
                              g_variant_builder_end (&state->key_options));
@@ -521,7 +529,7 @@ main (int argc, char **argv)
     {
       if (g_str_has_suffix (file, ".gschema.xml"))
 	{
-	  g_ptr_array_add (files, g_strdup (file));
+	  g_ptr_array_add (files, g_build_filename (srcdir, file, NULL));
 	}
     }
 
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index ae3d110..3d467d9 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -34,7 +34,8 @@ TEST_PROGS +=	 		\
 	simple-async-result	\
 	srvtarget		\
 	contexts		\
-	gsettings
+	gsettings		\
+	gschema-compile
 
 SAMPLE_PROGS = 			\
 	resolver		\
@@ -146,7 +147,14 @@ contexts_LDADD		  = $(progs_ldadd) \
 gsettings_SOURCES	  = gsettings.c
 gsettings_LDADD		  = $(progs_ldadd)
 
-EXTRA_DIST += org.gtk.test.gschema org.gtk.test.gschema.xml de.po
+gschema_compile_SOURCES	  = gschema-compile.c
+gschema_compile_LDADD	  = $(progs_ldadd)
+
+EXTRA_DIST +=                                          \
+	org.gtk.test.gschema                           \
+	org.gtk.test.gschema.xml                       \
+	de.po                                          \
+	schema-tests/no-default/no-default.gschema.xml \
 
 MISC_STUFF = gschemas.compiled test.mo
 
@@ -158,4 +166,10 @@ test.mo: de.po
 gschemas.compiled:
 	$(top_builddir)/gio/gschema-compile --targetdir=$(abs_builddir) $(srcdir)
 
-DISTCLEANFILES = applications/mimeinfo.cache de/LC_MESSAGES/test.mo test.mo  gsettings.store
+DISTCLEANFILES =                                  \
+	applications/mimeinfo.cache               \
+	de/LC_MESSAGES/test.mo                    \
+	test.mo                                   \
+	gsettings.store                           \
+	gschemas.compiled                         \
+	schema-tests/no-schema/gschemas.compiled
diff --git a/gio/tests/gschema-compile.c b/gio/tests/gschema-compile.c
new file mode 100644
index 0000000..7a7a8e4
--- /dev/null
+++ b/gio/tests/gschema-compile.c
@@ -0,0 +1,35 @@
+#include <stdlib.h>
+#include <unistd.h>
+#include <locale.h>
+#include <libintl.h>
+#include <gio.h>
+#include <gstdio.h>
+
+static void
+test_no_default (void)
+{
+  g_remove ("schema-tests/no-default/gschemas.compiled");
+
+  if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR))
+    {
+       gchar *argv[] = { "../gschema-compile", "./schema-tests/no-default/", NULL };
+       gchar *envp[] = { NULL }; 
+       execve (argv[0], argv, envp);
+    }
+  g_test_trap_assert_failed ();
+  g_test_trap_assert_stderr ("*<default> is required in <key>*");
+}
+
+
+int
+main (int argc, char *argv[])
+{
+  setlocale (LC_ALL, "");
+
+  g_type_init ();
+  g_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/gschema/no-default", test_no_default);
+
+  return g_test_run ();
+}
diff --git a/gio/tests/schema-tests/no-default/no-default.gschema.xml b/gio/tests/schema-tests/no-default/no-default.gschema.xml
new file mode 100644
index 0000000..5496ce3
--- /dev/null
+++ b/gio/tests/schema-tests/no-default/no-default.gschema.xml
@@ -0,0 +1,6 @@
+<schemalist>
+  <schema id="no-default" path="/tests/">
+    <key name="test" type="s">
+    </key>
+  </schema>
+</schemalist>



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