From f5bbe114a9fb0fd839beacece7ce226bfc10d1c6 Mon Sep 17 00:00:00 2001 From: Rob Norris Date: Mon, 2 Nov 2015 21:54:34 +0000 Subject: [PATCH] [QA] CID#131394: Check result of g_fopen() Add warnings if the LatLonTZ file can not be accessed or opened. --- src/vikutils.c | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/vikutils.c b/src/vikutils.c index 57b5ec44..e90efb4a 100644 --- a/src/vikutils.c +++ b/src/vikutils.c @@ -578,25 +578,32 @@ static void load_ll_tz_dir ( const gchar *dir ) gchar buffer[4096]; long line_num = 0; FILE *ff = g_fopen ( lltz, "r" ); - - while ( fgets ( buffer, 4096, ff ) ) { - line_num++; - gchar **components = g_strsplit (buffer, " ", 3); - guint nn = g_strv_length ( components ); - if ( nn == 3 ) { - double pt[2] = { g_ascii_strtod (components[0], NULL), g_ascii_strtod (components[1], NULL) }; - gchar *timezone = g_strchomp ( components[2] ); - if ( kd_insert ( kd, pt, timezone ) ) - g_critical ( "Insertion problem of %s for line %ld of latlontz.txt", timezone, line_num ); - // NB Don't free timezone as it's part of the kdtree data now - g_free ( components[0] ); - g_free ( components[1] ); - } else { - g_warning ( "Line %ld of latlontz.txt does not have 3 parts", line_num ); + if ( ff ) { + while ( fgets ( buffer, 4096, ff ) ) { + line_num++; + gchar **components = g_strsplit (buffer, " ", 3); + guint nn = g_strv_length ( components ); + if ( nn == 3 ) { + double pt[2] = { g_ascii_strtod (components[0], NULL), g_ascii_strtod (components[1], NULL) }; + gchar *timezone = g_strchomp ( components[2] ); + if ( kd_insert ( kd, pt, timezone ) ) + g_critical ( "Insertion problem of %s for line %ld of latlontz.txt", timezone, line_num ); + // NB Don't free timezone as it's part of the kdtree data now + g_free ( components[0] ); + g_free ( components[1] ); + } else { + g_warning ( "Line %ld of latlontz.txt does not have 3 parts", line_num ); + } + g_free ( components ); } - g_free ( components ); + fclose ( ff ); } - fclose ( ff ); + else { + g_warning ( "Could not open %s", lltz); + } + } + else { + g_warning ( "Could not access %s", lltz); } g_free ( lltz ); } -- 2.39.5