gsubprocess: Clear std buf outputs to NULL on failure

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 <withnall@endlessm.com>
This commit is contained in:
Philip Withnall 2018-10-11 11:29:48 +13:00
parent 90ca3b4dd0
commit 4795dadde4

View File

@ -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;
}