[PATCH 12/14] Cleanup: consistenty check return value of iostream functions

Dirk Hohndel dirk at hohndel.org
Wed Jan 3 07:44:53 PST 2018


> On Jan 3, 2018, at 7:34 AM, Jef Driesen <jef at libdivecomputer.org> wrote:
> 
> On 29-12-17 01:35, Dirk Hohndel wrote:
>> --- a/src/oceanic_vtpro.c
>> +++ b/src/oceanic_vtpro.c
>> @@ -269,9 +269,13 @@ oceanic_vtpro_calibrate (oceanic_vtpro_device_t *device)
>>  	// device needs approximately 6 seconds to respond.
>>  	unsigned char answer[2] = {0};
>>  	unsigned char command[2] = {0x18, 0x00};
>> -	dc_iostream_set_timeout (device->iostream, 9000);
>> -	dc_status_t rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
>> -	dc_iostream_set_timeout (device->iostream, 3000);
>> +	dc_status_t rc = dc_iostream_set_timeout (device->iostream, 9000);
>> +	if (rc != DC_STATUS_SUCCESS)
>> +		return rc;
>> +	rc = oceanic_vtpro_transfer (device, command, sizeof (command), answer, sizeof (answer));
>> +	if (rc != DC_STATUS_SUCCESS)
>> +		return rc;
>> +	rc = dc_iostream_set_timeout (device->iostream, 3000);
>>  	if (rc != DC_STATUS_SUCCESS)
>>  		return rc;
> 
> This fix changes the behavior slightly, but that's no longer an issue now.
> 
> The original intention was that the timeout is always restored, even if an error occurred. In the past this was important because the function was exposed in the public api and the connection could be re-used after a failed call. Nowadays, this is no longer the case,  because it's only called internally, and at that point any error is fatal (e.g. no handle is returned to the caller, and thus re-using it is simply not possible).

Ouch, I missed that part. It would be easy to fix that - simply to make sure that we aren't causing unintended side effects.

/D


More information about the devel mailing list