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>
This commit is contained in:
Dirk Hohndel 2015-07-06 12:09:20 -07:00
parent 56913191af
commit 6ebeba3c7c
2 changed files with 11 additions and 4 deletions

View file

@ -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;
}

View file

@ -631,6 +631,7 @@ void MainWindow::setupForAddAndPlan(const char *model)
{
// clean out the dive and give it an id and the correct dc model
clear_dive(&displayed_dive);
clear_dive_site(&displayed_dive_site);
displayed_dive.id = dive_getUniqID(&displayed_dive);
displayed_dive.when = QDateTime::currentMSecsSinceEpoch() / 1000L + gettimezoneoffset() + 3600;
displayed_dive.dc.model = model; // don't translate! this is stored in the XML file