From 0915c1ce431c08687303253f0c5aa9e87f3ecf9c Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 2 May 2024 09:50:02 +0200 Subject: [PATCH] cleanup: don't allocate device_data_t structure These can all just be local objects. Also, don't overwrite them with 0. We later want to convert the string to std::string, where this would be very sketchy. Signed-off-by: Berthold Stoeger --- core/datatrak.cpp | 22 +++++----- core/downloadfromdcthread.cpp | 1 - .../configuredivecomputerdialog.cpp | 3 +- smtk-import/smartrak.cpp | 43 +++++++++---------- 4 files changed, 32 insertions(+), 37 deletions(-) diff --git a/core/datatrak.cpp b/core/datatrak.cpp index d2fcdd1f5..09190bf3f 100644 --- a/core/datatrak.cpp +++ b/core/datatrak.cpp @@ -104,21 +104,21 @@ static int read_file_header(unsigned char *buffer) * Returns libdc's equivalent model number (also from g_models) or zero if * this a manual dive. */ -static int dtrak_prepare_data(int model, device_data_t *dev_data) +static int dtrak_prepare_data(int model, device_data_t &dev_data) { dc_descriptor_t *d = NULL; int i = 0; while (model != g_models[i].model_num && g_models[i].model_num != 0xEE) i++; - dev_data->model = copy_string(g_models[i].name); - dev_data->vendor = (const char *)malloc(strlen(g_models[i].name) + 1); - sscanf(g_models[i].name, "%[A-Za-z] ", (char *)dev_data->vendor); - dev_data->product = copy_string(strchr(g_models[i].name, ' ') + 1); + dev_data.model = copy_string(g_models[i].name); + dev_data.vendor = (const char *)malloc(strlen(g_models[i].name) + 1); + sscanf(g_models[i].name, "%[A-Za-z] ", (char *)dev_data.vendor); + dev_data.product = copy_string(strchr(g_models[i].name, ' ') + 1); d = get_descriptor(g_models[i].type, g_models[i].libdc_num); if (d) - dev_data->descriptor = d; + dev_data.descriptor = d; else return 0; return g_models[i].libdc_num; @@ -174,8 +174,8 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct struct dive_site *ds; char is_nitrox = 0, is_O2 = 0, is_SCR = 0; - device_data_t *devdata = (device_data_t *)calloc(1, sizeof(device_data_t)); - devdata->log = log; + device_data_t devdata; + devdata.log = log; /* * Parse byte to byte till next dive entry @@ -520,7 +520,7 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct libdc_model = dtrak_prepare_data(tmp_1byte, devdata); if (!libdc_model) report_error(translate("gettextFromC", "[Warning] Manual dive # %d\n"), dt_dive->number); - dt_dive->dc.model = copy_string(devdata->model); + dt_dive->dc.model = copy_string(devdata.model); /* * Air usage, unknown use. Probably allows or deny manually entering gas @@ -543,7 +543,7 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct compl_buffer = (unsigned char *) calloc(18 + profile_length, 1); rc = dt_libdc_buffer(membuf, profile_length, libdc_model, compl_buffer); if (rc == DC_STATUS_SUCCESS) { - libdc_buffer_parser(dt_dive, devdata, compl_buffer, profile_length + 18); + libdc_buffer_parser(dt_dive, &devdata, compl_buffer, profile_length + 18); } else { report_error(translate("gettextFromC", "[Error] Out of memory for dive %d. Abort parsing."), dt_dive->number); free(compl_buffer); @@ -570,11 +570,9 @@ static char *dt_dive_parser(unsigned char *runner, struct dive *dt_dive, struct get_cylinder(dt_dive, 0)->end.mbar = get_cylinder(dt_dive, 0)->start.mbar - ((get_cylinder(dt_dive, 0)->gas_used.mliter / get_cylinder(dt_dive, 0)->type.size.mliter) * 1000); } - free(devdata); return (char *)membuf; bail: free(locality); - free(devdata); return NULL; } diff --git a/core/downloadfromdcthread.cpp b/core/downloadfromdcthread.cpp index eb7ff8bc8..a536c85f2 100644 --- a/core/downloadfromdcthread.cpp +++ b/core/downloadfromdcthread.cpp @@ -202,7 +202,6 @@ void show_computer_list() DCDeviceData::DCDeviceData() { - memset(&data, 0, sizeof(data)); data.log = nullptr; data.diveid = 0; #if defined(BT_SUPPORT) diff --git a/desktop-widgets/configuredivecomputerdialog.cpp b/desktop-widgets/configuredivecomputerdialog.cpp index 03af9121a..b4a25f352 100644 --- a/desktop-widgets/configuredivecomputerdialog.cpp +++ b/desktop-widgets/configuredivecomputerdialog.cpp @@ -145,7 +145,6 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(const QString &filename ui.connectBluetoothButton->setVisible(false); #endif - memset(&device_data, 0, sizeof(device_data)); fill_computer_list(); unsigned int selectedDiveComputerIndex = 0; @@ -267,7 +266,7 @@ ConfigureDiveComputerDialog::ConfigureDiveComputerDialog(const QString &filename OstcFirmwareCheck::OstcFirmwareCheck(const QString &product) : parent(0) { QUrl url; - memset(&devData, 1, sizeof(devData)); + devData = device_data_t(); if (product == "OSTC 3" || product == "OSTC 3+" || product == "OSTC cR" || product == "OSTC Plus") { url = QUrl("http://www.heinrichsweikamp.net/autofirmware/ostc3_changelog.txt"); latestFirmwareHexFile = QString("http://www.heinrichsweikamp.net/autofirmware/ostc3_firmware.hex"); diff --git a/smtk-import/smartrak.cpp b/smtk-import/smartrak.cpp index 0ac692398..027ddad96 100644 --- a/smtk-import/smartrak.cpp +++ b/smtk-import/smartrak.cpp @@ -819,34 +819,33 @@ static dc_descriptor_t *get_data_descriptor(int data_model, dc_family_t data_fam * DC. dc_family_t is certainly known *only* if it is Aladin/Memomouse family * otherwise it will be known after get_data_descriptor call. */ -static dc_status_t prepare_data(int data_model, const char *serial, dc_family_t dc_fam, device_data_t *dev_data) +static dc_status_t prepare_data(int data_model, const char *serial, dc_family_t dc_fam, device_data_t &dev_data) { - dev_data->device = NULL; - dev_data->context = NULL; + dev_data.device = NULL; + dev_data.context = NULL; if (!data_model) { - dev_data->model = copy_string(manual_dc_name); - dev_data->descriptor = NULL; + dev_data.model = copy_string(manual_dc_name); + dev_data.descriptor = NULL; return DC_STATUS_NODEVICE; } - dev_data->descriptor = get_data_descriptor(data_model, dc_fam); - if (dev_data->descriptor) { - dev_data->vendor = dc_descriptor_get_vendor(dev_data->descriptor); - dev_data->product = dc_descriptor_get_product(dev_data->descriptor); - concat(&dev_data->model, "", format_string_std("%s %s", dev_data->vendor, dev_data->product)); - dev_data->devinfo.serial = (uint32_t) lrint(strtod(serial, NULL)); + dev_data.descriptor = get_data_descriptor(data_model, dc_fam); + if (dev_data.descriptor) { + dev_data.vendor = dc_descriptor_get_vendor(dev_data.descriptor); + dev_data.product = dc_descriptor_get_product(dev_data.descriptor); + concat(&dev_data.model, "", format_string_std("%s %s", dev_data.vendor, dev_data.product)); + dev_data.devinfo.serial = (uint32_t) lrint(strtod(serial, NULL)); return DC_STATUS_SUCCESS; } else { - dev_data->model = copy_string("unsupported dive computer"); - dev_data->devinfo.serial = (uint32_t) lrint(strtod(serial, NULL)); + dev_data.model = copy_string("unsupported dive computer"); + dev_data.devinfo.serial = (uint32_t) lrint(strtod(serial, NULL)); return DC_STATUS_UNSUPPORTED; } } -static void device_data_free(device_data_t *dev_data) +static void device_data_free(device_data_t &dev_data) { - free((void *) dev_data->model); - dc_descriptor_free(dev_data->descriptor); - free(dev_data); + free((void *) dev_data.model); + dc_descriptor_free(dev_data.descriptor); } /* @@ -931,7 +930,7 @@ extern "C" void smartrak_import(const char *file, struct divelog *log) return; } while (mdb_table.fetch_row()) { - device_data_t *devdata = (device_data_t *)calloc(1, sizeof(device_data_t)); + device_data_t devdata; dc_family_t dc_fam = DC_FAMILY_NULL; unsigned char *prf_buffer, *hdr_buffer; struct dive *smtkdive = alloc_dive(); @@ -953,18 +952,18 @@ extern "C" void smartrak_import(const char *file, struct divelog *log) dc_fam = DC_FAMILY_UWATEC_ALADIN; } rc = prepare_data(dc_model, (char *)col[coln(DCNUMBER)]->bind_ptr, dc_fam, devdata); - smtkdive->dc.model = copy_string(devdata->model); + smtkdive->dc.model = copy_string(devdata.model); if (rc == DC_STATUS_SUCCESS && mdb_table.get_len(coln(PROFILE))) { prf_buffer = static_cast(mdb_ole_read_full(mdb, col[coln(PROFILE)], &prf_length)); if (prf_length > 0) { - if (dc_descriptor_get_type(devdata->descriptor) == DC_FAMILY_UWATEC_ALADIN || dc_descriptor_get_type(devdata->descriptor) == DC_FAMILY_UWATEC_MEMOMOUSE) + if (dc_descriptor_get_type(devdata.descriptor) == DC_FAMILY_UWATEC_ALADIN || dc_descriptor_get_type(devdata.descriptor) == DC_FAMILY_UWATEC_MEMOMOUSE) hdr_length = 18; std::vector compl_buffer(hdr_length+prf_length); - rc = libdc_buffer_complete(devdata, hdr_buffer, hdr_length, prf_buffer, prf_length, compl_buffer.data()); + rc = libdc_buffer_complete(&devdata, hdr_buffer, hdr_length, prf_buffer, prf_length, compl_buffer.data()); if (rc != DC_STATUS_SUCCESS) { report_error("[Error][smartrak_import]\t- %s - for dive %d", errmsg(rc), smtkdive->number); } else { - rc = libdc_buffer_parser(smtkdive, devdata, compl_buffer.data(), hdr_length + prf_length); + rc = libdc_buffer_parser(smtkdive, &devdata, compl_buffer.data(), hdr_length + prf_length); if (rc != DC_STATUS_SUCCESS) report_error("[Error][libdc]\t\t- %s - for dive %d", errmsg(rc), smtkdive->number); }