From 4795dadde49dac18a7260044a74bf2236c188595 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Thu, 11 Oct 2018 11:29:48 +1300 Subject: [PATCH] gsubprocess: Clear std buf outputs to NULL on failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of sometimes returning a non-NULL buffer, always return NULL. However, keep the documentation as explicitly returning undefined values on failure, so that we can change the behaviour in future if needed. The return values weren’t defined for failure before, so were implicitly returning undefined values. Signed-off-by: Philip Withnall --- gio/gsubprocess.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/gio/gsubprocess.c b/gio/gsubprocess.c index 23bfbddf2..dcdb84d58 100644 --- a/gio/gsubprocess.c +++ b/gio/gsubprocess.c @@ -1784,6 +1784,9 @@ g_subprocess_communicate_finish (GSubprocess *subprocess, * * Like g_subprocess_communicate(), but validates the output of the * process as UTF-8, and returns it as a regular NUL terminated string. + * + * On error, @stdout_buf and @stderr_buf will be set to undefined values and + * should not be used. */ gboolean g_subprocess_communicate_utf8 (GSubprocess *subprocess, @@ -1868,6 +1871,7 @@ communicate_result_validate_utf8 (const char *stream_name, if (!g_utf8_validate (*return_location, -1, &end)) { g_free (*return_location); + *return_location = NULL; g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid UTF-8 in child %s at offset %lu", stream_name, @@ -1900,6 +1904,7 @@ g_subprocess_communicate_utf8_finish (GSubprocess *subprocess, { gboolean ret = FALSE; CommunicateState *state; + gchar *local_stdout_buf = NULL, *local_stderr_buf = NULL; g_return_val_if_fail (G_IS_SUBPROCESS (subprocess), FALSE); g_return_val_if_fail (g_task_is_valid (result, subprocess), FALSE); @@ -1913,11 +1918,11 @@ g_subprocess_communicate_utf8_finish (GSubprocess *subprocess, /* TODO - validate UTF-8 while streaming, rather than all at once. */ - if (!communicate_result_validate_utf8 ("stdout", stdout_buf, + if (!communicate_result_validate_utf8 ("stdout", &local_stdout_buf, state->stdout_buf, error)) goto out; - if (!communicate_result_validate_utf8 ("stderr", stderr_buf, + if (!communicate_result_validate_utf8 ("stderr", &local_stderr_buf, state->stderr_buf, error)) goto out; @@ -1925,5 +1930,14 @@ g_subprocess_communicate_utf8_finish (GSubprocess *subprocess, ret = TRUE; out: g_object_unref (result); + + if (ret && stdout_buf != NULL) + *stdout_buf = g_steal_pointer (&local_stdout_buf); + if (ret && stderr_buf != NULL) + *stderr_buf = g_steal_pointer (&local_stderr_buf); + + g_free (local_stderr_buf); + g_free (local_stdout_buf); + return ret; }