Avoid ignoring file read errors

This fixes a bunch of Coverity warnings.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-10-03 10:02:55 -04:00
parent 58fbee7150
commit 08db242e0e
2 changed files with 26 additions and 12 deletions

View file

@ -93,7 +93,8 @@ static struct sample *dtrak_profile(struct dive *dt_dive, FILE *archivo)
struct divecomputer *dc = &dt_dive->dc; struct divecomputer *dc = &dt_dive->dc;
for (i = 1; i <= dt_dive->dc.alloc_samples; i++) { for (i = 1; i <= dt_dive->dc.alloc_samples; i++) {
fread(&lector_bytes, 1, 2, archivo); if (fread(&lector_bytes, 1, 2, archivo) != 2)
return sample;
interval= 20 * (i + 1); interval= 20 * (i + 1);
sample = add_sample(sample, interval, dc); sample = add_sample(sample, interval, dc);
sample->depth.mm = (two_bytes_to_int(lector_bytes[0], lector_bytes[1]) & 0xFFC0) * 1000 / 410; sample->depth.mm = (two_bytes_to_int(lector_bytes[0], lector_bytes[1]) & 0xFFC0) * 1000 / 410;
@ -127,6 +128,7 @@ static struct sample *dtrak_profile(struct dive *dt_dive, FILE *archivo)
sample->setpoint.mbar = calculate_depth_to_mbar(sample->depth.mm, dt_dive->surface_pressure, 0) * o2percent / 100; sample->setpoint.mbar = calculate_depth_to_mbar(sample->depth.mm, dt_dive->surface_pressure, 0) * o2percent / 100;
j++; j++;
} }
bail:
return sample; return sample;
} }
@ -140,7 +142,10 @@ static dtrakheader read_file_header(FILE *archivo)
const short headerbytes = 12; const short headerbytes = 12;
unsigned char *lector = (unsigned char *)malloc(headerbytes); unsigned char *lector = (unsigned char *)malloc(headerbytes);
fread(lector, 1, headerbytes, archivo); if (fread(lector, 1, headerbytes, archivo) != headerbytes) {
free(lector);
return fileheader;
}
if (two_bytes_to_int(lector[0], lector[1]) != 0xA100) { if (two_bytes_to_int(lector[0], lector[1]) != 0xA100) {
report_error(translate("gettextFromC", "Error: the file does not appear to be a DATATRAK divelog")); report_error(translate("gettextFromC", "Error: the file does not appear to be a DATATRAK divelog"));
free(lector); free(lector);
@ -154,6 +159,7 @@ static dtrakheader read_file_header(FILE *archivo)
return fileheader; return fileheader;
} }
#define CHECK(_func, _val) if ((_func) != (_val)) goto bail
/* /*
* Parses the dive extracting its data and filling a subsurface's dive structure * Parses the dive extracting its data and filling a subsurface's dive structure
@ -175,14 +181,14 @@ bool dt_dive_parser(FILE *archivo, struct dive *dt_dive)
* Parse byte to byte till next dive entry * Parse byte to byte till next dive entry
*/ */
n = 0; n = 0;
fread(&lector_bytes[n], 1, 1, archivo); CHECK(fread(&lector_bytes[n], 1, 1, archivo), 1);
while (lector_bytes[n] != 0xA0) while (lector_bytes[n] != 0xA0)
fread(&lector_bytes[n], 1, 1, archivo); CHECK(fread(&lector_bytes[n], 1, 1, archivo), 1);
/* /*
* Found dive header 0xA000, verify second byte * Found dive header 0xA000, verify second byte
*/ */
fread(&lector_bytes[n+1], 1, 1, archivo); CHECK(fread(&lector_bytes[n+1], 1, 1, archivo), 1);
if (two_bytes_to_int(lector_bytes[0], lector_bytes[1]) != 0xA000) { if (two_bytes_to_int(lector_bytes[0], lector_bytes[1]) != 0xA000) {
printf("Error: byte = %4x\n", two_bytes_to_int(lector_bytes[0], lector_bytes[1])); printf("Error: byte = %4x\n", two_bytes_to_int(lector_bytes[0], lector_bytes[1]));
return false; return false;
@ -589,8 +595,8 @@ bool dt_dive_parser(FILE *archivo, struct dive *dt_dive)
* comsumption based on dc model - Useless for Subsurface * comsumption based on dc model - Useless for Subsurface
*/ */
read_bytes(1); read_bytes(1);
fseek(archivo, 6, 1); // jump over 6 bytes whitout known use if (fseek(archivo, 6, 1) != 0) // jump over 6 bytes whitout known use
goto bail;
/* /*
* Profile data length * Profile data length
*/ */
@ -601,7 +607,8 @@ bool dt_dive_parser(FILE *archivo, struct dive *dt_dive)
* 8 x 2 bytes for the tissues saturation useless for subsurface * 8 x 2 bytes for the tissues saturation useless for subsurface
* and other 6 bytes without known use * and other 6 bytes without known use
*/ */
fseek(archivo, 22, 1); if (fseek(archivo, 22, 1) != 0)
goto bail;
if (is_nitrox || is_O2) { if (is_nitrox || is_O2) {
/* /*
@ -649,6 +656,9 @@ bool dt_dive_parser(FILE *archivo, struct dive *dt_dive)
((dt_dive->cylinder[0].gas_used.mliter / dt_dive->cylinder[0].type.size.mliter) * 1000); ((dt_dive->cylinder[0].gas_used.mliter / dt_dive->cylinder[0].type.size.mliter) * 1000);
} }
return true; return true;
bail:
return false;
} }
void datatrak_import(const char *file, struct dive_table *table) void datatrak_import(const char *file, struct dive_table *table)

View file

@ -13,22 +13,26 @@ typedef struct dtrakheader_ {
#define read_bytes(_n) \ #define read_bytes(_n) \
switch (_n) { \ switch (_n) { \
case 1: \ case 1: \
fread (&lector_bytes, sizeof(char), _n, archivo); \ if (fread (&lector_bytes, sizeof(char), _n, archivo) != _n) \
goto bail; \
tmp_1byte = lector_bytes[0]; \ tmp_1byte = lector_bytes[0]; \
break; \ break; \
case 2: \ case 2: \
fread (&lector_bytes, sizeof(char), _n, archivo); \ if (fread (&lector_bytes, sizeof(char), _n, archivo) != _n) \
goto bail; \
tmp_2bytes = two_bytes_to_int (lector_bytes[1], lector_bytes[0]); \ tmp_2bytes = two_bytes_to_int (lector_bytes[1], lector_bytes[0]); \
break; \ break; \
default: \ default: \
fread (&lector_word, sizeof(char), _n, archivo); \ if (fread (&lector_word, sizeof(char), _n, archivo) != _n) \
goto bail; \
tmp_4bytes = four_bytes_to_long(lector_word[3], lector_word[2], lector_word[1], lector_word[0]); \ tmp_4bytes = four_bytes_to_long(lector_word[3], lector_word[2], lector_word[1], lector_word[0]); \
break; \ break; \
} }
#define read_string(_property) \ #define read_string(_property) \
unsigned char *_property##tmp = (unsigned char *)calloc(tmp_1byte + 1, 1); \ unsigned char *_property##tmp = (unsigned char *)calloc(tmp_1byte + 1, 1); \
fread((char *)_property##tmp, 1, tmp_1byte, archivo); \ if (fread((char *)_property##tmp, 1, tmp_1byte, archivo) != tmp_1byte) \
goto bail; \
_property = (unsigned char *)strcat(to_utf8(_property##tmp), ""); \ _property = (unsigned char *)strcat(to_utf8(_property##tmp), ""); \
free(_property##tmp); free(_property##tmp);