aboutsummaryrefslogtreecommitdiffstats
path: root/core/ostctools.c
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2019-10-26 14:59:26 -0400
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-10-26 19:17:53 -0700
commit30566135aa8b9fa75b071a2b23927db33aea63b5 (patch)
treeae5bc335ed867e80f39723f4e2bd55b8f8ef7e34 /core/ostctools.c
parent0b47757f2ac93ea39b622c3553fd7a06d13f8e29 (diff)
downloadsubsurface-30566135aa8b9fa75b071a2b23927db33aea63b5.tar.gz
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>
Diffstat (limited to 'core/ostctools.c')
-rw-r--r--core/ostctools.c30
1 files changed, 11 insertions, 19 deletions
diff --git a/core/ostctools.c b/core/ostctools.c
index 02056abcc..c8d7723ce 100644
--- a/core/ostctools.c
+++ b/core/ostctools.c
@@ -44,7 +44,8 @@ void ostctools_import(const char *file, struct dive_table *divetable, struct tri
FILE *archive;
device_data_t *devdata = calloc(1, sizeof(device_data_t));
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;
struct dive *ostcdive = alloc_dive();
dc_status_t rc = 0;
@@ -56,49 +57,40 @@ void ostctools_import(const char *file, struct dive_table *divetable, struct tri
// Open the archive
if ((archive = subsurface_fopen(file, "rb")) == NULL) {
report_error(failed_to_read_msg, file);
- free(ostcdive);
+ free_dive(ostcdive);
goto out;
}
// Read dive number from the log
- uc_tmp = calloc(2, 1);
if (fseek(archive, 258, 0) == -1) {
report_error(failed_to_read_msg, file);
- free(uc_tmp);
- free(ostcdive);
+ free_dive(ostcdive);
goto close_out;
}
if (fread(uc_tmp, 1, 2, archive) != 2) {
report_error(failed_to_read_msg, file);
- free(uc_tmp);
- free(ostcdive);
+ free_dive(ostcdive);
goto close_out;
}
ostcdive->number = uc_tmp[0] + (uc_tmp[1] << 8);
- free(uc_tmp);
// Read device's serial number
- uc_tmp = calloc(2, 1);
if (fseek(archive, 265, 0) == -1) {
report_error(failed_to_read_msg, file);
- free(uc_tmp);
- free(ostcdive);
+ free_dive(ostcdive);
goto close_out;
}
if (fread(uc_tmp, 1, 2, archive) != 2) {
report_error(failed_to_read_msg, file);
- free(uc_tmp);
- free(ostcdive);
+ free_dive(ostcdive);
goto close_out;
}
serial = uc_tmp[0] + (uc_tmp[1] << 8);
- free(uc_tmp);
// Read dive's raw data, header + profile
if (fseek(archive, 456, 0) == -1) {
report_error(failed_to_read_msg, file);
- free(uc_tmp);
- free(ostcdive);
+ free_dive(ostcdive);
goto close_out;
}
while ((c = getc(archive)) != EOF) {
@@ -109,7 +101,7 @@ void ostctools_import(const char *file, struct dive_table *divetable, struct tri
}
if (ferror(archive)) {
report_error(failed_to_read_msg, file);
- free(ostcdive);
+ free_dive(ostcdive);
goto close_out;
}
@@ -127,7 +119,7 @@ void ostctools_import(const char *file, struct dive_table *divetable, struct tri
break;
default:
report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
- free(ostcdive);
+ free_dive(ostcdive);
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);
if (ret == 0) {
report_error(translate("gettextFromC", "Unknown DC in dive %d"), ostcdive->number);
- free(ostcdive);
+ free_dive(ostcdive);
goto close_out;
}
tmp = calloc(strlen(devdata->vendor) + strlen(devdata->model) + 28, 1);