summaryrefslogtreecommitdiffstats
path: root/divesite.c
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-07-06 12:09:20 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-07-06 12:09:20 -0700
commit6ebeba3c7c142994051061c495850827e51b552f (patch)
tree6eeedd6938a2dfd1b145ea29bbdb1a7b83111fa5 /divesite.c
parent56913191af919186343029a4e5c0cc8ecc345490 (diff)
downloadsubsurface-6ebeba3c7c142994051061c495850827e51b552f.tar.gz
Be more careful freeing taxonomy data
We want to only access data that we consider valid. And we need to clear out pointers to freed memory. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'divesite.c')
-rw-r--r--divesite.c14
1 files changed, 10 insertions, 4 deletions
diff --git a/divesite.c b/divesite.c
index 547b6b0dd..5abab7601 100644
--- a/divesite.c
+++ b/divesite.c
@@ -169,18 +169,23 @@ void copy_dive_site(struct dive_site *orig, struct dive_site *copy)
copy->notes = copy_string(orig->notes);
copy->description = copy_string(orig->description);
copy->uuid = orig->uuid;
- copy->taxonomy.nr = orig->taxonomy.nr;
if (orig->taxonomy.category == NULL) {
+ free_taxonomy(copy->taxonomy.category);
free(copy->taxonomy.category);
copy->taxonomy.category = NULL;
+ copy->taxonomy.nr = 0;
} else {
if (copy->taxonomy.category == NULL)
copy->taxonomy.category = alloc_taxonomy();
for (int i = 0; i < TC_NR_CATEGORIES; i++) {
- free((void *)copy->taxonomy.category[i].value);
- copy->taxonomy.category[i] = orig->taxonomy.category[i];
- copy->taxonomy.category[i].value = copy_string(orig->taxonomy.category[i].value);
+ if (i < copy->taxonomy.nr)
+ free((void *)copy->taxonomy.category[i].value);
+ if (i < orig->taxonomy.nr) {
+ copy->taxonomy.category[i] = orig->taxonomy.category[i];
+ copy->taxonomy.category[i].value = copy_string(orig->taxonomy.category[i].value);
+ }
}
+ copy->taxonomy.nr = orig->taxonomy.nr;
}
}
@@ -197,4 +202,5 @@ void clear_dive_site(struct dive_site *ds)
ds->uuid = 0;
ds->taxonomy.nr = 0;
free_taxonomy(ds->taxonomy.category);
+ ds->taxonomy.category = NULL;
}