aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorGravatar Tim Segers <tsegers@pm.me>2022-10-07 14:38:25 +0200
committerGravatar Tim Segers <tsegers@pm.me>2022-10-07 14:38:25 +0200
commitdb698a50a443388de1ad86d539d8b59aadd71bc9 (patch)
tree3aa5b2de572630a711714c40e4fd5f72552914cc
parent7b719915552d037e04a3cc7278267efa4659c7cf (diff)
downloadopendeco-db698a50a443388de1ad86d539d8b59aadd71bc9.tar.gz
Prevent crash when total time is less than descent time
-rw-r--r--opendeco.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/opendeco.c b/opendeco.c
index b9bc44e..9231ab3 100644
--- a/opendeco.c
+++ b/opendeco.c
@@ -52,10 +52,10 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
switch (key) {
case 'd':
- arguments->depth = arg ? atof(arg) : 0;
+ arguments->depth = arg ? atof(arg) : -1;
break;
case 't':
- arguments->time = arg ? atof(arg) : 0;
+ arguments->time = arg ? atof(arg) : -1;
break;
case 'g':
arguments->gas = arg;
@@ -70,7 +70,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
arguments->gfhigh = arg ? atoi(arg) : 100;
break;
case ARGP_KEY_END:
- if (arguments->depth == 0 || arguments->time == 0) {
+ if (arguments->depth < 0 || arguments->time < 0) {
argp_state_help(state, stderr, ARGP_HELP_USAGE);
argp_failure(state, 1, 0, "Options -d and -t are required. See --help for more information");
exit(ARGP_ERR_UNKNOWN);
@@ -151,8 +151,8 @@ int main(int argc, char *argv[])
/* argp */
struct arguments arguments;
- arguments.depth = 0;
- arguments.time = 0;
+ arguments.depth = -1;
+ arguments.time = -1;
arguments.gas = "Air";
arguments.gflow = 30;
arguments.gfhigh = 75;
@@ -178,10 +178,11 @@ int main(int argc, char *argv[])
/* simulate dive */
double descent_time = msw_to_bar(arguments.depth) / dec_per_min;
+ double bottom_time = max(1, arguments.time - descent_time);
waypoint_t waypoints[] = {
- {.depth = abs_depth(msw_to_bar(arguments.depth)), .time = descent_time, &bottom_gas},
- {.depth = abs_depth(msw_to_bar(arguments.depth)), .time = arguments.time - descent_time, &bottom_gas},
+ {.depth = abs_depth(msw_to_bar(arguments.depth)), .time = descent_time, &bottom_gas},
+ {.depth = abs_depth(msw_to_bar(arguments.depth)), .time = bottom_time, &bottom_gas},
};
print_planhead();