From 945898f8fd99d6357b834a546c1a80a9f01d06b0 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 9 Apr 2018 21:18:13 +0200 Subject: [PATCH] Always initialize the output parameters I/O functions with output parameters, should always initialize those output parameters, even when an error is returned. This prevents the (accidental) use of uninitialized variables, whenever the caller forgets to check the return code. As a nice side effect, the use of a local variable guarantees that the underlying I/O implementation will always receive a valid pointer. --- src/iostream.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/iostream.c b/src/iostream.c index 9681d02..954a85f 100644 --- a/src/iostream.c +++ b/src/iostream.c @@ -131,19 +131,41 @@ dc_iostream_set_rts (dc_iostream_t *iostream, unsigned int value) dc_status_t dc_iostream_get_lines (dc_iostream_t *iostream, unsigned int *value) { - if (iostream == NULL || iostream->vtable->get_lines == NULL) - return DC_STATUS_UNSUPPORTED; + dc_status_t status = DC_STATUS_SUCCESS; + unsigned int lines = 0; - return iostream->vtable->get_lines (iostream, value); + if (iostream == NULL || iostream->vtable->get_lines == NULL) { + status = DC_STATUS_UNSUPPORTED; + goto out; + } + + status = iostream->vtable->get_lines (iostream, &lines); + +out: + if (value) + *value = lines; + + return status; } dc_status_t dc_iostream_get_available (dc_iostream_t *iostream, size_t *value) { - if (iostream == NULL || iostream->vtable->get_available == NULL) - return DC_STATUS_UNSUPPORTED; + dc_status_t status = DC_STATUS_SUCCESS; + size_t available = 0; - return iostream->vtable->get_available (iostream, value); + if (iostream == NULL || iostream->vtable->get_available == NULL) { + status = DC_STATUS_UNSUPPORTED; + goto out; + } + + status = iostream->vtable->get_available (iostream, &available); + +out: + if (value) + *value = available; + + return status; } dc_status_t