Fix some of the problems reported by cppcheck

Thanks to Christian for running the static code analysis tool against
subsurface...

There were some false positives, a few style issues that I'll ignore for
now, and two actual potential bugs.

First: Don't check unsigned variables for < 0

This has been around for a while and we are lucky that while technically a
bug it still works as expected. Passing a negative idx simply turns it
into a very large unsigned integer which then fails the > dive_table.nr
test. So it still gets a NULL returned. A bug? Yes. Critical? No.

Mismatched allocation and free

This is an actual bug that potentially could cause issues. We allocate
memory with malloc and free it with g_free. Not good.

Reported-by: Cristian Ionescu-Idbohrn <cristian.ionescu-idbohrn@axis.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2012-09-18 19:51:48 -04:00
parent f73e5b7268
commit d14932058f
5 changed files with 7 additions and 7 deletions

2
dive.c
View file

@ -277,7 +277,7 @@ static void sanitize_gasmix(struct gasmix *mix)
/* Sane mix? */ /* Sane mix? */
if (o2 <= 1000 && he <= 1000 && o2+he <= 1000) if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
return; return;
fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he); fprintf(stderr, "Odd gasmix: %u O2 %u He\n", o2, he);
memset(mix, 0, sizeof(*mix)); memset(mix, 0, sizeof(*mix));
} }

2
dive.h
View file

@ -366,7 +366,7 @@ extern struct dive_table dive_table;
extern int selected_dive; extern int selected_dive;
#define current_dive (get_dive(selected_dive)) #define current_dive (get_dive(selected_dive))
static inline struct dive *get_dive(unsigned int nr) static inline struct dive *get_dive(int nr)
{ {
if (nr >= dive_table.nr || nr < 0) if (nr >= dive_table.nr || nr < 0)
return NULL; return NULL;

View file

@ -110,7 +110,7 @@ void dump_selection(void)
int i; int i;
struct dive *dive; struct dive *dive;
printf("currently selected are %d dives:", amount_selected); printf("currently selected are %u dives:", amount_selected);
for_each_dive(i, dive) { for_each_dive(i, dive) {
if (dive->selected) if (dive->selected)
printf(" %d", i); printf(" %d", i);

View file

@ -96,13 +96,13 @@ static void convert_volume_pressure(int ml, int mbar, double *v, double *p)
volume *= bar_to_atm(mbar / 1000.0); volume *= bar_to_atm(mbar / 1000.0);
} }
if (output_units.pressure == PSI) { if (output_units.pressure == PSI)
pressure = mbar_to_PSI(mbar); pressure = mbar_to_PSI(mbar);
} else else
pressure = mbar / 1000.0; pressure = mbar / 1000.0;
*p = pressure;
} }
*v = volume; *v = volume;
*p = pressure;
} }
static int convert_weight(int grams, double *m) static int convert_weight(int grams, double *m)

View file

@ -226,7 +226,7 @@ static gboolean ask_save_changes()
"You have unsaved changes to file: %s \nWould you like to save those before closing the datafile?", "You have unsaved changes to file: %s \nWould you like to save those before closing the datafile?",
existing_filename); existing_filename);
label = gtk_label_new (label_text); label = gtk_label_new (label_text);
g_free(label_text); free(label_text);
} }
gtk_container_add (GTK_CONTAINER (content), label); gtk_container_add (GTK_CONTAINER (content), label);
gtk_widget_show_all (dialog); gtk_widget_show_all (dialog);