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.
This commit is contained in:
Jef Driesen 2018-04-09 21:18:13 +02:00
parent 8957d61f4e
commit 945898f8fd

View File

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