aboutsummaryrefslogtreecommitdiffstats
path: root/profile.c
diff options
context:
space:
mode:
authorGravatar Linus Torvalds <torvalds@linux-foundation.org>2014-03-17 08:19:09 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2014-03-17 10:53:49 -0700
commit37794e2e236fbbc4a1a55724df3bb1ef160a9ae7 (patch)
tree54130255d041e1019fb48eb5e2a7fb35b634f249 /profile.c
parent60ceb8ebc1afb196e66f7decb494c9e4e67fed7a (diff)
downloadsubsurface-37794e2e236fbbc4a1a55724df3bb1ef160a9ae7.tar.gz
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>
Diffstat (limited to 'profile.c')
-rw-r--r--profile.c32
1 files changed, 8 insertions, 24 deletions
diff --git a/profile.c b/profile.c
index 6cf137edb..2102179f2 100644
--- a/profile.c
+++ b/profile.c
@@ -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;
-}
-
-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);
+ /* Reset 'dc_number' if we've switched dives and it is now out of range */
+ if (i >= max)
+ dc_number = i = 0;
- /* 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)