[glib/glib-2-58: 17/21] tests: Run GMarkup tests with and without nul-termination



commit 29c1b6e72c0efc1ac9b2de0227dba805614984f8
Author: Philip Withnall <withnall endlessm com>
Date:   Thu Aug 2 11:01:02 2018 +0100

    tests: Run GMarkup tests with and without nul-termination
    
    When using GMarkup to parse a string, the string can be provided with an
    explicit length specified, or with no length and a nul terminator
    instead. Run all the GMarkup tests both ways, to catch problems with
    length checks, or with nul terminator checks.
    
    Signed-off-by: Philip Withnall <withnall endlessm com>

 glib/tests/markup-parse.c | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)
---
diff --git a/glib/tests/markup-parse.c b/glib/tests/markup-parse.c
index 2ec30d051..18d96ccc8 100644
--- a/glib/tests/markup-parse.c
+++ b/glib/tests/markup-parse.c
@@ -148,7 +148,7 @@ test_in_chunks (const gchar       *contents,
 static int
 test_file (const gchar *filename, GMarkupParseFlags flags)
 {
-  gchar *contents;
+  gchar *contents = NULL, *contents_unterminated = NULL;
   gsize  length;
   GError *error;
   GMarkupParseContext *context;
@@ -165,12 +165,13 @@ test_file (const gchar *filename, GMarkupParseFlags flags)
       return 1;
     }
 
+  /* Test with nul termination. */
   context = g_markup_parse_context_new (&parser, flags, NULL, NULL);
   g_assert (g_markup_parse_context_get_user_data (context) == NULL);
   g_markup_parse_context_get_position (context, &line, &col);
   g_assert (line == 1 && col == 1);
 
-  if (!g_markup_parse_context_parse (context, contents, length, NULL))
+  if (!g_markup_parse_context_parse (context, contents, -1, NULL))
     {
       g_markup_parse_context_free (context);
       g_free (contents);
@@ -186,6 +187,28 @@ test_file (const gchar *filename, GMarkupParseFlags flags)
 
   g_markup_parse_context_free (context);
 
+  /* With the length specified explicitly and a nul terminator present (since
+   * g_file_get_contents() always adds one). */
+  if (test_in_chunks (contents, length, length, flags) != 0)
+    {
+      g_free (contents);
+      return 1;
+    }
+
+  /* With the length specified explicitly and no nul terminator present. */
+  contents_unterminated = g_malloc (length);
+  if (contents_unterminated != NULL)
+    memcpy (contents_unterminated, contents, length);
+
+  if (test_in_chunks (contents_unterminated, length, length, flags) != 0)
+    {
+      g_free (contents);
+      g_free (contents_unterminated);
+      return 1;
+    }
+
+  g_free (contents_unterminated);
+
   /* A byte at a time */
   if (test_in_chunks (contents, length, 1, flags) != 0)
     {


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