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

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 <pwithnall@gnome.org>

oss-fuzz#372819437
This commit is contained in:
Philip Withnall 2024-10-12 13:02:27 +01:00
parent 38000eb9a0
commit 048a0f73e9
No known key found for this signature in database
GPG Key ID: DCDF5885B1F3ED73
2 changed files with 20 additions and 2 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_CONVERT_ERROR_ILLEGAL_SEQUENCE,
_("Invalid byte sequence in conversion input"));
if (length != NULL)
*length = 0;
g_free (res);
return NULL;
}
return res;

View File

@ -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;
}