[Xfce4-commits] <xfce4-weather-plugin:master> Protect against NULL in data parsing.

Harald Judt noreply at xfce.org
Sun Dec 9 23:44:30 CET 2012


Updating branch refs/heads/master
         to 89a12b770faf16c3dbb0ffa2522c2bde60b3d7c6 (commit)
       from 1eda67307db7d676cb77724805abdfed42dc444b (commit)

commit 89a12b770faf16c3dbb0ffa2522c2bde60b3d7c6
Author: Harald Judt <h.judt at gmx.at>
Date:   Sun Dec 9 19:24:56 2012 +0100

    Protect against NULL in data parsing.

 panel-plugin/weather-data.c    |   15 +++--------
 panel-plugin/weather-parsers.c |   56 +++++++++++++++++++++++----------------
 panel-plugin/weather-parsers.h |    2 +
 panel-plugin/weather.c         |    4 +-
 4 files changed, 41 insertions(+), 36 deletions(-)

diff --git a/panel-plugin/weather-data.c b/panel-plugin/weather-data.c
index 638aa68..48ed0c4 100644
--- a/panel-plugin/weather-data.c
+++ b/panel-plugin/weather-data.c
@@ -36,9 +36,10 @@
 
 #define ROUND_TO_INT(default_format) (round ? "%.0f" : default_format)
 
-#define LOCALE_DOUBLE(value, format)                \
-    (g_strdup_printf(format,                        \
-                     g_ascii_strtod(value, NULL)))
+#define LOCALE_DOUBLE(value, format)                        \
+    (value                                                  \
+     ? g_strdup_printf(format, g_ascii_strtod(value, NULL)) \
+     : g_strdup(""))
 
 #define INTERPOLATE_OR_COPY(var, radian)                                \
     if (ipol)                                                           \
@@ -528,14 +529,6 @@ merge_timeslice(xml_weather *wd,
     if (G_UNLIKELY(wd == NULL))
         return;
 
-    if (wd->timeslices == NULL)
-        wd->timeslices = g_array_sized_new(FALSE, TRUE,
-                                           sizeof(xml_time *), 200);
-
-    g_assert(wd->timeslices != NULL);
-    if (G_UNLIKELY(wd->timeslices == NULL))
-        return;
-
     /* first check if it isn't too old */
     if (difftime(now_t, timeslice->end) > DATA_EXPIRY_TIME) {
         weather_debug("Not merging timeslice because it has expired.");
diff --git a/panel-plugin/weather-parsers.c b/panel-plugin/weather-parsers.c
index b365b3d..03b055b 100644
--- a/panel-plugin/weather-parsers.c
+++ b/panel-plugin/weather-parsers.c
@@ -80,6 +80,10 @@ get_timeslice(xml_weather *wd,
     xml_time *timeslice;
     guint i;
 
+    g_assert(wd != NULL);
+    if (G_UNLIKELY(wd == NULL))
+        return NULL;
+
     for (i = 0; i < wd->timeslices->len; i++) {
         timeslice = g_array_index(wd->timeslices, xml_time *, i);
         if (timeslice &&
@@ -100,6 +104,7 @@ parse_timestring(const gchar *ts,
     struct tm tm;
 
     memset(&t, 0, sizeof(time_t));
+    g_assert(ts != NULL);
     if (G_UNLIKELY(ts == NULL))
         return t;
 
@@ -202,6 +207,24 @@ parse_location(xmlNode *cur_node,
 }
 
 
+xml_weather *
+make_weather_data(void)
+{
+    xml_weather *wd;
+
+    wd = g_slice_new0(xml_weather);
+    if (G_UNLIKELY(wd == NULL))
+        return NULL;
+    wd->timeslices = g_array_sized_new(FALSE, TRUE,
+                                       sizeof(xml_time *), 200);
+    if (G_UNLIKELY(wd->timeslices == NULL)) {
+        g_slice_free(xml_weather, wd);
+        return NULL;
+    }
+    return wd;
+}
+
+
 xml_time *
 make_timeslice(void)
 {
@@ -278,16 +301,7 @@ parse_weather(xmlNode *cur_node,
     if (G_UNLIKELY(wd == NULL))
         return;
 
-    if (G_UNLIKELY(!NODE_IS_TYPE(cur_node, "weatherdata")))
-        return;
-
-    /* create new timeslices array if it doesn't exist yet, otherwise
-       overwrite existing data */
-    if (G_UNLIKELY(wd->timeslices == NULL))
-        wd->timeslices = g_array_sized_new(FALSE, TRUE,
-                                           sizeof(xml_time *), 200);
-    g_assert(wd->timeslices != NULL);
-    if (G_UNLIKELY(wd->timeslices == NULL))
+    if (G_UNLIKELY(cur_node == NULL || !NODE_IS_TYPE(cur_node, "weatherdata")))
         return;
 
     for (cur_node = cur_node->children; cur_node; cur_node = cur_node->next) {
@@ -392,7 +406,8 @@ parse_astro(xmlNode *cur_node)
     xmlNode *child_node, *time_node = NULL;
     xml_astro *astro;
 
-    if (cur_node == NULL || !NODE_IS_TYPE(cur_node, "astrodata"))
+    g_assert(cur_node != NULL);
+    if (G_UNLIKELY(cur_node == NULL || !NODE_IS_TYPE(cur_node, "astrodata")))
         return NULL;
 
     astro = g_slice_new0(xml_astro);
@@ -453,10 +468,7 @@ parse_place(xmlNode *cur_node)
     xml_place *place;
 
     g_assert(cur_node != NULL);
-    if (G_UNLIKELY(cur_node == NULL))
-        return NULL;
-
-    if (!NODE_IS_TYPE(cur_node, "place"))
+    if (G_UNLIKELY(cur_node == NULL || !NODE_IS_TYPE(cur_node, "place")))
         return NULL;
 
     place = g_slice_new0(xml_place);
@@ -475,10 +487,7 @@ parse_altitude(xmlNode *cur_node)
     xml_altitude *alt;
 
     g_assert(cur_node != NULL);
-    if (G_UNLIKELY(cur_node == NULL))
-        return NULL;
-
-    if (!NODE_IS_TYPE(cur_node, "geonames"))
+    if (G_UNLIKELY(cur_node == NULL) || !NODE_IS_TYPE(cur_node, "geonames"))
         return NULL;
 
     alt = g_slice_new0(xml_altitude);
@@ -498,10 +507,7 @@ parse_timezone(xmlNode *cur_node)
     xml_timezone *tz;
 
     g_assert(cur_node != NULL);
-    if (G_UNLIKELY(cur_node == NULL))
-        return NULL;
-
-    if (!NODE_IS_TYPE(cur_node, "timezone"))
+    if (G_UNLIKELY(cur_node == NULL) || !NODE_IS_TYPE(cur_node, "timezone"))
         return NULL;
 
     tz = g_slice_new0(xml_timezone);
@@ -550,6 +556,10 @@ parse_xml_document(SoupMessage *msg,
     xmlNode *root_node;
     gpointer user_data = NULL;
 
+    g_assert(msg != NULL);
+    if (G_UNLIKELY(msg == NULL))
+        return NULL;
+
     doc = get_xml_document(msg);
     if (G_LIKELY(doc)) {
         root_node = xmlDocGetRootElement(doc);
diff --git a/panel-plugin/weather-parsers.h b/panel-plugin/weather-parsers.h
index 53116d8..4a54701 100644
--- a/panel-plugin/weather-parsers.h
+++ b/panel-plugin/weather-parsers.h
@@ -121,6 +121,8 @@ typedef struct {
 } xml_timezone;
 
 
+xml_weather *make_weather_data(void);
+
 xml_time *make_timeslice(void);
 
 time_t parse_timestring(const gchar *ts,
diff --git a/panel-plugin/weather.c b/panel-plugin/weather.c
index 454c836..a229858 100644
--- a/panel-plugin/weather.c
+++ b/panel-plugin/weather.c
@@ -986,7 +986,7 @@ update_weatherdata_with_reset(plugin_data *data, gboolean clear)
     /* clear existing weather data, needed for location changes */
     if (clear && data->weatherdata) {
         xml_weather_free(data->weatherdata);
-        data->weatherdata = g_slice_new0(xml_weather);
+        data->weatherdata = make_weather_data();
 
         /* make use of previously saved data */
         read_cache_file(data);
@@ -1334,7 +1334,7 @@ xfceweather_create_control(XfcePanelPlugin *plugin)
     /* Initialize with sane default values */
     data->plugin = plugin;
     data->units = g_slice_new0(units_config);
-    data->weatherdata = g_slice_new0(xml_weather);
+    data->weatherdata = make_weather_data();
     data->cache_file_max_age = CACHE_FILE_MAX_AGE;
     data->show_scrollbox = TRUE;
     data->scrollbox_lines = 1;


More information about the Xfce4-commits mailing list