[epiphany] sync-records: Convert NULL to empty string for serializable string properties



commit 55c9ec60b44b2c5232a383ed5fdc2f9db127ac00
Author: Gabriel Ivascu <gabrielivascu gnome org>
Date:   Wed Nov 1 14:22:04 2017 +0200

    sync-records: Convert NULL to empty string for serializable string properties
    
    Firefox may afford to work with NULL values since JavaScript is more
    permissive than C, but we cannot afford to crash because of a random
    misplaced NULL value. Better be safe than sorry.

 lib/sync/ephy-history-record.c   |   28 +++++++++++++++-----------
 lib/sync/ephy-open-tabs-record.c |   11 ++++++++++
 src/bookmarks/ephy-bookmark.c    |   40 ++++++++++++++++++++-----------------
 3 files changed, 49 insertions(+), 30 deletions(-)
---
diff --git a/lib/sync/ephy-history-record.c b/lib/sync/ephy-history-record.c
index 1d2385c..62883a1 100644
--- a/lib/sync/ephy-history-record.c
+++ b/lib/sync/ephy-history-record.c
@@ -307,16 +307,18 @@ serializable_serialize_property (JsonSerializable *serializable,
                                  const GValue     *value,
                                  GParamSpec       *pspec)
 {
+  if (G_VALUE_HOLDS_STRING (value) && g_value_get_string (value) == NULL) {
+    JsonNode *node = json_node_new (JSON_NODE_VALUE);
+    json_node_set_string (node, "");
+    return node;
+  }
+
   if (!g_strcmp0 (name, "visits")) {
-    JsonNode *node;
-    JsonArray *array;
-    GSequence *visits;
+    JsonNode *node = json_node_new (JSON_NODE_ARRAY);
+    JsonArray *array = json_array_new ();
+    GSequence *visits = g_value_get_pointer (value);
     GSequenceIter *it;
 
-    node = json_node_new (JSON_NODE_ARRAY);
-    array = json_array_new ();
-    visits = g_value_get_pointer (value);
-
     if (visits != NULL) {
       for (it = g_sequence_get_begin_iter (visits); !g_sequence_iter_is_end (it); it = g_sequence_iter_next 
(it)) {
         EphyHistoryRecordVisit *visit = g_sequence_get (it);
@@ -342,12 +344,14 @@ serializable_deserialize_property (JsonSerializable *serializable,
                                    GParamSpec       *pspec,
                                    JsonNode         *node)
 {
-  if (!g_strcmp0 (name, "visits")) {
-    JsonArray *array;
-    GSequence *visits;
+  if (G_VALUE_HOLDS_STRING (value) && JSON_NODE_HOLDS_NULL (node)) {
+    g_value_set_string (value, "");
+    return TRUE;
+  }
 
-    array = json_node_get_array (node);
-    visits = g_sequence_new ((GDestroyNotify)ephy_history_record_visit_free);
+  if (!g_strcmp0 (name, "visits")) {
+    JsonArray *array = json_node_get_array (node);
+    GSequence *visits = g_sequence_new ((GDestroyNotify)ephy_history_record_visit_free);
 
     for (guint i = 0; i < json_array_get_length (array); i++) {
       JsonObject *object = json_node_get_object (json_array_get_element (array, i));
diff --git a/lib/sync/ephy-open-tabs-record.c b/lib/sync/ephy-open-tabs-record.c
index 755c846..394ac0f 100644
--- a/lib/sync/ephy-open-tabs-record.c
+++ b/lib/sync/ephy-open-tabs-record.c
@@ -220,6 +220,12 @@ serializable_serialize_property (JsonSerializable *serializable,
                                  const GValue     *value,
                                  GParamSpec       *pspec)
 {
+  if (G_VALUE_HOLDS_STRING (value) && g_value_get_string (value) == NULL) {
+    JsonNode *node = json_node_new (JSON_NODE_VALUE);
+    json_node_set_string (node, "");
+    return node;
+  }
+
   if (!g_strcmp0 (name, "tabs")) {
     JsonNode *node = json_node_new (JSON_NODE_ARRAY);
     JsonArray *array = json_array_new ();
@@ -242,6 +248,11 @@ serializable_deserialize_property (JsonSerializable *serializable,
                                    GParamSpec       *pspec,
                                    JsonNode         *node)
 {
+  if (G_VALUE_HOLDS_STRING (value) && JSON_NODE_HOLDS_NULL (node)) {
+    g_value_set_string (value, "");
+    return TRUE;
+  }
+
   if (!g_strcmp0 (name, "tabs")) {
     JsonArray *array;
     GList *tabs = NULL;
diff --git a/src/bookmarks/ephy-bookmark.c b/src/bookmarks/ephy-bookmark.c
index 2af41e0..d84e6e9 100644
--- a/src/bookmarks/ephy-bookmark.c
+++ b/src/bookmarks/ephy-bookmark.c
@@ -520,16 +520,17 @@ serializable_serialize_property (JsonSerializable *serializable,
                                  const GValue     *value,
                                  GParamSpec       *pspec)
 {
-  JsonNode *node = NULL;
+  if (G_VALUE_HOLDS_STRING (value) && g_value_get_string (value) == NULL) {
+    JsonNode *node = json_node_new (JSON_NODE_VALUE);
+    json_node_set_string (node, "");
+    return node;
+  }
 
   if (g_strcmp0 (name, "tags") == 0) {
-    GSequence *tags;
+    JsonNode *node = json_node_new (JSON_NODE_ARRAY);
+    JsonArray *array = json_array_new ();
+    GSequence *tags = g_value_get_pointer (value);
     GSequenceIter *iter;
-    JsonArray *array;
-
-    node = json_node_new (JSON_NODE_ARRAY);
-    array = json_array_new ();
-    tags = g_value_get_pointer (value);
 
     if (tags != NULL) {
       for (iter = g_sequence_get_begin_iter (tags);
@@ -540,13 +541,15 @@ serializable_serialize_property (JsonSerializable *serializable,
     }
 
     json_node_set_array (node, array);
-  } else if (!g_strcmp0 (name, "time-added")) {
-    /* This is not a Firefox bookmark property, skip it.  */
-  } else {
-    node = json_serializable_default_serialize_property (serializable, name, value, pspec);
+
+    return node;
   }
 
-  return node;
+  /* This is not a Firefox bookmark property, skip it. */
+  if (!g_strcmp0 (name, "time-added"))
+    return NULL;
+
+  return json_serializable_default_serialize_property (serializable, name, value, pspec);
 }
 
 static gboolean
@@ -556,15 +559,16 @@ serializable_deserialize_property (JsonSerializable *serializable,
                                    GParamSpec       *pspec,
                                    JsonNode         *node)
 {
+  if (G_VALUE_HOLDS_STRING (value) && JSON_NODE_HOLDS_NULL (node)) {
+    g_value_set_string (value, "");
+    return TRUE;
+  }
+
   if (g_strcmp0 (name, "tags") == 0) {
-    GSequence *tags;
-    JsonArray *array;
+    GSequence *tags = g_sequence_new (g_free);
+    JsonArray *array = json_node_get_array (node);
     const char *tag;
 
-    g_assert (JSON_NODE_HOLDS_ARRAY (node));
-    array = json_node_get_array (node);
-    tags = g_sequence_new (g_free);
-
     for (gsize i = 0; i < json_array_get_length (array); i++) {
       tag = json_node_get_string (json_array_get_element (array, i));
       g_sequence_insert_sorted (tags, g_strdup (tag),


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]