From 23658b55c83666163f359f83703418d1dfa43728 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Tue, 4 Mar 2014 18:58:27 +0100 Subject: [PATCH] Fix a subtle bug in the previous commit. The workaround in the previous commit has a bug. If after a reset, the first entry happens to be located near the end of the TOC, then after a few dives, the TOC will overflow and new entries will be written at the start of the TOC. But the current algorithm starts scanning the TOC from the start and abort the scan as soon as an empty entry is found. Thus if there are less than 256 dives present, those entries near the end will never be reached. We now ignore all uninitialized entries, when searching for the most recent dive. An explicit safety check is added in case dives are unexpectedly interleaved with empty entries. --- src/hw_ostc3.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/hw_ostc3.c b/src/hw_ostc3.c index a2bac63..f9f1921 100644 --- a/src/hw_ostc3.c +++ b/src/hw_ostc3.c @@ -374,12 +374,8 @@ hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, voi unsigned int offset = i * RB_LOGBOOK_SIZE; // Ignore uninitialized header entries. - if (array_isequal (header + offset, RB_LOGBOOK_SIZE, 0xFF)) { - if (count) - break; - else - continue; - } + if (array_isequal (header + offset, RB_LOGBOOK_SIZE, 0xFF)) + continue; // Get the internal dive number. unsigned int current = array_uint16_le (header + offset + 80); @@ -399,6 +395,14 @@ hw_ostc3_device_foreach (dc_device_t *abstract, dc_dive_callback_t callback, voi unsigned int idx = (latest + RB_LOGBOOK_COUNT - i) % RB_LOGBOOK_COUNT; unsigned int offset = idx * RB_LOGBOOK_SIZE; + // Uninitialized header entries should no longer be present at this + // stage, unless the dives are interleaved with empty entries. But + // that's something we don't support at all. + if (array_isequal (header + offset, RB_LOGBOOK_SIZE, 0xFF)) { + WARNING (abstract->context, "Unexpected empty header found."); + break; + } + // Get the firmware version. unsigned int firmware = array_uint16_be (header + offset + 0x30);