From 35c2ce9849765d933aa1a825800c5ed5954d3736 Mon Sep 17 00:00:00 2001
From: Jesse Gross <jesse@nicira.com>
Date: Thu, 24 Jun 2010 15:31:18 -0700
Subject: [PATCH] ovsdb-idl: Check if row->written is valid.

Commit cde3f1 "ovsdb-idl: Drop unnecessary allocation from
ovsdb_idl_txn_insert()." does lazy allocation of row->written
on the assumption that ovsdb_idl_txn_write() will handle it.
However, this isn't the case for empty rows created by something
like "ovs-vsctl init" so add a check before reading the bitfield.
---
 lib/ovsdb-idl.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/lib/ovsdb-idl.c b/lib/ovsdb-idl.c
index aa2f2ce71..62a33609e 100644
--- a/lib/ovsdb-idl.c
+++ b/lib/ovsdb-idl.c
@@ -670,8 +670,10 @@ ovsdb_idl_row_clear_new(struct ovsdb_idl_row *row)
             const struct ovsdb_idl_table_class *class = row->table->class;
             size_t i;
 
-            BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
-                ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
+            if (row->written) {
+                BITMAP_FOR_EACH_1 (i, class->n_columns, row->written) {
+                    ovsdb_datum_destroy(&row->new[i], &class->columns[i].type);
+                }
             }
             free(row->new);
             free(row->written);
@@ -1233,18 +1235,22 @@ ovsdb_idl_txn_commit(struct ovsdb_idl_txn *txn)
             row_json = json_object_create();
             json_object_put(op, "row", row_json);
 
-            BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
-                const struct ovsdb_idl_column *column = &class->columns[idx];
-
-                if (row->old
-                    ? !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
-                                          &column->type)
-                    : !ovsdb_datum_is_default(&row->new[idx], &column->type)) {
-                    json_object_put(row_json, column->name,
-                                    substitute_uuids(
-                                        ovsdb_datum_to_json(&row->new[idx],
-                                                            &column->type),
-                                        txn));
+            if (row->written) {
+                BITMAP_FOR_EACH_1 (idx, class->n_columns, row->written) {
+                    const struct ovsdb_idl_column *column =
+                                                        &class->columns[idx];
+
+                    if (row->old
+                        ? !ovsdb_datum_equals(&row->old[idx], &row->new[idx],
+                                              &column->type)
+                        : !ovsdb_datum_is_default(&row->new[idx],
+                                                  &column->type)) {
+                        json_object_put(row_json, column->name,
+                                        substitute_uuids(
+                                            ovsdb_datum_to_json(&row->new[idx],
+                                                                &column->type),
+                                            txn));
+                    }
                 }
             }
 
-- 
2.47.0