g_output_stream_write: Allow NULL buffer if count is 0

We currently fail a buffer != NULL assertion if buffer is NULL and count is 0. Allow this case without critical assertions.

Fixes https://gitlab.gnome.org/GNOME/glib/-/issues/3402
This commit is contained in:
Gary Li 2024-06-26 14:18:41 -04:00
parent e2a36f8b05
commit d64336e1a9
2 changed files with 6 additions and 1 deletions

View File

@ -216,7 +216,7 @@ g_output_stream_write (GOutputStream *stream,
gssize res;
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), -1);
g_return_val_if_fail (buffer != NULL, 0);
g_return_val_if_fail (buffer != NULL || count == 0, 0);
if (count == 0)
return 0;

View File

@ -307,6 +307,7 @@ test_write_null (void)
{
GOutputStream *mo;
GError *error = NULL;
gssize bytes_written;
g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2471");
@ -316,6 +317,10 @@ test_write_null (void)
g_assert_cmpint (0, ==, g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)));
bytes_written = g_output_stream_write (mo, NULL, 0, NULL, &error);
g_assert_no_error (error);
g_assert_cmpint (0, ==, bytes_written);
g_output_stream_close (mo, NULL, &error);
g_assert_no_error (error);
g_object_unref (mo);