g_output_stream_write_all: Allow NULL empty buffer

This commit is contained in:
James Westman 2021-09-21 10:40:14 +00:00 committed by Philip Withnall
parent 44666880ad
commit f6ddce4b16
2 changed files with 21 additions and 1 deletions

View File

@ -293,7 +293,7 @@ g_output_stream_write_all (GOutputStream *stream,
gssize res; gssize res;
g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE); 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; _bytes_written = 0;
while (_bytes_written < count) while (_bytes_written < count)

View File

@ -300,6 +300,25 @@ test_write_bytes (void)
g_bytes_unref (bytes2); 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 /* Test that writev() works on #GMemoryOutputStream with a non-empty set of vectors. This
* covers the default writev() implementation around write(). */ * covers the default writev() implementation around write(). */
static void 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/get-data-size", test_data_size);
g_test_add_func ("/memory-output-stream/properties", test_properties); 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-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", test_writev);
g_test_add_func ("/memory-output-stream/writev_nonblocking", test_writev_nonblocking); 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); g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);