diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2014-03-17 08:19:09 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2014-03-17 10:53:49 -0700 |
commit | 37794e2e236fbbc4a1a55724df3bb1ef160a9ae7 (patch) | |
tree | 54130255d041e1019fb48eb5e2a7fb35b634f249 /dive.h | |
parent | 60ceb8ebc1afb196e66f7decb494c9e4e67fed7a (diff) | |
download | subsurface-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 'dive.h')
-rw-r--r-- | dive.h | 25 |
1 files changed, 21 insertions, 4 deletions
@@ -600,13 +600,30 @@ static inline struct dive *get_dive(int nr) return dive_table.dives[nr]; } +static inline unsigned int number_of_computers(struct dive *dive) +{ + unsigned int total_number = 0; + struct divecomputer *dc = &dive->dc; + + if (!dive) + return 1; + + do { + total_number++; + dc = dc->next; + } while (dc); + return total_number; +} + static inline struct divecomputer *get_dive_dc(struct dive *dive, int nr) { - struct divecomputer *dc = NULL; - if (nr >= 0) - dc = &dive->dc; - while (nr-- > 0) + struct divecomputer *dc = &dive->dc; + + while (nr-- > 0) { dc = dc->next; + if (!dc) + return &dive->dc; + } return dc; } |