summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGary Li <gary.li1@uwaterloo.ca>2024-06-26 14:18:41 -0400
committerGary Li <gary.li1@uwaterloo.ca>2024-06-26 14:18:41 -0400
commitd64336e1a98d47f3484e69b7eaf19f105379c171 (patch)
tree51584698cf3584cb4fd0e5c970bee80cc6021f16
parente2a36f8b05341a8e8e92a7fe84e4f0cd0ea3f36e (diff)
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
-rw-r--r--gio/goutputstream.c2
-rw-r--r--gio/tests/memory-output-stream.c5
2 files changed, 6 insertions, 1 deletions
diff --git a/gio/goutputstream.c b/gio/goutputstream.c
index 1348208dd..dfecfb2fc 100644
--- a/gio/goutputstream.c
+++ b/gio/goutputstream.c
@@ -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;
diff --git a/gio/tests/memory-output-stream.c b/gio/tests/memory-output-stream.c
index a517cb964..60733511f 100644
--- a/gio/tests/memory-output-stream.c
+++ b/gio/tests/memory-output-stream.c
@@ -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);