From 8d9730f74f1071851f734f5014e297519792e058 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sat, 23 Apr 2022 15:01:42 -0700 Subject: [PATCH] core: avoid crash when merging dive with no cylinders Arguably every dive should at least have one cylinder, but an imported dive from divelogs.de might end up without one. Sadly, that breaks assumptions that we make in the cylinder remapping. To work around it, force at least on cylinder to be assumed in the merge code. Signed-off-by: Dirk Hohndel --- CHANGELOG.md | 1 + core/dive.c | 6 ++++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 43ab3a100..3e4558bd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +- core: prevent crash when merging dives without cylinders (as we might get when importing from divelogs.de) - core: work around bug in TecDiving dive computer reporting spurious 0 deg C water temperature in first sample - core: correctly parse DC_FIELD_SALINITY response; fixes incorrect water type with some dive computers, including the Mares Smart - Desktop: Allow more than one media file to be imported from web diff --git a/core/dive.c b/core/dive.c index 03eb82e70..1d20e1421 100644 --- a/core/dive.c +++ b/core/dive.c @@ -2623,8 +2623,10 @@ struct dive *merge_dives(const struct dive *a, const struct dive *b, int offset, MERGE_NONZERO(res, a, b, visibility); copy_pictures(a->pictures.nr ? &a->pictures : &b->pictures, &res->pictures); taglist_merge(&res->tag_list, a->tag_list, b->tag_list); - cylinders_map_a = malloc(a->cylinders.nr * sizeof(*cylinders_map_a)); - cylinders_map_b = malloc(b->cylinders.nr * sizeof(*cylinders_map_b)); + /* if we get dives without any gas / cylinder information in an import, make sure + * that there is at leatst one entry in the cylinder map for that dive */ + cylinders_map_a = malloc(MAX(1, a->cylinders.nr) * sizeof(*cylinders_map_a)); + cylinders_map_b = malloc(MAX(1, b->cylinders.nr) * sizeof(*cylinders_map_b)); merge_cylinders(res, a, b, cylinders_map_a, cylinders_map_b); merge_equipment(res, a, b); merge_temperatures(res, a, b);