diff mbox series

[ovs-dev,branch-2.11,2/2] ovsdb-tool: fix memory leak while converting cluster into standalone database

Message ID 20200324193354.56514-2-amginwal@gmail.com
State Deferred
Headers show
Series [ovs-dev,branch-2.11,1/2] ovsdb-tool: Convert clustered db to standalone db. | expand

Commit Message

aginwala aginwala March 24, 2020, 7:33 p.m. UTC
From: Damijan Skvarc <damjan.skvarc@gmail.com>

memory leak is reported by valgrind while executing functional test
"ovsdb-tool convert-to-standalone"

==13842== 2,850 (280 direct, 2,570 indirect) bytes in 7 blocks are definitely lost in loss record 20 of 20
==13842==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==13842==    by 0x45EE2E: xmalloc (util.c:138)
==13842==    by 0x43E386: json_create (json.c:1451)
==13842==    by 0x43BDD2: json_object_create (json.c:254)
==13842==    by 0x43DEE3: json_parser_push_object (json.c:1273)
==13842==    by 0x43E167: json_parser_input (json.c:1371)
==13842==    by 0x43D6EA: json_lex_input (json.c:991)
==13842==    by 0x43DAC1: json_parser_feed (json.c:1149)
==13842==    by 0x40D108: parse_body (log.c:411)
==13842==    by 0x40D386: ovsdb_log_read (log.c:476)
==13842==    by 0x406A0B: do_convert_to_standalone (ovsdb-tool.c:1571)
==13842==    by 0x406A0B: do_cluster_standalone (ovsdb-tool.c:1606)
==13842==    by 0x438670: ovs_cmdl_run_command__ (command-line.c:223)
==13842==    by 0x438720: ovs_cmdl_run_command (command-line.c:254)
==13842==    by 0x405A4C: main (ovsdb-tool.c:79)

The problem was in do_convert_to_standalone() function which while reading log file
allocate json object which was not deallocated at the end.

Signed-off-by: Damijan Skvarc <damjan.skvarc@gmail.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
Signed-off-by: Aliasgar Ginwala <aginwala@ebay.com>
---
 ovsdb/ovsdb-tool.c | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)

Comments

0-day Robot March 24, 2020, 8:39 p.m. UTC | #1
Bleep bloop.  Greetings aginwala aginwala, I am a robot and I have tried out your patch.
Thanks for your contribution.

I encountered some error that I wasn't expecting.  See the details below.


checkpatch:
WARNING: Unexpected sign-offs from developers who are not authors or co-authors or committers: Ben Pfaff <blp@ovn.org>, Aliasgar Ginwala <aginwala@ebay.com>
Lines checked: 112, Warnings: 1, Errors: 0


Please check this out.  If you feel there has been an error, please email aconole@redhat.com

Thanks,
0-day Robot
diff mbox series

Patch

diff --git a/ovsdb/ovsdb-tool.c b/ovsdb/ovsdb-tool.c
index ba78760ef..91662cab8 100644
--- a/ovsdb/ovsdb-tool.c
+++ b/ovsdb/ovsdb-tool.c
@@ -953,26 +953,30 @@  raft_header_to_standalone_log(const struct raft_header *h,
 {
     if (h->snap_index) {
         if (!h->snap.data || json_array(h->snap.data)->n != 2) {
-        ovs_fatal(0, "Incorrect raft header data array length");
+            ovs_fatal(0, "Incorrect raft header data array length");
         }
 
-        struct json *schema_json = json_array(h->snap.data)->elems[0];
+        struct json_array *pa = json_array(h->snap.data);
+        struct json *schema_json = pa->elems[0];
+        struct ovsdb_error *error = NULL;
+
         if (schema_json->type != JSON_NULL) {
             struct ovsdb_schema *schema;
             check_ovsdb_error(ovsdb_schema_from_json(schema_json, &schema));
             ovsdb_schema_destroy(schema);
-            check_ovsdb_error(ovsdb_log_write_and_free(db_log_data,
-                                                       schema_json));
+            error = ovsdb_log_write(db_log_data, schema_json);
         }
 
-        struct json *data_json = json_array(h->snap.data)->elems[1];
-        if (!data_json || data_json->type != JSON_OBJECT) {
-            ovs_fatal(0, "Invalid raft header data");
-        }
-        if (data_json->type != JSON_NULL) {
-            check_ovsdb_error(ovsdb_log_write_and_free(db_log_data,
-                                                       data_json));
+        if (!error) {
+            struct json *data_json = pa->elems[1];
+            if (!data_json || data_json->type != JSON_OBJECT) {
+                ovs_fatal(0, "Invalid raft header data");
+            }
+            if (data_json->type != JSON_NULL) {
+                error = ovsdb_log_write(db_log_data, data_json);
+            }
         }
+        check_ovsdb_error(error);
     }
 }
 
@@ -984,14 +988,14 @@  raft_record_to_standalone_log(const struct raft_record *r,
         if (!r->entry.data) {
             return;
         }
-        if (json_array(r->entry.data)->n != 2) {
+        struct json_array *pa = json_array(r->entry.data);
+
+        if (pa->n != 2) {
             ovs_fatal(0, "Incorrect raft record array length");
         }
-
-        struct json *data_json = json_array(r->entry.data)->elems[1];
+        struct json *data_json = pa->elems[1];
         if (data_json->type != JSON_NULL) {
-            check_ovsdb_error(ovsdb_log_write_and_free(db_log_data,
-                                                       data_json));
+            check_ovsdb_error(ovsdb_log_write(db_log_data, data_json));
         }
     }
 }
@@ -1586,6 +1590,7 @@  do_convert_to_standalone(struct ovsdb_log *log, struct ovsdb_log *db_log_data)
             raft_record_to_standalone_log(&r, db_log_data);
             raft_record_uninit(&r);
         }
+        json_destroy(json);
     }
 }