From b186846a9e7fa5b542c451ca0917acce2f6a0e4b Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Fri, 18 Jun 2021 20:45:28 +0200 Subject: [PATCH] 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. --- src/atomics_cobalt_parser.c | 4 ++-- src/mclean_extreme_parser.c | 4 ++-- src/shearwater_predator_parser.c | 2 +- src/uwatec_smart_parser.c | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/atomics_cobalt_parser.c b/src/atomics_cobalt_parser.c index 282bbb8..ace7b7a 100644 --- a/src/atomics_cobalt_parser.c +++ b/src/atomics_cobalt_parser.c @@ -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). diff --git a/src/mclean_extreme_parser.c b/src/mclean_extreme_parser.c index f053986..9b5c91c 100644 --- a/src/mclean_extreme_parser.c +++ b/src/mclean_extreme_parser.c @@ -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; diff --git a/src/shearwater_predator_parser.c b/src/shearwater_predator_parser.c index 43eefa8..290001a 100644 --- a/src/shearwater_predator_parser.c +++ b/src/shearwater_predator_parser.c @@ -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). diff --git a/src/uwatec_smart_parser.c b/src/uwatec_smart_parser.c index dd867e4..c28e1d0 100644 --- a/src/uwatec_smart_parser.c +++ b/src/uwatec_smart_parser.c @@ -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); }