Skip to content

Commit

Permalink
json: Always use the json_string() method to access the strings.
Browse files Browse the repository at this point in the history
We'll be changing the way strings are stored, so the direct access
will not be safe anymore.  Change all the users to use the proper
API as they should have been doing anyway.

Signed-off-by: Ilya Maximets <[email protected]>
  • Loading branch information
igsilya committed Dec 16, 2024
1 parent a48b327 commit 1bce015
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 49 deletions.
8 changes: 5 additions & 3 deletions lib/db-ctl-base.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,15 +247,17 @@ record_id_equals(const union ovsdb_atom *name, enum ovsdb_atomic_type type,
const char *record_id)
{
if (type == OVSDB_TYPE_STRING) {
if (!strcmp(name->s->string, record_id)) {
const char *name_str = json_string(name->s);

if (!strcmp(name_str, record_id)) {
return true;
}

struct uuid uuid;
size_t len = strlen(record_id);
if (len >= 4
&& uuid_from_string(&uuid, name->s->string)
&& !strncmp(name->s->string, record_id, len)) {
&& uuid_from_string(&uuid, name_str)
&& !strncmp(name_str, record_id, len)) {
return true;
}

Expand Down
12 changes: 6 additions & 6 deletions lib/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ json_deep_clone(const struct json *json)
return json_deep_clone_array(&json->array);

case JSON_STRING:
return json_string_create(json->string);
return json_string_create(json_string(json));

case JSON_SERIALIZED_OBJECT:
return json_serialized_object_create(json);
Expand Down Expand Up @@ -578,7 +578,7 @@ json_hash(const struct json *json, size_t basis)

case JSON_STRING:
case JSON_SERIALIZED_OBJECT:
return hash_string(json->string, basis);
return hash_string(json_string(json), basis);

case JSON_NULL:
case JSON_FALSE:
Expand Down Expand Up @@ -654,7 +654,7 @@ json_equal(const struct json *a, const struct json *b)

case JSON_STRING:
case JSON_SERIALIZED_OBJECT:
return !strcmp(a->string, b->string);
return !strcmp(json_string(a), json_string(b));

case JSON_NULL:
case JSON_FALSE:
Expand Down Expand Up @@ -1053,7 +1053,7 @@ struct json *
json_from_serialized_object(const struct json *json)
{
ovs_assert(json->type == JSON_SERIALIZED_OBJECT);
return json_from_string(json->string);
return json_from_string(json_string(json));
}

/* Reads the file named 'file_name', parses its contents as a JSON object or
Expand Down Expand Up @@ -1645,11 +1645,11 @@ json_serialize(const struct json *json, struct json_serializer *s)
break;

case JSON_STRING:
json_serialize_string(json->string, ds);
json_serialize_string(json_string(json), ds);
break;

case JSON_SERIALIZED_OBJECT:
ds_put_cstr(ds, json->string);
ds_put_cstr(ds, json_string(json));
break;

case JSON_N_TYPES:
Expand Down
4 changes: 2 additions & 2 deletions lib/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ jsonrpc_msg_from_json(struct json *json, struct jsonrpc_msg **msgp)
}

msg = xzalloc(sizeof *msg);
msg->method = method ? xstrdup(method->string) : NULL;
msg->method = method ? xstrdup(json_string(method)) : NULL;
msg->params = null_from_json_null(shash_find_and_delete(object, "params"));
msg->result = null_from_json_null(shash_find_and_delete(object, "result"));
msg->error = null_from_json_null(shash_find_and_delete(object, "error"));
Expand Down Expand Up @@ -1196,7 +1196,7 @@ jsonrpc_session_recv(struct jsonrpc_session *s)
jsonrpc_session_send(s, reply);
} else if (msg->type == JSONRPC_REPLY
&& msg->id && msg->id->type == JSON_STRING
&& !strcmp(msg->id->string, "echo")) {
&& !strcmp(json_string(msg->id), "echo")) {
/* It's a reply to our echo request. Suppress it. */
} else {
return msg;
Expand Down
2 changes: 1 addition & 1 deletion lib/ovsdb-cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ server_column_get_string(const struct server_row *row,
{
ovs_assert(server_columns[index].type.key.type == OVSDB_TYPE_STRING);
const struct ovsdb_datum *d = &row->data[index];
return d->n == 1 ? d->keys[0].s->string : default_value;
return d->n == 1 ? json_string(d->keys[0].s) : default_value;
}

static bool
Expand Down
4 changes: 2 additions & 2 deletions lib/ovsdb-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ unwrap_json(const struct json *json, const char *name,
if (json->type != JSON_ARRAY
|| json->array.n != 2
|| json->array.elems[0]->type != JSON_STRING
|| (name && strcmp(json->array.elems[0]->string, name))
|| (name && strcmp(json_string(json->array.elems[0]), name))
|| json->array.elems[1]->type != value_type)
{
*value = NULL;
Expand Down Expand Up @@ -1279,7 +1279,7 @@ ovsdb_datum_from_json__(struct ovsdb_datum *datum,
|| (json->type == JSON_ARRAY
&& json->array.n > 0
&& json->array.elems[0]->type == JSON_STRING
&& !strcmp(json->array.elems[0]->string, "set"))) {
&& !strcmp(json_string(json->array.elems[0]), "set"))) {
bool is_map = ovsdb_type_is_map(type);
const char *class = is_map ? "map" : "set";
const struct json *inner;
Expand Down
16 changes: 9 additions & 7 deletions lib/ovsdb-idl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2866,8 +2866,8 @@ substitute_uuids(struct json *json, const struct ovsdb_idl_txn *txn)
if (json->array.n == 2
&& json->array.elems[0]->type == JSON_STRING
&& json->array.elems[1]->type == JSON_STRING
&& !strcmp(json->array.elems[0]->string, "uuid")
&& uuid_from_string(&uuid, json->array.elems[1]->string)) {
&& !strcmp(json_string(json->array.elems[0]), "uuid")
&& uuid_from_string(&uuid, json_string(json->array.elems[1]))) {
const struct ovsdb_idl_row *row;

row = ovsdb_idl_txn_get_row(txn, &uuid);
Expand Down Expand Up @@ -4040,18 +4040,20 @@ ovsdb_idl_txn_process_reply(struct ovsdb_idl *idl,
error = shash_find_data(json_object(op), "error");
if (error) {
if (error->type == JSON_STRING) {
if (!strcmp(error->string, "timed out")) {
const char *error_string = json_string(error);

if (!strcmp(error_string, "timed out")) {
soft_errors++;
} else if (!strcmp(error->string,
} else if (!strcmp(error_string,
"unknown database")) {
ovsdb_cs_flag_inconsistency(idl->cs);
soft_errors++;
} else if (!strcmp(error->string, "not owner")) {
} else if (!strcmp(error_string, "not owner")) {
lock_errors++;
} else if (!strcmp(error->string, "not allowed")) {
} else if (!strcmp(error_string, "not allowed")) {
hard_errors++;
ovsdb_idl_txn_set_error_json(txn, op);
} else if (strcmp(error->string, "aborted")) {
} else if (strcmp(error_string, "aborted")) {
hard_errors++;
ovsdb_idl_txn_set_error_json(txn, op);
VLOG_WARN_RL(&other_rl,
Expand Down
2 changes: 1 addition & 1 deletion lib/ovsdb-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ovsdb_parser_member(struct ovsdb_parser *parser, const char *name,
if (((int) value->type >= 0 && value->type < JSON_N_TYPES
&& types & (1u << value->type))
|| (types & OP_ID && value->type == JSON_STRING
&& ovsdb_parser_is_id(value->string)))
&& ovsdb_parser_is_id(json_string(value))))
{
sset_add(&parser->used, name);
return value;
Expand Down
4 changes: 2 additions & 2 deletions lib/ovsdb-types.c
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ ovsdb_base_type_from_json(struct ovsdb_base_type *base,
if (refTable) {
const struct json *refType;

base->uuid.refTableName = xstrdup(refTable->string);
base->uuid.refTableName = xstrdup(json_string(refTable));

/* We can't set base->uuid.refTable here because we don't have
* enough context (we might not even be running in ovsdb-server).
Expand Down Expand Up @@ -718,7 +718,7 @@ ovsdb_type_from_json(struct ovsdb_type *type, const struct json *json)
}

if (max && max->type == JSON_STRING
&& !strcmp(max->string, "unlimited")) {
&& !strcmp(json_string(max), "unlimited")) {
type->n_max = UINT_MAX;
} else {
error = n_from_json(max, &type->n_max);
Expand Down
2 changes: 1 addition & 1 deletion ovsdb/column.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ ovsdb_column_set_from_json(const struct json *json,
goto error;
}

s = json->array.elems[i]->string;
s = json_string(json->array.elems[i]);
column = shash_find_data(&schema->columns, s);
if (!column) {
error = ovsdb_syntax_error(json, NULL, "%s is not a valid "
Expand Down
2 changes: 1 addition & 1 deletion ovsdb/execution.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ ovsdb_execute_compose(struct ovsdb *db, const struct ovsdb_session *session,
if (params->type != JSON_ARRAY
|| !params->array.n
|| params->array.elems[0]->type != JSON_STRING
|| strcmp(params->array.elems[0]->string, db->schema->name)) {
|| strcmp(json_string(params->array.elems[0]), db->schema->name)) {
if (params->type != JSON_ARRAY) {
error = ovsdb_syntax_error(params, NULL, "array expected");
} else {
Expand Down
6 changes: 3 additions & 3 deletions ovsdb/jsonrpc-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ ovsdb_jsonrpc_lookup_db(const struct ovsdb_jsonrpc_session *s,
goto error;
}

db_name = params->elems[0]->string;
db_name = json_string(params->elems[0]);
db = shash_find_data(&s->up.server->dbs, db_name);
if (!db) {
error = ovsdb_syntax_error(
Expand Down Expand Up @@ -1448,7 +1448,7 @@ ovsdb_jsonrpc_parse_monitor_request(
"array of column names expected");
}

s = columns->array.elems[i]->string;
s = json_string(columns->array.elems[i]);
column = shash_find_data(&table->schema->columns, s);
if (!column) {
return ovsdb_syntax_error(columns, NULL, "%s is not a valid "
Expand Down Expand Up @@ -1591,7 +1591,7 @@ ovsdb_jsonrpc_monitor_create(struct ovsdb_jsonrpc_session *s, struct ovsdb *db,
goto error;
}
struct uuid txn_uuid;
if (!uuid_from_string(&txn_uuid, last_id->string)) {
if (!uuid_from_string(&txn_uuid, json_string(last_id))) {
error = ovsdb_syntax_error(last_id, NULL,
"last-txn-id must be UUID format.");
goto error;
Expand Down
2 changes: 1 addition & 1 deletion ovsdb/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ ovsdb_log_read(struct ovsdb_log *file, struct json **jsonp)
"offset %lld are not valid JSON (%s)",
file->display_name, data_length,
(long long int) data_offset,
json->string);
json_string(json));
goto error;
}
if (json->type != JSON_OBJECT) {
Expand Down
4 changes: 2 additions & 2 deletions ovsdb/ovsdb-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ parse_json(const char *s)
{
struct json *json = json_from_string(s);
if (json->type == JSON_STRING) {
ovs_fatal(0, "\"%s\": %s", s, json->string);
ovs_fatal(0, "\"%s\": %s", s, json_string(json));
}
return json;
}
Expand Down Expand Up @@ -596,7 +596,7 @@ fetch_dbs(struct jsonrpc *rpc, struct svec *dbs)
if (name->type != JSON_STRING) {
ovs_fatal(0, "list_dbs response %"PRIuSIZE" is not string", i);
}
svec_add(dbs, name->string);
svec_add(dbs, json_string(name));
}
jsonrpc_msg_destroy(reply);
svec_sort(dbs);
Expand Down
12 changes: 10 additions & 2 deletions ovsdb/ovsdb-idlc.in
Original file line number Diff line number Diff line change
Expand Up @@ -601,13 +601,17 @@ static void
elif (type.n_min == 1 and type.n_max == 1) or type.is_optional_pointer():
print("")
print(" if (datum->n >= 1) {")
if not type.key.ref_table:
if type.key.type == ovs.db.types.StringType:
print(" %s = CONST_CAST(char *, json_string(datum->keys[0].s));" % keyVar)
elif not type.key.ref_table:
print(" %s = datum->keys[0].%s;" % (keyVar, type.key.type.to_rvalue_string()))
else:
print(" %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_%s, &datum->keys[0].uuid));" % (keyVar, prefix, type.key.ref_table.name.lower(), prefix, type.key.ref_table.name.lower()))

if valueVar:
if not type.value.ref_table:
if type.value.type == ovs.db.types.StringType:
print(" %s = CONST_CAST(char *, json_string(datum->values[0].s));" % valueVar)
elif not type.value.ref_table:
print(" %s = datum->values[0].%s;" % (valueVar, type.value.type.to_rvalue_string()))
else:
print(" %s = %s%s_cast(ovsdb_idl_get_row_arc(row_, &%stable_%s, &datum->values[0].uuid));" % (valueVar, prefix, type.value.ref_table.name.lower(), prefix, type.value.ref_table.name.lower()))
Expand Down Expand Up @@ -635,6 +639,8 @@ static void
}\
""" % (prefix, type.key.ref_table.name.lower(), prefix, type.key.ref_table.name.lower(), prefix, type.key.ref_table.name.lower()))
keySrc = "keyRow"
elif type.key.type == ovs.db.types.StringType:
keySrc = "CONST_CAST(char *, json_string(datum->keys[i].s))"
else:
keySrc = "datum->keys[i].%s" % type.key.type.to_rvalue_string()
if type.value and type.value.ref_table:
Expand All @@ -645,6 +651,8 @@ static void
}\
""" % (prefix, type.value.ref_table.name.lower(), prefix, type.value.ref_table.name.lower(), prefix, type.value.ref_table.name.lower()))
valueSrc = "valueRow"
elif valueVar and type.value.type == ovs.db.types.StringType:
valueSrc = "CONST_CAST(char *, json_string(datum->values[i].s))"
elif valueVar:
valueSrc = "datum->values[i].%s" % type.value.type.to_rvalue_string()
print(" if (!row->n_%s) {" % (columnName))
Expand Down
2 changes: 1 addition & 1 deletion ovsdb/ovsdb-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ parse_json(const char *s)
{
struct json *json = json_from_string(s);
if (json->type == JSON_STRING) {
ovs_fatal(0, "\"%s\": %s", s, json->string);
ovs_fatal(0, "\"%s\": %s", s, json_string(json));
}
return json;
}
Expand Down
8 changes: 4 additions & 4 deletions ovsdb/ovsdb-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ ovsdb_util_read_map_string_column(const struct ovsdb_row *row,

for (i = 0; i < datum->n; i++) {
atom_key = &datum->keys[i];
if (!strcmp(atom_key->s->string, key)) {
if (!strcmp(json_string(atom_key->s), key)) {
atom_value = &datum->values[i];
break;
}
}

return atom_value ? atom_value->s->string : NULL;
return atom_value ? json_string(atom_value->s) : NULL;
}

/* Read string-uuid key-values from a map. Returns the row associated with
Expand All @@ -143,7 +143,7 @@ ovsdb_util_read_map_string_uuid_column(const struct ovsdb_row *row,
const struct ovsdb_datum *datum = &row->fields[column->index];
for (size_t i = 0; i < datum->n; i++) {
union ovsdb_atom *atom_key = &datum->keys[i];
if (!strcmp(atom_key->s->string, key)) {
if (!strcmp(json_string(atom_key->s), key)) {
const union ovsdb_atom *atom_value = &datum->values[i];
return ovsdb_table_get_row(ref_table, &atom_value->uuid);
}
Expand Down Expand Up @@ -181,7 +181,7 @@ ovsdb_util_read_string_column(const struct ovsdb_row *row,
const union ovsdb_atom *atom;

atom = ovsdb_util_read_column(row, column_name, OVSDB_TYPE_STRING);
*stringp = atom ? atom->s->string : NULL;
*stringp = atom ? json_string(atom->s) : NULL;
return atom != NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion ovsdb/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ replication_run_db(struct replication_db *rdb)
if (msg->params->type == JSON_ARRAY
&& msg->params->array.n == 2
&& msg->params->array.elems[0]->type == JSON_STRING) {
char *db_name = msg->params->array.elems[0]->string;
const char *db_name = json_string(msg->params->array.elems[0]);

if (!strcmp(db_name, rdb->db->name)) {
struct ovsdb_error *error;
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ json_to_python(struct json *json)
return PyLong_FromLong((long) json->integer);

case JSON_STRING:
return PyUnicode_FromString(json->string);
return PyUnicode_FromString(json_string(json));
default:
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion python/ovs/db/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def to_string(self):

def to_rvalue_string(self):
if self == StringType:
return 's->' + self.name
raise error.Error("can't convert string type to rvalue")
return self.name

def to_lvalue_string(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/test-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ test_json_equal(const struct json *a, const struct json *b,

case JSON_STRING:
case JSON_SERIALIZED_OBJECT:
ovs_assert(a->string != b->string);
ovs_assert(!strcmp(a->string, b->string));
ovs_assert(json_string(a) != json_string(b));
ovs_assert(!strcmp(json_string(a), json_string(b)));
return;

case JSON_NULL:
Expand Down Expand Up @@ -154,7 +154,7 @@ print_test_and_free_json(struct json *json)
{
bool ok;
if (json->type == JSON_STRING) {
printf("error: %s\n", json->string);
printf("error: %s\n", json_string(json));
ok = false;
} else {
char *s = json_to_string(json, JSSF_SORT | (pretty ? JSSF_PRETTY : 0));
Expand Down
2 changes: 1 addition & 1 deletion tests/test-jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ parse_json(const char *s)
{
struct json *json = json_from_string(s);
if (json->type == JSON_STRING) {
ovs_fatal(0, "\"%s\": %s", s, json->string);
ovs_fatal(0, "\"%s\": %s", s, json_string(json));
}
return json;
}
Expand Down
Loading

0 comments on commit 1bce015

Please sign in to comment.