2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-22 01:51:26 +00:00

treewide: Don't pass NULL to library functions that expect non-NULL.

It's actually undefined behavior to pass NULL to standard library
functions that manipulate arrays (e.g., qsort, memcpy, memcmp), even if
the passed number of items is 0.

UB Sanitizer reports:
  ovsdb/monitor.c:408:9: runtime error: null pointer passed as argument 1,
                                        which is declared to never be null
      #0 0x406ae1 in ovsdb_monitor_columns_sort ovsdb/monitor.c:408
      #1 0x406ae1 in ovsdb_monitor_add ovsdb/monitor.c:1683
  [...]
  lib/ovsdb-data.c:1970:5: runtime error: null pointer passed as argument 2,
                                          which is declared to never be null
      #0 0x4071c8 in ovsdb_datum_push_unsafe lib/ovsdb-data.c:1970
      #1 0x471cd0 in ovsdb_datum_apply_diff_in_place lib/ovsdb-data.c:2345
  [...]
  ofproto/ofproto-dpif-rid.c:159:17:
        runtime error: null pointer passed as argument 1,
                       which is declared to never be null
      #0 0x4df5d8 in frozen_state_equal ofproto/ofproto-dpif-rid.c:159
      #1 0x4dfd27 in recirc_find_equal ofproto/ofproto-dpif-rid.c:179
      [...]

Acked-by: Aaron Conole <aconole@redhat.com>
Signed-off-by: Dumitru Ceara <dceara@redhat.com>
Acked-by: Paolo Valerio <pvalerio@redhat.com>
Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
This commit is contained in:
Dumitru Ceara 2022-01-24 15:17:28 +01:00 committed by Ilya Maximets
parent 989895501c
commit 8ed26a8be3
6 changed files with 20 additions and 4 deletions

View File

@ -5099,8 +5099,10 @@ dp_netdev_actions_create(const struct nlattr *actions, size_t size)
struct dp_netdev_actions *netdev_actions; struct dp_netdev_actions *netdev_actions;
netdev_actions = xmalloc(sizeof *netdev_actions + size); netdev_actions = xmalloc(sizeof *netdev_actions + size);
memcpy(netdev_actions->actions, actions, size);
netdev_actions->size = size; netdev_actions->size = size;
if (size) {
memcpy(netdev_actions->actions, actions, size);
}
return netdev_actions; return netdev_actions;
} }

View File

@ -9157,7 +9157,7 @@ bool
ofpacts_equal(const struct ofpact *a, size_t a_len, ofpacts_equal(const struct ofpact *a, size_t a_len,
const struct ofpact *b, size_t b_len) const struct ofpact *b, size_t b_len)
{ {
return a_len == b_len && !memcmp(a, b, a_len); return a_len == b_len && (!a_len || !memcmp(a, b, a_len));
} }
/* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of /* Returns true if the 'a_len' bytes of actions in 'a' and the 'b_len' bytes of

View File

@ -384,6 +384,10 @@ ofpbuf_put_zeros(struct ofpbuf *b, size_t size)
void * void *
ofpbuf_put(struct ofpbuf *b, const void *p, size_t size) ofpbuf_put(struct ofpbuf *b, const void *p, size_t size)
{ {
if (!size) {
return ofpbuf_tail(b);
}
void *dst = ofpbuf_put_uninit(b, size); void *dst = ofpbuf_put_uninit(b, size);
memcpy(dst, p, size); memcpy(dst, p, size);
return dst; return dst;

View File

@ -1967,6 +1967,10 @@ ovsdb_datum_push_unsafe(struct ovsdb_datum *dst,
unsigned int start_idx, unsigned int n, unsigned int start_idx, unsigned int n,
const struct ovsdb_type *type) const struct ovsdb_type *type)
{ {
if (n == 0) {
return;
}
memcpy(&dst->keys[dst->n], &src->keys[start_idx], n * sizeof src->keys[0]); memcpy(&dst->keys[dst->n], &src->keys[start_idx], n * sizeof src->keys[0]);
if (type->value.type != OVSDB_TYPE_VOID) { if (type->value.type != OVSDB_TYPE_VOID) {
memcpy(&dst->values[dst->n], &src->values[start_idx], memcpy(&dst->values[dst->n], &src->values[start_idx],

View File

@ -156,7 +156,7 @@ frozen_state_equal(const struct frozen_state *a, const struct frozen_state *b)
&& uuid_equals(&a->ofproto_uuid, &b->ofproto_uuid) && uuid_equals(&a->ofproto_uuid, &b->ofproto_uuid)
&& !memcmp(&a->metadata, &b->metadata, sizeof a->metadata) && !memcmp(&a->metadata, &b->metadata, sizeof a->metadata)
&& a->stack_size == b->stack_size && a->stack_size == b->stack_size
&& !memcmp(a->stack, b->stack, a->stack_size) && (!a->stack_size || !memcmp(a->stack, b->stack, a->stack_size))
&& a->mirrors == b->mirrors && a->mirrors == b->mirrors
&& a->conntracked == b->conntracked && a->conntracked == b->conntracked
&& a->was_mpls == b->was_mpls && a->was_mpls == b->was_mpls
@ -164,7 +164,9 @@ frozen_state_equal(const struct frozen_state *a, const struct frozen_state *b)
b->ofpacts, b->ofpacts_len) b->ofpacts, b->ofpacts_len)
&& ofpacts_equal(a->action_set, a->action_set_len, && ofpacts_equal(a->action_set, a->action_set_len,
b->action_set, b->action_set_len) b->action_set, b->action_set_len)
&& !memcmp(a->userdata, b->userdata, a->userdata_len) && a->userdata_len == b->userdata_len
&& (!a->userdata_len
|| !memcmp(a->userdata, b->userdata, a->userdata_len))
&& uuid_equals(&a->xport_uuid, &b->xport_uuid)); && uuid_equals(&a->xport_uuid, &b->xport_uuid));
} }

View File

@ -405,6 +405,10 @@ ovsdb_monitor_columns_sort(struct ovsdb_monitor *dbmon)
SHASH_FOR_EACH (node, &dbmon->tables) { SHASH_FOR_EACH (node, &dbmon->tables) {
struct ovsdb_monitor_table *mt = node->data; struct ovsdb_monitor_table *mt = node->data;
if (mt->n_columns == 0) {
continue;
}
qsort(mt->columns, mt->n_columns, sizeof *mt->columns, qsort(mt->columns, mt->n_columns, sizeof *mt->columns,
compare_ovsdb_monitor_column); compare_ovsdb_monitor_column);
for (i = 0; i < mt->n_columns; i++) { for (i = 0; i < mt->n_columns; i++) {