Datatrak parsing

I have some trouble understanding this code, but as there is a clear
bug involved (null dereference), I ask others to verify if I am onto
something. And if datatrak import still works with this patch.

From af2935622b1f00f373ed38c8e3194e25504372b6 Mon Sep 17 00:00:00 2001
From: Miika Turkia <miika.turkia@gmail.com>
Date: Wed, 9 Sep 2015 18:03:45 +0300
Subject: [PATCH] Fix null dereference and parsing logic

Null dereference in the first change is obviously a bug.

The parsing logic I only assume to be wrong and suggest that we discard
dives that are deemed to be bogus.

Signed-off-by: Miika Turkia <miika.turkia@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Miika Turkia 2015-09-09 18:13:49 +03:00 committed by Dirk Hohndel
parent 7a4b046c76
commit 1533191176

View file

@ -158,7 +158,7 @@ static dtrakheader read_file_header(FILE *archivo)
/*
* Parses the dive extracting its data and filling a subsurface's dive structure
*/
static struct dive dt_dive_parser(FILE *archivo, struct dive *dt_dive)
bool dt_dive_parser(FILE *archivo, struct dive *dt_dive)
{
unsigned char n;
int profile_length;
@ -185,8 +185,7 @@ static struct dive dt_dive_parser(FILE *archivo, struct dive *dt_dive)
fread(&lector_bytes[n+1], 1, 1, archivo);
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]));
dt_dive = NULL;
return *dt_dive;
return false;
}
/*
@ -649,7 +648,7 @@ static struct dive dt_dive_parser(FILE *archivo, struct dive *dt_dive)
dt_dive->cylinder[0].end.mbar = dt_dive->cylinder[0].start.mbar -
((dt_dive->cylinder[0].gas_used.mliter / dt_dive->cylinder[0].type.size.mliter) * 1000);
}
return *dt_dive;
return true;
}
void datatrak_import(const char *file, struct dive_table *table)
@ -670,11 +669,14 @@ void datatrak_import(const char *file, struct dive_table *table)
*fileheader = read_file_header(archivo);
while (i < fileheader->divesNum) {
struct dive *ptdive = alloc_dive();
*ptdive = dt_dive_parser(archivo, ptdive);
if (!ptdive)
if (!dt_dive_parser(archivo, ptdive)) {
report_error(translate("gettextFromC", "Error: no dive"));
free(ptdive);
} else {
record_dive(ptdive);
}
i++;
record_dive(ptdive);
}
taglist_cleanup(&g_tag_list);
fclose(archivo);