mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Cleanup: clean up resource handling in ostctools.c
Free resources allocated by alloc_dive() with free_dive(). Don't allocate and re-allocate a fixed two byte buffer on the heap. Indirectly this fixes CID 216616 Suggested-by; Berthold Stoeger <bstoeger@mail.tuwien.ac.at> Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
0b47757f2a
commit
30566135aa
1 changed files with 11 additions and 19 deletions
|
@ -44,7 +44,8 @@ void ostctools_import(const char *file, struct dive_table *divetable, struct tri
|
||||||
FILE *archive;
|
FILE *archive;
|
||||||
device_data_t *devdata = calloc(1, sizeof(device_data_t));
|
device_data_t *devdata = calloc(1, sizeof(device_data_t));
|
||||||
dc_family_t dc_fam;
|
dc_family_t dc_fam;
|
||||||
unsigned char *buffer = calloc(65536, 1), *uc_tmp;
|
unsigned char *buffer = calloc(65536, 1);
|
||||||
|
unsigned char uc_tmp[2];
|
||||||
char *tmp;
|
char *tmp;
|
||||||
struct dive *ostcdive = alloc_dive();
|
struct dive *ostcdive = alloc_dive();
|
||||||
dc_status_t rc = 0;
|
dc_status_t rc = 0;
|
||||||
|
@ -56,49 +57,40 @@ void ostctools_import(const char *file, struct dive_table *divetable, struct tri
|
||||||
// Open the archive
|
// Open the archive
|
||||||
if ((archive = subsurface_fopen(file, "rb")) == NULL) {
|
if ((archive = subsurface_fopen(file, "rb")) == NULL) {
|
||||||
report_error(failed_to_read_msg, file);
|
report_error(failed_to_read_msg, file);
|
||||||
free(ostcdive);
|
free_dive(ostcdive);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read dive number from the log
|
// Read dive number from the log
|
||||||
uc_tmp = calloc(2, 1);
|
|
||||||
if (fseek(archive, 258, 0) == -1) {
|
if (fseek(archive, 258, 0) == -1) {
|
||||||
report_error(failed_to_read_msg, file);
|
report_error(failed_to_read_msg, file);
|
||||||
free(uc_tmp);
|
free_dive(ostcdive);
|
||||||
free(ostcdive);
|
|
||||||
goto close_out;
|
goto close_out;
|
||||||
}
|
}
|
||||||
if (fread(uc_tmp, 1, 2, archive) != 2) {
|
if (fread(uc_tmp, 1, 2, archive) != 2) {
|
||||||
report_error(failed_to_read_msg, file);
|
report_error(failed_to_read_msg, file);
|
||||||
free(uc_tmp);
|
free_dive(ostcdive);
|
||||||
free(ostcdive);
|
|
||||||
goto close_out;
|
goto close_out;
|
||||||
}
|
}
|
||||||
ostcdive->number = uc_tmp[0] + (uc_tmp[1] << 8);
|
ostcdive->number = uc_tmp[0] + (uc_tmp[1] << 8);
|
||||||
free(uc_tmp);
|
|
||||||
|
|
||||||
// Read device's serial number
|
// Read device's serial number
|
||||||
uc_tmp = calloc(2, 1);
|
|
||||||
if (fseek(archive, 265, 0) == -1) {
|
if (fseek(archive, 265, 0) == -1) {
|
||||||
report_error(failed_to_read_msg, file);
|
report_error(failed_to_read_msg, file);
|
||||||
free(uc_tmp);
|
free_dive(ostcdive);
|
||||||
free(ostcdive);
|
|
||||||
goto close_out;
|
goto close_out;
|
||||||
}
|
}
|
||||||
if (fread(uc_tmp, 1, 2, archive) != 2) {
|
if (fread(uc_tmp, 1, 2, archive) != 2) {
|
||||||
report_error(failed_to_read_msg, file);
|
report_error(failed_to_read_msg, file);
|
||||||
free(uc_tmp);
|
free_dive(ostcdive);
|
||||||
free(ostcdive);
|
|
||||||
goto close_out;
|
goto close_out;
|
||||||
}
|
}
|
||||||
serial = uc_tmp[0] + (uc_tmp[1] << 8);
|
serial = uc_tmp[0] + (uc_tmp[1] << 8);
|
||||||
free(uc_tmp);
|
|
||||||
|
|
||||||
// Read dive's raw data, header + profile
|
// Read dive's raw data, header + profile
|
||||||
if (fseek(archive, 456, 0) == -1) {
|
if (fseek(archive, 456, 0) == -1) {
|
||||||
report_error(failed_to_read_msg, file);
|
report_error(failed_to_read_msg, file);
|
||||||
free(uc_tmp);
|
free_dive(ostcdive);
|
||||||
free(ostcdive);
|
|
||||||
goto close_out;
|
goto close_out;
|
||||||
}
|
}
|
||||||
while ((c = getc(archive)) != EOF) {
|
while ((c = getc(archive)) != EOF) {
|
||||||
|
@ -109,7 +101,7 @@ void ostctools_import(const char *file, struct dive_table *divetable, struct tri
|
||||||
}
|
}
|
||||||
if (ferror(archive)) {
|
if (ferror(archive)) {
|
||||||
report_error(failed_to_read_msg, file);
|
report_error(failed_to_read_msg, file);
|
||||||
free(ostcdive);
|
free_dive(ostcdive);
|
||||||
goto close_out;
|
goto close_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +119,7 @@ void ostctools_import(const char *file, struct dive_table *divetable, struct tri
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
|
report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
|
||||||
free(ostcdive);
|
free_dive(ostcdive);
|
||||||
goto close_out;
|
goto close_out;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -158,7 +150,7 @@ void ostctools_import(const char *file, struct dive_table *divetable, struct tri
|
||||||
ret = ostc_prepare_data(model, dc_fam, devdata);
|
ret = ostc_prepare_data(model, dc_fam, devdata);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
|
report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
|
||||||
free(ostcdive);
|
free_dive(ostcdive);
|
||||||
goto close_out;
|
goto close_out;
|
||||||
}
|
}
|
||||||
tmp = calloc(strlen(devdata->vendor) + strlen(devdata->model) + 28, 1);
|
tmp = calloc(strlen(devdata->vendor) + strlen(devdata->model) + 28, 1);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue