]> git.proxmox.com Git - ovs.git/commitdiff
ovsdb-idlc: Fix memory leak reported by Coverity.
authorWilliam Tu <u9012063@gmail.com>
Sat, 2 May 2020 16:01:48 +0000 (09:01 -0700)
committerWilliam Tu <u9012063@gmail.com>
Tue, 12 May 2020 15:34:52 +0000 (08:34 -0700)
Coverity shows the following memory leak in this code pattern:

void
ovsrec_ipfix_index_set_obs_domain_id(...
{
    struct ovsdb_datum datum;
//     1. alloc_fn: Storage is returned from allocation function xmalloc.
//     2. var_assign: Assigning: key = storage returned from xmalloc(16UL).
    union ovsdb_atom *key = xmalloc(sizeof(union ovsdb_atom));

//     3. Condition n_obs_domain_id, taking false branch.
    if (n_obs_domain_id) {
        datum.n = 1;
        datum.keys = key;
        key->integer = *obs_domain_id;
    } else {
        datum.n = 0;
        datum.keys = NULL;
    }
    datum.values = NULL;
    ovsdb_idl_index_write(CONST_CAST(struct ovsdb_idl_row *, &row->head...
//     CID 1420891 (#1 of 1): Resource leak (RESOURCE_LEAK)

Fixed it by moving the xmalloc to the true branch.

Reviewed-by: Yifeng Sun <pkusunyifeng@gmail.com>
Signed-off-by: William Tu <u9012063@gmail.com>
ovsdb/ovsdb-idlc.in

index c285ee4b3c100b28ce4fa20aa9697ac5ade0d6bf..1d385e15c1e569f824f2a546658a38d29b948595 100755 (executable)
@@ -1351,9 +1351,10 @@ struct %(s)s *
                     print("    datum.values = NULL;")
                 txn_write_func = "ovsdb_idl_index_write"
             elif type.is_optional_pointer():
-                print("    union ovsdb_atom *key = xmalloc(sizeof (union ovsdb_atom));")
+                print("    union ovsdb_atom *key;")
                 print()
                 print("    if (%s) {" % keyVar)
+                print("        key = xmalloc(sizeof (union ovsdb_atom));")
                 print("        datum.n = 1;")
                 print("        datum.keys = key;")
                 print("        " + type.key.assign_c_value_casting_away_const("key->%s" % type.key.type.to_string(), keyVar))
@@ -1364,9 +1365,10 @@ struct %(s)s *
                 print("    datum.values = NULL;")
                 txn_write_func = "ovsdb_idl_index_write"
             elif type.n_max == 1:
-                print("    union ovsdb_atom *key = xmalloc(sizeof(union ovsdb_atom));")
+                print("    union ovsdb_atom *key;")
                 print()
                 print("    if (%s) {" % nVar)
+                print("        key = xmalloc(sizeof(union ovsdb_atom));")
                 print("        datum.n = 1;")
                 print("        datum.keys = key;")
                 print("        " + type.key.assign_c_value_casting_away_const("key->%s" % type.key.type.to_string(), "*" + keyVar))