Fix a number of resource leaks

Free memory returned from parse_mkvi_value()
Free memory returned from printGPSCoords()
Free memory allocated in added_list and removed_list
Free memory allocated when adding suffix to dive site name
Free memory allocated in cache_deco_state()
Free memory allocated in build_filename()
Free memory allocated in get_utf8()
Free memory allocated in alloc_dive()
Free memory allocated as cache but never used

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2015-06-21 20:24:07 -07:00
parent 004705e33e
commit f5726c3d18
8 changed files with 26 additions and 11 deletions

8
file.c
View file

@ -547,11 +547,12 @@ int parse_txt_file(const char *filename, const char *csv)
struct divecomputer *dc;
struct tm cur_tm;
if (sscanf(parse_mkvi_value(memtxt.buffer, "Dive started at"), "%d-%d-%d %d:%d:%d",
&y, &m, &d, &hh, &mm, &ss) != 6) {
value = parse_mkvi_value(memtxt.buffer, "Dive started at");
if (sscanf(value, "%d-%d-%d %d:%d:%d", &y, &m, &d, &hh, &mm, &ss) != 6) {
free(value);
return -1;
}
free(value);
cur_tm.tm_year = y;
cur_tm.tm_mon = m - 1;
cur_tm.tm_mday = d;
@ -622,6 +623,7 @@ int parse_txt_file(const char *filename, const char *csv)
*/
if (readfile(csv, &memcsv) < 0) {
free(dive);
return report_error(translate("gettextFromC", "Poseidon import failed: unable to read '%s'"), csv);
}
lineptr = memcsv.buffer;

View file

@ -461,7 +461,10 @@ static dc_status_t libdc_header_parser(dc_parser_t *parser, struct device_data_t
}
// Parse the divetime.
dev_info(devdata, translate("gettextFromC", "Dive %d: %s"), import_dive_number, get_dive_date_c_string(dive->when));
const char *date_string = get_dive_date_c_string(dive->when);
dev_info(devdata, translate("gettextFromC", "Dive %d: %s"), import_dive_number, date_string);
free((void *)date_string);
unsigned int divetime = 0;
rc = dc_parser_get_field(parser, DC_FIELD_DIVETIME, 0, &divetime);
if (rc != DC_STATUS_SUCCESS && rc != DC_STATUS_UNSUPPORTED) {

View file

@ -184,9 +184,10 @@ static void parse_dive_gps(char *line, struct membuffer *str, void *_dive)
} else {
if (dive_site_has_gps_location(ds) &&
(ds->latitude.udeg != latitude.udeg || ds->longitude.udeg != longitude.udeg)) {
const char *coords = printGPSCoords(latitude.udeg, longitude.udeg);
// we have a dive site that already has GPS coordinates
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "multiple gps locations for this dive site; also %s\n"),
printGPSCoords(latitude.udeg, longitude.udeg));
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "multiple gps locations for this dive site; also %s\n"), coords);
free((void *)coords);
}
ds->latitude = latitude;
ds->longitude = longitude;
@ -218,6 +219,7 @@ static void parse_dive_location(char *line, struct membuffer *str, void *_dive)
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "additional name for site: %s\n"), name);
}
}
free(name);
}
static void parse_dive_divemaster(char *line, struct membuffer *str, void *_dive)

View file

@ -1204,8 +1204,9 @@ static void gps_in_dive(char *buffer, struct dive *dive)
fprintf(stderr, "dive site uuid in dive, but gps location (%10.6f/%10.6f) different from dive location (%10.6f/%10.6f)\n",
ds->latitude.udeg / 1000000.0, ds->longitude.udeg / 1000000.0,
latitude.udeg / 1000000.0, longitude.udeg / 1000000.0);
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "multiple gps locations for this dive site; also %s\n"),
printGPSCoords(latitude.udeg, longitude.udeg));
const char *coords = printGPSCoords(latitude.udeg, longitude.udeg);
ds->notes = add_to_string(ds->notes, translate("gettextFromC", "multiple gps locations for this dive site; also %s\n"), coords);
free((void *)coords);
} else {
fprintf(stderr, "let's add the gps coordinates to divesite with uuid %8x and name %s\n", ds->uuid, ds->name ?: "(none)");
ds->latitude = latitude;
@ -1220,6 +1221,7 @@ static void add_dive_site(char *ds_name, struct dive *dive)
{
static int suffix = 1;
char *buffer = ds_name;
char *to_free = NULL;
fprintf(stderr, "add_dive_site with name %s\n", buffer);
int size = trimspace(buffer);
if(size) {
@ -1238,7 +1240,7 @@ static void add_dive_site(char *ds_name, struct dive *dive)
// get a lot of dives with identical names (the autogenerated fixes).
// So in this case modify the name to make it unique
int name_size = strlen(buffer) + 10; // 8 digits - enough for 100 million sites
buffer = malloc(name_size);
to_free = buffer = malloc(name_size);
do {
suffix++;
snprintf(buffer, name_size, "%s %8d", ds_name, suffix);
@ -1276,6 +1278,7 @@ static void add_dive_site(char *ds_name, struct dive *dive)
dive->dive_site_uuid = create_dive_site(buffer);
}
}
free(to_free);
}
static void gps_picture_location(char *buffer, struct picture *pic)

View file

@ -837,6 +837,7 @@ bool trial_ascent(int trial_depth, int stoplevel, int avg_depth, int bottom_time
trial_depth -= deltad;
}
restore_deco_state(trial_cache);
free(trial_cache);
return clear_to_ascend;
}

View file

@ -882,6 +882,7 @@ void DivePlannerPointsModel::createPlan(bool replanCopy)
//TODO: C-based function here?
bool did_deco = plan(&diveplan, &cache, isPlanner(), true);
free(cache);
if (!current_dive || displayed_dive.id != current_dive->id) {
// we were planning a new dive, not re-planning an existing on
record_dive(clone_dive(&displayed_dive));

View file

@ -1184,6 +1184,8 @@ void MainTab::saveTags()
taglist_free(mydive->tag_list);
mydive->tag_list = new_tag_list;
);
taglist_free(added_list);
taglist_free(removed_list);
}
// buddy and divemaster are represented in the UI just like the tags, but the internal

View file

@ -534,6 +534,7 @@ static bool uemis_get_answer(const char *path, char *request, int n_param_in,
snprintf(fl, 13, "ANS%d.TXT", assembling_mbuf ? filenr - 2 : filenr - 1);
ans_path = build_filename(build_filename(path, "ANS"), fl);
ans_file = subsurface_open(ans_path, O_RDONLY, 0666);
free(ans_path);
size = bytes_available(ans_file);
if (size > 3) {
char *buf;
@ -564,6 +565,7 @@ static bool uemis_get_answer(const char *path, char *request, int n_param_in,
snprintf(fl, 13, "ANS%d.TXT", filenr - 1);
ans_path = build_filename(build_filename(path, "ANS"), fl);
ans_file = subsurface_open(ans_path, O_RDONLY, 0666);
free(ans_path);
size = bytes_available(ans_file);
if (size > 3) {
int r;
@ -578,12 +580,11 @@ static bool uemis_get_answer(const char *path, char *request, int n_param_in,
buffer_add(&mbuf, &mbuf_size, buf);
show_progress(buf, what);
#if UEMIS_DEBUG & 8
fprintf(debugfile, "::r %s \"%s\"\n", ans_path, buf);
fprintf(debugfile, "::r %s \"%s\"\n", fl, buf);
#endif
}
size -= 3;
close(ans_file);
free(ans_path);
} else {
ismulti = false;
}