From 20f42e7aa890efaa3f6391bc463d751fff6b641d Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sat, 7 Oct 2017 17:03:53 -0700 Subject: Locationinformation: correctly handle strings on the heap Signed-off-by: Dirk Hohndel --- desktop-widgets/locationinformation.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'desktop-widgets/locationinformation.cpp') diff --git a/desktop-widgets/locationinformation.cpp b/desktop-widgets/locationinformation.cpp index 4ca872192..542d268fb 100644 --- a/desktop-widgets/locationinformation.cpp +++ b/desktop-widgets/locationinformation.cpp @@ -142,7 +142,7 @@ void LocationInformationWidget::acceptChanges() { char *uiString; struct dive_site *currentDs; - uiString = ui.diveSiteName->text().toUtf8().data(); + uiString = copy_string(ui.diveSiteName->text().toUtf8().data()); if (get_dive_site_by_uuid(displayed_dive_site.uuid) != NULL) { currentDs = get_dive_site_by_uuid(displayed_dive_site.uuid); } else { @@ -154,27 +154,36 @@ void LocationInformationWidget::acceptChanges() currentDs->longitude = displayed_dive_site.longitude; if (!same_string(uiString, currentDs->name)) { free(currentDs->name); - currentDs->name = copy_string(uiString); + currentDs->name = uiString; + } else { + free(uiString); } - uiString = ui.diveSiteDescription->text().toUtf8().data(); + uiString = copy_string(ui.diveSiteDescription->text().toUtf8().data()); if (!same_string(uiString, currentDs->description)) { free(currentDs->description); - currentDs->description = copy_string(uiString); + currentDs->description = uiString; + } else { + free(uiString); } uiString = copy_string(ui.diveSiteCountry->text().toUtf8().data()); // if the user entered a different contriy, first update the taxonomy // for the displayed dive site; this below will get copied into the currentDs if (!same_string(uiString, taxonomy_get_country(&displayed_dive_site.taxonomy)) && !same_string(uiString, "")) - taxonomy_set_country(&displayed_dive_site.taxonomy, copy_string(uiString), taxonomy_origin::GEOMANUAL); + taxonomy_set_country(&displayed_dive_site.taxonomy, uiString, taxonomy_origin::GEOMANUAL); + else + free(uiString); // now update the currentDs (which we then later copy back ontop of displayed_dive_site copy_dive_site_taxonomy(&displayed_dive_site, currentDs); - uiString = ui.diveSiteNotes->document()->toPlainText().toUtf8().data(); + uiString = copy_string(ui.diveSiteNotes->document()->toPlainText().toUtf8().data()); if (!same_string(uiString, currentDs->notes)) { free(currentDs->notes); - currentDs->notes = copy_string(uiString); + currentDs->notes = uiString; + } else { + free(uiString); } + if (!ui.diveSiteCoordinates->text().isEmpty()) { double lat, lon; parseGpsText(ui.diveSiteCoordinates->text(), &lat, &lon); -- cgit v1.2.3-70-g09d2