]> git.street.me.uk Git - andy/viking.git/blobdiff - src/gpspoint.c
Coverity: Prevent deference after null checks
[andy/viking.git] / src / gpspoint.c
index 77d1f6eb77328d40c598df0bb410432716efe91a..19bec4b3dffaa696ec824940cff392e6db10dd91 100644 (file)
@@ -2,7 +2,7 @@
  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
  *
  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
- * Copyright (C) 2012, Rob Norris <rw_norris@hotmail.com>
+ * Copyright (C) 2012-2013, Rob Norris <rw_norris@hotmail.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -46,7 +46,6 @@ static void a_gpspoint_write_track ( const gpointer id, const VikTrack *t, FILE
 static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type *write_info );
 static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp, FILE *f );
 
-
 /* outline for file gpspoint.c
 
 reading file:
@@ -157,7 +156,7 @@ static gchar *deslashndup ( const gchar *str, guint16 len )
  * No obvious way to test for a 'gpspoint' file,
  *  thus set a flag if any actual tag found during processing of the file
  */
-gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
+gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f, const gchar *dirpath ) {
   VikCoordMode coord_mode = vik_trw_layer_get_coord_mode ( trw );
   gchar *tag_start, *tag_end;
   g_assert ( f != NULL && trw != NULL );
@@ -209,7 +208,9 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
           inside_quote = !inside_quote;
       }
 
-      gpspoint_process_tag ( tag_start, tag_end - tag_start );
+      // Won't have super massively long strings, so potential truncation in cast to gint is acceptable.
+      gint len = (gint)(tag_end - tag_start);
+      gpspoint_process_tag ( tag_start, len );
 
       if (*tag_end == '\0' )
         break;
@@ -222,6 +223,8 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
       VikWaypoint *wp = vik_waypoint_new();
       wp->visible = line_visible;
       wp->altitude = line_altitude;
+      wp->has_timestamp = line_has_timestamp;
+      wp->timestamp = line_timestamp;
 
       vik_coord_load_from_latlon ( &(wp->coord), coord_mode, &line_latlon );
 
@@ -235,8 +238,19 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
       if ( line_description )
         vik_waypoint_set_description ( wp, line_description );
 
-      if ( line_image )
-        vik_waypoint_set_image ( wp, line_image );
+      if ( line_image ) {
+        // Ensure the filename is absolute
+        if ( g_path_is_absolute ( line_image ) )
+          vik_waypoint_set_image ( wp, line_image );
+        else {
+          // Otherwise create the absolute filename from the directory of the .vik file & and the relative filename
+          gchar *full = g_strconcat(dirpath, G_DIR_SEPARATOR_S, line_image, NULL);
+          gchar *absolute = file_realpath_dup ( full ); // resolved into the canonical name
+          vik_waypoint_set_image ( wp, absolute );
+          g_free ( absolute );
+          g_free ( full );
+        }
+      }
 
       if ( line_symbol )
         vik_waypoint_set_symbol ( wp, line_symbol );
@@ -285,6 +299,7 @@ gboolean a_gpspoint_read_file(VikTrwLayer *trw, FILE *f ) {
       tp->has_timestamp = line_has_timestamp;
       tp->timestamp = line_timestamp;
       tp->altitude = line_altitude;
+      vik_trackpoint_set_name ( tp, line_name );
       if (line_extended) {
         tp->speed = line_speed;
         tp->course = line_course;
@@ -450,7 +465,7 @@ static void gpspoint_process_key_and_value ( const gchar *key, gint key_len, con
   {
     line_altitude = g_ascii_strtod(value, NULL);
   }
-  else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T')
+  else if (key_len == 7 && strncasecmp( key, "visible", key_len ) == 0 && value != NULL && value[0] != 'y' && value[0] != 'Y' && value[0] != 't' && value[0] != 'T')
   {
     line_visible = FALSE;
   }
@@ -494,10 +509,12 @@ static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp
 {
   static struct LatLon ll;
   gchar *s_lat, *s_lon;
-  // Sanity clause
-  if ( wp && !(wp->name) ) {
+  // Sanity clauses
+  if ( !wp )
     return;
-  }
+  if ( !(wp->name) )
+    return;
+
   vik_coord_to_latlon ( &(wp->coord), &ll );
   s_lat = a_coords_dtostr(ll.lat);
   s_lon = a_coords_dtostr(ll.lon);
@@ -512,6 +529,8 @@ static void a_gpspoint_write_waypoint ( const gpointer id, const VikWaypoint *wp
     fprintf ( f, " altitude=\"%s\"", s_alt );
     g_free(s_alt);
   }
+  if ( wp->has_timestamp )
+    fprintf ( f, " unixtime=\"%ld\"", wp->timestamp );
   if ( wp->comment )
   {
     gchar *tmp_comment = slashdup(wp->comment);
@@ -575,6 +594,12 @@ static void a_gpspoint_write_trackpoint ( VikTrackpoint *tp, TP_write_info_type
   g_free ( s_lat ); 
   g_free ( s_lon );
 
+  if ( tp->name ) {
+    gchar *name = slashdup(tp->name);
+    fprintf ( f, " name=\"%s\"", name );
+    g_free(name);
+  }
+
   if ( tp->altitude != VIK_DEFAULT_ALTITUDE ) {
     gchar *s_alt = a_coords_dtostr(tp->altitude);
     fprintf ( f, " altitude=\"%s\"", s_alt );