Be more careful about dive computer selection

The selection logic was a bit random: some places would return NULL if
the dive computer index was out of range, others would return the
primary dive computer, and actually moving between dive computers would
just blindly increment and decrement the number.

This always selects the primary computer if the index is out of bounds,
and makes sure we stay in bound when switching beteen dive computers
(but switching between dives can then turn an in-bound number into an
out-of-bounds one)

Fixes #464

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Linus Torvalds 2014-03-17 08:19:09 -07:00 committed by Dirk Hohndel
parent 60ceb8ebc1
commit 37794e2e23
8 changed files with 41 additions and 38 deletions

View file

@ -16,7 +16,7 @@
#include "membuffer.h"
int selected_dive = -1; /* careful: 0 is a valid value */
char dc_number = 0;
unsigned int dc_number = 0;
static struct plot_data *last_pi_entry_new = NULL;
@ -1193,32 +1193,16 @@ void create_plot_info_new(struct dive *dive, struct divecomputer *dc, struct plo
analyze_plot_info(pi);
}
/* make sure you pass this the FIRST dc - it just walks the list */
static int nr_dcs(struct divecomputer *main)
struct divecomputer *select_dc(struct dive *dive)
{
int i = 1;
struct divecomputer *dc = main;
unsigned int max = number_of_computers(dive);
unsigned int i = dc_number;
while ((dc = dc->next) != NULL)
i++;
return i;
}
/* Reset 'dc_number' if we've switched dives and it is now out of range */
if (i >= max)
dc_number = i = 0;
struct divecomputer *select_dc(struct divecomputer *main)
{
int i = dc_number;
struct divecomputer *dc = main;
while (i < 0)
i += nr_dcs(main);
do {
if (--i < 0)
return dc;
} while ((dc = dc->next) != NULL);
/* If we switched dives to one with fewer DC's, reset the dive computer counter */
dc_number = 0;
return main;
return get_dive_dc(dive, i);
}
static void plot_string(struct plot_data *entry, struct membuffer *b, bool has_ndl)