[glib: 1/2] g_output_stream_write_all: Allow NULL empty buffer




commit f6ddce4b16b8832ece01ba8617e3f14c823cb7d8
Author: James Westman <james jwestman net>
Date:   Tue Sep 21 10:40:14 2021 +0000

    g_output_stream_write_all: Allow NULL empty buffer

 gio/goutputstream.c              |  2 +-
 gio/tests/memory-output-stream.c | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
---
diff --git a/gio/goutputstream.c b/gio/goutputstream.c
index 8e48803be..3547b8f12 100644
--- a/gio/goutputstream.c
+++ b/gio/goutputstream.c
@@ -293,7 +293,7 @@ g_output_stream_write_all (GOutputStream  *stream,
   gssize res;
 
   g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
-  g_return_val_if_fail (buffer != NULL, FALSE);
+  g_return_val_if_fail (buffer != NULL || count == 0, FALSE);
 
   _bytes_written = 0;
   while (_bytes_written < count)
diff --git a/gio/tests/memory-output-stream.c b/gio/tests/memory-output-stream.c
index b448516df..ec1644ed8 100644
--- a/gio/tests/memory-output-stream.c
+++ b/gio/tests/memory-output-stream.c
@@ -300,6 +300,25 @@ test_write_bytes (void)
   g_bytes_unref (bytes2);
 }
 
+static void
+test_write_null (void)
+{
+  GOutputStream *mo;
+  GError *error = NULL;
+
+  g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2471";);
+
+  mo = g_memory_output_stream_new_resizable ();
+  g_output_stream_write_all (mo, NULL, 0, NULL, NULL, &error);
+  g_assert_no_error (error);
+
+  g_assert_cmpint (0, ==, g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)));
+
+  g_output_stream_close (mo, NULL, &error);
+  g_assert_no_error (error);
+  g_object_unref (mo);
+}
+
 /* Test that writev() works on #GMemoryOutputStream with a non-empty set of vectors. This
  * covers the default writev() implementation around write(). */
 static void
@@ -437,6 +456,7 @@ main (int   argc,
   g_test_add_func ("/memory-output-stream/get-data-size", test_data_size);
   g_test_add_func ("/memory-output-stream/properties", test_properties);
   g_test_add_func ("/memory-output-stream/write-bytes", test_write_bytes);
+  g_test_add_func ("/memory-output-stream/write-null", test_write_null);
   g_test_add_func ("/memory-output-stream/writev", test_writev);
   g_test_add_func ("/memory-output-stream/writev_nonblocking", test_writev_nonblocking);
   g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]