From patchwork Sat May 2 17:28:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Tu X-Patchwork-Id: 1281718 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.137; helo=fraxinus.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=CK/eEy1N; dkim-atps=neutral Received: from fraxinus.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49Dx1q1R5Hz9sSW for ; Sun, 3 May 2020 03:29:19 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id EEC7F871CF; Sat, 2 May 2020 17:29:15 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id dpnQP4znQsV0; Sat, 2 May 2020 17:29:14 +0000 (UTC) Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by fraxinus.osuosl.org (Postfix) with ESMTP id D0FBF8716A; Sat, 2 May 2020 17:29:13 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id A4D70C088D; Sat, 2 May 2020 17:29:13 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by lists.linuxfoundation.org (Postfix) with ESMTP id 1DCD9C016F for ; Sat, 2 May 2020 17:29:12 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 0C7EF881BE for ; Sat, 2 May 2020 17:29:12 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id D288aMAJ6cSV for ; Sat, 2 May 2020 17:29:11 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by whitealder.osuosl.org (Postfix) with ESMTPS id 8CB018816C for ; Sat, 2 May 2020 17:29:11 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id 18so3199644pfv.8 for ; Sat, 02 May 2020 10:29:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=EzZmw8sBaYAIns60nND+diIg1nyWN9I1og2rtTJsf5E=; b=CK/eEy1N4BIFDh6vyHH929il81tEvBgbqF2FBeKZ8rcz/4xKB0h4XW7TGAKjtJnC/5 h0xjQl82bkjSijNENU6XQD4n0BZYrDA/OHnEtG8xm7G6T5h1IB3GB8n6O9xdNvuWKbHI 9GkPLfO7q1mxHN1gWe8J9ynL7J9LmER9F04LKTSBUtzA+xCe8agi1q4I73+RmqyO+vR9 x9FXnbQKPzRiLfeaOQ8z4GAfNtfcjVa5WdgFoQTLDNC3Z40rnIRE0kYeygv/aiq5Uf/W gYTRZEBx7xNyFpJ0Jjm0CktevAWnc7FqSgMS9CIrS5hwyAZXUZwKdClNzpXYUz5pzgb3 CF8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=EzZmw8sBaYAIns60nND+diIg1nyWN9I1og2rtTJsf5E=; b=IK3avwQBbl9hzwbNLGM8Crdl1yZ4xFKyspnyVIsCOvvjP2P3MaKFtE4ORXFk9wgQGD RBvUkg7zEL5Wah1bbLPuhvsPvk2j2v5lJGh7f70ZhIEei1VQTH7sfTHIOYW95KPef/N3 Avg/N9AOJ6rc/Xdss1NSwv6w0i+0XkFv3UEHLIE0SEGvyNtftSrArGED1SUyKRli4aJs PBgWm1ZrPUIBZBxTnQXdIZpnixH0d8T9DEaHNUi6c6aVTvePd7OaW0S4hVWMTmwiN8hv /ktFwu3GhqBLrt5pmg4ormwOBz22YddA5WV8YNeepwUqGRQQPIxqXScaJPYwy69yZOJT J+mw== X-Gm-Message-State: AGi0PubHluno2yMeu/UWSPoR/+P2tksb+PVAq13g3KVzSIcWolpzeIML Eo9nrOK0+raVlLIcimDF7cAVw69j X-Google-Smtp-Source: APiQypKTmpwokVT4Y7NvQPbIr+Zrqety74u8Xp5KhUnuip9+R3HA33fmXMwJvzm/7y2wF7JNq18SPg== X-Received: by 2002:aa7:8489:: with SMTP id u9mr9937684pfn.248.1588440550829; Sat, 02 May 2020 10:29:10 -0700 (PDT) Received: from sc9-mailhost3.vmware.com (c-76-21-95-192.hsd1.ca.comcast.net. [76.21.95.192]) by smtp.gmail.com with ESMTPSA id gn20sm2637784pjb.24.2020.05.02.10.29.10 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 02 May 2020 10:29:10 -0700 (PDT) From: William Tu To: dev@openvswitch.org Date: Sat, 2 May 2020 10:28:43 -0700 Message-Id: <1588440525-71022-2-git-send-email-u9012063@gmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1588440525-71022-1-git-send-email-u9012063@gmail.com> References: <1588440525-71022-1-git-send-email-u9012063@gmail.com> Subject: [ovs-dev] [PATCH 1/3] ovsdb-idlc: Fix memory leak reported by Coverity. 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: , MIME-Version: 1.0 Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" 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 *, // CID 1420891 (#1 of 1): Resource leak (RESOURCE_LEAK) Fix it by moving the xmalloc to the true branch. Signed-off-by: William Tu Reviewed-by: Yifeng Sun --- ovsdb/ovsdb-idlc.in | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ovsdb/ovsdb-idlc.in b/ovsdb/ovsdb-idlc.in index c285ee4b3c10..1d385e15c1e5 100755 --- a/ovsdb/ovsdb-idlc.in +++ b/ovsdb/ovsdb-idlc.in @@ -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))