[SATYR PATCH 3/8] Minor json parsing refactorization

Martin Milata mmilata at redhat.com
Mon Aug 26 14:49:02 UTC 2013


Avoid some code repetition, fix a memory leak.

Related to #86.

Signed-off-by: Martin Milata <mmilata at redhat.com>
---
 include/json.h           |  2 +-
 lib/abrt.c               | 20 ++------------------
 lib/core_stacktrace.c    | 19 ++++++-------------
 lib/generic_stacktrace.c | 15 ++++++---------
 lib/json.c               | 10 ++++++++--
 5 files changed, 23 insertions(+), 43 deletions(-)

diff --git a/include/json.h b/include/json.h
index 99370df..af3ddb3 100644
--- a/include/json.h
+++ b/include/json.h
@@ -108,7 +108,7 @@ struct sr_json_value
 };
 
 struct sr_json_value *
-sr_json_parse(const char *json);
+sr_json_parse(const char *json, char **error_message);
 
 struct sr_json_value *
 sr_json_parse_ex(struct sr_json_settings *settings,
diff --git a/lib/abrt.c b/lib/abrt.c
index cdee0c6..e59c30d 100644
--- a/lib/abrt.c
+++ b/lib/abrt.c
@@ -373,26 +373,10 @@ sr_abrt_report_from_dir(const char *directory,
             return NULL;
         }
 
-        struct sr_json_settings settings;
-        memset(&settings, 0, sizeof(struct sr_json_settings));
-        struct sr_location location;
-        sr_location_init(&location);
-        struct sr_json_value *json_root = sr_json_parse_ex(&settings,
-                                                           core_backtrace_contents,
-                                                           &location);
+        report->core_stacktrace = sr_core_stacktrace_from_json_text(
+                core_backtrace_contents, error_message);
 
         free(core_backtrace_contents);
-        if (!json_root)
-        {
-            *error_message = sr_location_to_string(&location);
-            sr_report_free(report);
-            return NULL;
-        }
-
-        report->core_stacktrace = sr_core_stacktrace_from_json(json_root,
-                                                               error_message);
-
-        sr_json_value_free(json_root);
         if (!report->core_stacktrace)
         {
             sr_report_free(report);
diff --git a/lib/core_stacktrace.c b/lib/core_stacktrace.c
index 0e1bca1..f7fd369 100644
--- a/lib/core_stacktrace.c
+++ b/lib/core_stacktrace.c
@@ -213,22 +213,15 @@ struct sr_core_stacktrace *
 sr_core_stacktrace_from_json_text(const char *text,
                                   char **error_message)
 {
-    struct sr_json_settings settings;
-    memset(&settings, 0, sizeof(struct sr_json_settings));
-    struct sr_location location;
-    sr_location_init(&location);
-    struct sr_json_value *json_root = sr_json_parse_ex(&settings,
-                                                        text,
-                                                        &location);
-
+    struct sr_json_value *json_root = sr_json_parse(text, error_message);
     if (!json_root)
-    {
-        *error_message = sr_location_to_string(&location);
         return NULL;
-    }
 
-    return sr_core_stacktrace_from_json(json_root,
-                                        error_message);
+    struct sr_core_stacktrace *stacktrace =
+        sr_core_stacktrace_from_json(json_root, error_message);
+
+    sr_json_value_free(json_root);
+    return stacktrace;
 }
 
 char *
diff --git a/lib/generic_stacktrace.c b/lib/generic_stacktrace.c
index 70d876f..feb1dbc 100644
--- a/lib/generic_stacktrace.c
+++ b/lib/generic_stacktrace.c
@@ -108,19 +108,16 @@ sr_stacktrace_from_json(enum sr_report_type type, struct sr_json_value *root, ch
 struct sr_stacktrace *
 sr_stacktrace_from_json_text(enum sr_report_type type, const char *input, char **error_message)
 {
-    struct sr_json_settings settings;
-    memset(&settings, 0, sizeof(struct sr_json_settings));
-    struct sr_location location;
-    sr_location_init(&location);
-    struct sr_json_value *json_root = sr_json_parse_ex(&settings, input, &location);
+    struct sr_json_value *json_root = sr_json_parse(input, error_message);
 
     if (!json_root)
-    {
-        *error_message = sr_location_to_string(&location);
         return NULL;
-    }
 
-    return sr_stacktrace_from_json(type, json_root, error_message);
+    struct sr_stacktrace *stacktrace =
+        sr_stacktrace_from_json(type, json_root, error_message);
+
+    sr_json_value_free(json_root);
+    return stacktrace;
 }
 
 char *
diff --git a/lib/json.c b/lib/json.c
index aa60c0a..35b7e6b 100644
--- a/lib/json.c
+++ b/lib/json.c
@@ -636,13 +636,19 @@ sr_json_parse_ex(struct sr_json_settings *settings,
 }
 
 struct sr_json_value *
-sr_json_parse(const char *json)
+sr_json_parse(const char *json, char **error_message)
 {
     struct sr_json_settings settings;
     memset(&settings, 0, sizeof(struct sr_json_settings));
     struct sr_location location;
     sr_location_init(&location);
-    return sr_json_parse_ex(&settings, json, &location);
+    struct sr_json_value *json_root = sr_json_parse_ex(&settings, json,
+                                                       &location);
+
+    if (!json_root)
+        *error_message = sr_location_to_string(&location);
+
+    return json_root;
 }
 
 void
-- 
1.8.3.1



More information about the Crash-catcher mailing list