From 5a66ac7698afbe82e3d55549fd63590918d0a3d9 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Mon, 29 Aug 2016 19:05:30 -0700 Subject: Make sure DC_FIELD_TANK starts from a clean slate for each cylinder We used to clear the 'dc_tank_t' for each dive, but then only clear the volume field in between each cylinder. That means that if the libdivecomputer back-end does not touch a field, it might contain the stale value from the previous tank information. I'm not sure this is actually much of an issue, since I'd expect back-ends do seem to initialize the fields fully (at least the EON Steel back-end does). But it's inconsistent. Also, the code was actually buggy because of the odd indentation: it would only ask for new tank information up to 'ntanks' tanks, but because of the final fixup that was done outside of the conditional, it would actually update the cylinder begin/end pressure data *beyond* 'ntanks', and just re-use the last libdivecomputer data for the rest of the cylinders. Again, in practice, that probably never really happened, but it is a real bug. The fixed-up code actually looks better too, imho, and is one line shorter because of the initialization now being done in one place rather than two. Signed-off-by: Linus Torvalds Signed-off-by: Dirk Hohndel --- core/libdivecomputer.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'core') diff --git a/core/libdivecomputer.c b/core/libdivecomputer.c index 90c2c61cb..92e61b92c 100644 --- a/core/libdivecomputer.c +++ b/core/libdivecomputer.c @@ -99,7 +99,6 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t report_error("different number of gases (%d) and tanks (%d)", ngases, ntanks); } } - dc_tank_t tank = { 0 }; #endif for (i = 0; i < ngases; i++) { @@ -136,8 +135,8 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t dive->cylinder[i].gasmix.he.permille = he; #if DC_VERSION_CHECK(0, 5, 0) && defined(DC_GASMIX_UNKNOWN) - tank.volume = 0.0; if (i < ntanks) { + dc_tank_t tank = { 0 }; rc = dc_parser_get_field(parser, DC_FIELD_TANK, i, &tank); if (rc == DC_STATUS_SUCCESS) { cylinder_t *cyl = dive->cylinder + i; @@ -191,14 +190,14 @@ static int parse_gasmixes(device_data_t *devdata, struct dive *dive, dc_parser_t report_error("gasmix %d for tank %d doesn't match", tank.gasmix, i); } } - } - if (!IS_FP_SAME(tank.volume, 0.0)) - no_volume = false; + if (!IS_FP_SAME(tank.volume, 0.0)) + no_volume = false; - // this new API also gives us the beginning and end pressure for the tank - if (!IS_FP_SAME(tank.beginpressure, 0.0) && !IS_FP_SAME(tank.endpressure, 0.0)) { - dive->cylinder[i].start.mbar = tank.beginpressure * 1000; - dive->cylinder[i].end.mbar = tank.endpressure * 1000; + // this new API also gives us the beginning and end pressure for the tank + if (!IS_FP_SAME(tank.beginpressure, 0.0) && !IS_FP_SAME(tank.endpressure, 0.0)) { + dive->cylinder[i].start.mbar = tank.beginpressure * 1000; + dive->cylinder[i].end.mbar = tank.endpressure * 1000; + } } #endif if (no_volume) { -- cgit v1.2.3-70-g09d2