Fix negative depth values

The difference between two unsigned integers can be negative. To avoid
ending up with some very large positive values, an explicit cast to a
signed integer is required.

Depths are normally expected to be always positive, but near the surface
the pressure will be very close to the atmospheric pressure. Therefore
small negative values are not unusual.
This commit is contained in:
Jef Driesen 2021-06-18 20:45:28 +02:00
parent da2446283a
commit b186846a9e
4 changed files with 6 additions and 6 deletions

View File

@ -149,7 +149,7 @@ atomics_cobalt_parser_get_field (dc_parser_t *abstract, dc_field_type_t type, un
*((unsigned int *) value) = array_uint16_le (p + 0x58) * 60;
break;
case DC_FIELD_MAXDEPTH:
*((double *) value) = (array_uint16_le (p + 0x56) - atmospheric) * (BAR / 1000.0) / parser->hydrostatic;
*((double *) value) = (signed int)(array_uint16_le (p + 0x56) - atmospheric) * (BAR / 1000.0) / parser->hydrostatic;
break;
case DC_FIELD_GASMIX_COUNT:
case DC_FIELD_TANK_COUNT:
@ -266,7 +266,7 @@ atomics_cobalt_parser_samples_foreach (dc_parser_t *abstract, dc_sample_callback
// Depth (1/1000 bar).
unsigned int depth = array_uint16_le (data + offset + 0);
sample.depth = (depth - atmospheric) * (BAR / 1000.0) / parser->hydrostatic;
sample.depth = (signed int)(depth - atmospheric) * (BAR / 1000.0) / parser->hydrostatic;
if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata);
// Pressure (1 psi).

View File

@ -177,10 +177,10 @@ mclean_extreme_parser_get_field(dc_parser_t *abstract, dc_field_type_t type, uns
*((unsigned int *)value) = array_uint32_le(abstract->data + SZ_CFG + 0x000C) - array_uint32_le(abstract->data + SZ_CFG + 0x0000);
break;
case DC_FIELD_MAXDEPTH:
*((double *)value) = 0.01 * (array_uint16_le(abstract->data + SZ_CFG + 0x0016) - psurf) / density;
*((double *)value) = 0.01 * (signed int)(array_uint16_le(abstract->data + SZ_CFG + 0x0016) - psurf) / density;
break;
case DC_FIELD_AVGDEPTH:
*((double *)value) = 0.01 * (array_uint16_le(abstract->data + SZ_CFG + 0x0018) - psurf) / density;
*((double *)value) = 0.01 * (signed int)(array_uint16_le(abstract->data + SZ_CFG + 0x0018) - psurf) / density;
break;
case DC_FIELD_SALINITY:
salinity->density = density * 1000.0;

View File

@ -809,7 +809,7 @@ shearwater_predator_parser_samples_foreach (dc_parser_t *abstract, dc_sample_cal
// Depth (absolute pressure in millibar)
unsigned int depth = array_uint16_be (data + idx + 1);
sample.depth = (depth - parser->atmospheric) * (BAR / 1000.0) / (parser->density * GRAVITY);
sample.depth = (signed int)(depth - parser->atmospheric) * (BAR / 1000.0) / (parser->density * GRAVITY);
if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata);
// Temperature (1/10 °C).

View File

@ -1233,7 +1233,7 @@ uwatec_smart_parse (uwatec_smart_parser_t *parser, dc_sample_callback_t callback
}
if (have_depth) {
sample.depth = (depth - depth_calibration) / 50.0 / salinity;
sample.depth = (signed int)(depth - depth_calibration) / 50.0 / salinity;
if (callback) callback (DC_SAMPLE_DEPTH, sample, userdata);
}