From c7e351290252acc14054baf569a4674615e22af0 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 19 Sep 2024 17:40:45 +0100 Subject: [PATCH] tests: Add some additional pattern_coalesce() tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first of these tests is to cover the extreme case where the number of bytes written out by `pattern_coalesce()` is as close to `strlen (left) + strlen (right)` as we can manage, due to both inputs being really small and hence `max (strlen (left), strlen (right))` being only 1 less than `strlen (left) + strlen (right)`, which is as close as we can get to triggering an off-by-one error. The second of these tests is an attempt to encode the case used in the proof for correctness of `pattern_coalesce()` not overflowing its buffer bounds: `(*(iii)) + ((iii)*) = ((iii)(iii))`. I don’t think it’s actually possible to hit that case, as it’s not possible to generate a plain `*` in either of the input patterns. `Ma*` is the best we can manage in practice. See https://gitlab.gnome.org/GNOME/glib/-/issues/3469#note_2226901 for reasoning. Signed-off-by: Philip Withnall Helps: #3469 --- glib/tests/gvariant.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/glib/tests/gvariant.c b/glib/tests/gvariant.c index c00a559e6..2aa80d6da 100644 --- a/glib/tests/gvariant.c +++ b/glib/tests/gvariant.c @@ -4025,6 +4025,29 @@ test_parses (void) g_variant_unref (value); } + /* pattern coalesce of `u` and `u` is `u`; this operates close to the string + * length bounds in pattern_coalesce() */ + { + GVariant *value = NULL; + GError *error = NULL; + + value = g_variant_parse (NULL, "[@u 5, @u 15]", NULL, NULL, &error); + g_assert_no_error (error); + g_assert_cmpstr (g_variant_get_type_string (value), ==, "au"); + g_variant_unref (value); + } + + /* pattern coalesce of `(Ma*Ma(iii))` and `(Ma(iii)Ma*)` is `(Ma(iii)Ma(iii))` */ + { + GVariant *value = NULL; + GError *error = NULL; + + value = g_variant_parse (NULL, "[([], [(1,2,3)]), ([(1,2,3)], [])]", NULL, NULL, &error); + g_assert_no_error (error); + g_assert_cmpstr (g_variant_get_type_string (value), ==, "a(a(iii)a(iii))"); + g_variant_unref (value); + } + #ifndef _MSC_VER /* inf/nan strings are C99 features which Visual C++ does not support */ /* inf/nan mini test */