From 38000eb9a0433c9c68e0731249e6930a0df68f5d Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Sat, 12 Oct 2024 12:56:00 +0100 Subject: [PATCH 1/2] tests: Use g_assert_*() rather than g_assert() in GDataInputStream tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It won’t get compiled out with `G_DISABLE_ASSERT`. Signed-off-by: Philip Withnall --- gio/tests/data-input-stream.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/gio/tests/data-input-stream.c b/gio/tests/data-input-stream.c index 280c93fac..e3f131f68 100644 --- a/gio/tests/data-input-stream.c +++ b/gio/tests/data-input-stream.c @@ -80,9 +80,9 @@ test_read_lines (GDataStreamNewlineType newline_type) lines[i] = "some_text"; base_stream = g_memory_input_stream_new (); - g_assert (base_stream != NULL); + g_assert_nonnull (base_stream); stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream)); - g_assert(stream != NULL); + g_assert_nonnull (stream); /* Byte order testing */ g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN); @@ -210,7 +210,7 @@ test_read_lines_LF_invalid_utf8 (void) g_assert_no_error (error); else { - g_assert (error != NULL); + g_assert_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE); g_clear_error (&error); g_free (line); break; @@ -318,7 +318,7 @@ test_read_upto (void) line++; stop_char = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error); - g_assert (memchr (DATA_SEP, stop_char, DATA_SEP_LEN) != NULL); + g_assert_nonnull (memchr (DATA_SEP, stop_char, DATA_SEP_LEN)); g_assert_no_error (error); } g_free (data); From 048a0f73e980118c2ac7e82615abfcf856588605 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Sat, 12 Oct 2024 13:02:27 +0100 Subject: [PATCH 2/2] gdatainputstream: Fix length return value on UTF-8 validation failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The method was correctly returning an error from `g_data_input_stream_read_line_utf8()` if the line contained invalid UTF-8, but it wasn’t correctly setting the returned line length to 0. This could have caused problems if callers were basing subsequent logic on the length and not the return value nullness or `GError`. Signed-off-by: Philip Withnall oss-fuzz#372819437 --- gio/gdatainputstream.c | 4 ++++ gio/tests/data-input-stream.c | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/gio/gdatainputstream.c b/gio/gdatainputstream.c index 0a1672409..fa3842d8d 100644 --- a/gio/gdatainputstream.c +++ b/gio/gdatainputstream.c @@ -840,7 +840,11 @@ g_data_input_stream_read_line_utf8 (GDataInputStream *stream, g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE, _("Invalid byte sequence in conversion input")); + + if (length != NULL) + *length = 0; g_free (res); + return NULL; } return res; diff --git a/gio/tests/data-input-stream.c b/gio/tests/data-input-stream.c index e3f131f68..55fc304e4 100644 --- a/gio/tests/data-input-stream.c +++ b/gio/tests/data-input-stream.c @@ -174,8 +174,17 @@ test_read_lines_LF_valid_utf8 (void) gsize length = -1; line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error); g_assert_no_error (error); + if (line == NULL) - break; + { + g_assert_cmpuint (length, ==, 0); + break; + } + else + { + g_assert_cmpuint (length, >, 0); + } + n_lines++; g_free (line); } @@ -207,11 +216,16 @@ test_read_lines_LF_invalid_utf8 (void) gsize length = -1; line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error); if (n_lines == 0) - g_assert_no_error (error); + { + /* First line is valid UTF-8 */ + g_assert_no_error (error); + g_assert_cmpuint (length, ==, 3); + } else { g_assert_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE); g_clear_error (&error); + g_assert_cmpuint (length, ==, 0); g_free (line); break; }