From patchwork Fri Dec 17 13:16:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dumitru Ceara X-Patchwork-Id: 1570017 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=iA9ewL/J; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=2605:bc80:3010::137; helo=smtp4.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Received: from smtp4.osuosl.org (smtp4.osuosl.org [IPv6:2605:bc80:3010::137]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JFqJy5hYWz9t6S for ; Sat, 18 Dec 2021 00:17:22 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id BF4BE41D61; Fri, 17 Dec 2021 13:17:19 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id I2GcOwISPouF; Fri, 17 Dec 2021 13:17:18 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp4.osuosl.org (Postfix) with ESMTPS id D52FF41642; Fri, 17 Dec 2021 13:17:17 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id B9F05C002F; Fri, 17 Dec 2021 13:17:17 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp3.osuosl.org (smtp3.osuosl.org [140.211.166.136]) by lists.linuxfoundation.org (Postfix) with ESMTP id 535C9C0012 for ; Fri, 17 Dec 2021 13:17:16 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 0D310610BC for ; Fri, 17 Dec 2021 13:17:10 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Authentication-Results: smtp3.osuosl.org (amavisd-new); dkim=pass (1024-bit key) header.d=redhat.com Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 2_ux1nPleju1 for ; Fri, 17 Dec 2021 13:17:09 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp3.osuosl.org (Postfix) with ESMTPS id 4F23E610C0 for ; Fri, 17 Dec 2021 13:17:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1639747028; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vZ4L0YHC9yUcf6ja+Qe7dYxXCk/h1UZ5PuJh2/jlHW0=; b=iA9ewL/J3QYh9OIWM/80AgQh2yUxfuwi6Pg+ftMyuppYUrZ6tHXnUzRPG2pJ6sGMAfXXQA yHPa5uYoHrXNTeNCEmogUv3gNzCy0Ht6Rub5Dp35Rtww6y8OLCn+J1Uhnu5DODcduYtpLQ XvUnRFw7akfJsW9XY16daQXIRFLrVhE= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-651-26m4iCtBO2yNFfIHrvP0Fg-1; Fri, 17 Dec 2021 08:17:05 -0500 X-MC-Unique: 26m4iCtBO2yNFfIHrvP0Fg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4E84F8DD876; Fri, 17 Dec 2021 13:17:04 +0000 (UTC) Received: from dceara.remote.csb (unknown [10.39.193.197]) by smtp.corp.redhat.com (Postfix) with ESMTP id 663CD45D8A; Fri, 17 Dec 2021 13:17:03 +0000 (UTC) From: Dumitru Ceara To: dev@openvswitch.org Date: Fri, 17 Dec 2021 14:16:59 +0100 Message-Id: <20211217131658.30994.78806.stgit@dceara.remote.csb> In-Reply-To: <20211217131623.30994.63519.stgit@dceara.remote.csb> References: <20211217131623.30994.63519.stgit@dceara.remote.csb> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=dceara@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Cc: i.maximets@ovn.org Subject: [ovs-dev] [PATCH 02/11] treewide: Don't pass NULL to library functions that expect non-NULL. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" 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 [...] Signed-off-by: Dumitru Ceara --- lib/dpif-netdev.c | 4 +++- lib/ofp-actions.c | 2 +- lib/ofpbuf.c | 4 ++++ lib/ovsdb-data.c | 4 ++++ ofproto/ofproto-dpif-rid.c | 6 ++++-- ovsdb/monitor.c | 4 ++++ 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index a790df5fd697..f62202a8b53d 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -4506,8 +4506,10 @@ dp_netdev_actions_create(const struct nlattr *actions, size_t size) struct dp_netdev_actions *netdev_actions; netdev_actions = xmalloc(sizeof *netdev_actions + size); - memcpy(netdev_actions->actions, actions, size); netdev_actions->size = size; + if (size) { + memcpy(netdev_actions->actions, actions, size); + } return netdev_actions; } diff --git a/lib/ofp-actions.c b/lib/ofp-actions.c index ecf914eac10b..f21b3c579f2b 100644 --- a/lib/ofp-actions.c +++ b/lib/ofp-actions.c @@ -9147,7 +9147,7 @@ bool ofpacts_equal(const struct ofpact *a, size_t a_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 diff --git a/lib/ofpbuf.c b/lib/ofpbuf.c index 4edb3c114ae6..271105bdea17 100644 --- a/lib/ofpbuf.c +++ b/lib/ofpbuf.c @@ -384,6 +384,10 @@ ofpbuf_put_zeros(struct ofpbuf *b, size_t size) void * ofpbuf_put(struct ofpbuf *b, const void *p, size_t size) { + if (!size) { + return ofpbuf_tail(b); + } + void *dst = ofpbuf_put_uninit(b, size); memcpy(dst, p, size); return dst; diff --git a/lib/ovsdb-data.c b/lib/ovsdb-data.c index 2832e94ea04a..6b1c20ff85a0 100644 --- a/lib/ovsdb-data.c +++ b/lib/ovsdb-data.c @@ -1967,6 +1967,10 @@ ovsdb_datum_push_unsafe(struct ovsdb_datum *dst, unsigned int start_idx, unsigned int n, const struct ovsdb_type *type) { + if (n == 0) { + return; + } + memcpy(&dst->keys[dst->n], &src->keys[start_idx], n * sizeof src->keys[0]); if (type->value.type != OVSDB_TYPE_VOID) { memcpy(&dst->values[dst->n], &src->values[start_idx], diff --git a/ofproto/ofproto-dpif-rid.c b/ofproto/ofproto-dpif-rid.c index 29aafc2c0b40..f01468025c03 100644 --- a/ofproto/ofproto-dpif-rid.c +++ b/ofproto/ofproto-dpif-rid.c @@ -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) && !memcmp(&a->metadata, &b->metadata, sizeof a->metadata) && 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->conntracked == b->conntracked && 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) && ofpacts_equal(a->action_set, a->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)); } diff --git a/ovsdb/monitor.c b/ovsdb/monitor.c index ab814cf20e26..0f222cc99284 100644 --- a/ovsdb/monitor.c +++ b/ovsdb/monitor.c @@ -405,6 +405,10 @@ ovsdb_monitor_columns_sort(struct ovsdb_monitor *dbmon) SHASH_FOR_EACH (node, &dbmon->tables) { struct ovsdb_monitor_table *mt = node->data; + if (mt->n_columns == 0) { + continue; + } + qsort(mt->columns, mt->n_columns, sizeof *mt->columns, compare_ovsdb_monitor_column); for (i = 0; i < mt->n_columns; i++) {