Merge branch 'data-input-stream-read-line-utf8-fix' into 'main'

gdatainputstream: Fix length return value on UTF-8 validation failure

See merge request GNOME/glib!4348
This commit is contained in:
Philip Withnall 2024-10-12 12:19:10 +00:00
commit a8dbd7cad5
2 changed files with 24 additions and 6 deletions

View File

@ -840,7 +840,11 @@ g_data_input_stream_read_line_utf8 (GDataInputStream *stream,
g_set_error_literal (error, G_CONVERT_ERROR, g_set_error_literal (error, G_CONVERT_ERROR,
G_CONVERT_ERROR_ILLEGAL_SEQUENCE, G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
_("Invalid byte sequence in conversion input")); _("Invalid byte sequence in conversion input"));
if (length != NULL)
*length = 0;
g_free (res); g_free (res);
return NULL; return NULL;
} }
return res; return res;

View File

@ -80,9 +80,9 @@ test_read_lines (GDataStreamNewlineType newline_type)
lines[i] = "some_text"; lines[i] = "some_text";
base_stream = g_memory_input_stream_new (); 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)); stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
g_assert(stream != NULL); g_assert_nonnull (stream);
/* Byte order testing */ /* Byte order testing */
g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN); g_data_input_stream_set_byte_order (G_DATA_INPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
@ -174,8 +174,17 @@ test_read_lines_LF_valid_utf8 (void)
gsize length = -1; gsize length = -1;
line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error); line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
if (line == NULL) if (line == NULL)
{
g_assert_cmpuint (length, ==, 0);
break; break;
}
else
{
g_assert_cmpuint (length, >, 0);
}
n_lines++; n_lines++;
g_free (line); g_free (line);
} }
@ -207,11 +216,16 @@ test_read_lines_LF_invalid_utf8 (void)
gsize length = -1; gsize length = -1;
line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error); line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
if (n_lines == 0) if (n_lines == 0)
{
/* First line is valid UTF-8 */
g_assert_no_error (error); g_assert_no_error (error);
g_assert_cmpuint (length, ==, 3);
}
else else
{ {
g_assert (error != NULL); g_assert_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE);
g_clear_error (&error); g_clear_error (&error);
g_assert_cmpuint (length, ==, 0);
g_free (line); g_free (line);
break; break;
} }
@ -354,7 +368,7 @@ test_read_upto (void)
line++; line++;
stop_char = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error); 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_assert_no_error (error);
} }
g_free (data); g_free (data);