summaryrefslogtreecommitdiffstats
path: root/core/ostctools.c
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2017-12-19 23:34:32 +0100
committerGravatar Lubomir I. Ivanov <neolit123@gmail.com>2017-12-21 00:04:30 +0100
commit3c346bb341854897628117def6f3415ee5740444 (patch)
tree3da27fc846153e834f81a3d88965bb87332a14e1 /core/ostctools.c
parent1c1ca8c1c7a85938f41efb6b1ae2fa50a7f778a7 (diff)
downloadsubsurface-3c346bb341854897628117def6f3415ee5740444.tar.gz
Check return of fread() in core/ostctools.c
Since the corresponding error message appears thrice, it is translated once at the beginning of the function (even in the non-error case). A single-byte fread() was transformed into getc(). Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/ostctools.c')
-rw-r--r--core/ostctools.c28
1 files changed, 22 insertions, 6 deletions
diff --git a/core/ostctools.c b/core/ostctools.c
index 413841ad0..48c86a378 100644
--- a/core/ostctools.c
+++ b/core/ostctools.c
@@ -43,13 +43,14 @@ void ostctools_import(const char *file, struct dive_table *divetable)
char *tmp;
struct dive *ostcdive = alloc_dive();
dc_status_t rc = 0;
- int model, ret, i = 0;
+ int model, ret, i = 0, c;
unsigned int serial;
struct extra_data *ptr;
+ const char *failed_to_read_msg = translate("gettextFromC", "Failed to read '%s'");
// Open the archive
if ((archive = subsurface_fopen(file, "rb")) == NULL) {
- report_error(translate("gettextFromC", "Failed to read '%s'"), file);
+ report_error(failed_to_read_msg, file);
free(ostcdive);
goto out;
}
@@ -57,25 +58,40 @@ void ostctools_import(const char *file, struct dive_table *divetable)
// Read dive number from the log
uc_tmp = calloc(2, 1);
fseek(archive, 258, 0);
- fread(uc_tmp, 1, 2, archive);
+ if (fread(uc_tmp, 1, 2, archive) != 2) {
+ report_error(failed_to_read_msg, file);
+ free(uc_tmp);
+ free(ostcdive);
+ goto out;
+ }
ostcdive->number = uc_tmp[0] + (uc_tmp[1] << 8);
free(uc_tmp);
// Read device's serial number
uc_tmp = calloc(2, 1);
fseek(archive, 265, 0);
- fread(uc_tmp, 1, 2, archive);
+ if (fread(uc_tmp, 1, 2, archive) != 2) {
+ report_error(failed_to_read_msg, file);
+ free(uc_tmp);
+ free(ostcdive);
+ goto out;
+ }
serial = uc_tmp[0] + (uc_tmp[1] << 8);
free(uc_tmp);
// Read dive's raw data, header + profile
fseek(archive, 456, 0);
- while (!feof(archive)) {
- fread(buffer + i, 1, 1, archive);
+ while ((c = getc(archive)) != EOF) {
+ buffer[i] = c;
if (buffer[i] == 0xFD && buffer[i - 1] == 0xFD)
break;
i++;
}
+ if (ferror(archive)) {
+ report_error(failed_to_read_msg, file);
+ free(ostcdive);
+ goto out;
+ }
// Try to determine the dc family based on the header type
if (buffer[2] == 0x20 || buffer[2] == 0x21) {