From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by dpdk.org (Postfix) with ESMTP id 5B2D68072 for ; Fri, 12 Dec 2014 13:24:08 +0100 (CET) Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga103.jf.intel.com with ESMTP; 12 Dec 2014 04:22:04 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.07,563,1413270000"; d="scan'208";a="622740206" Received: from irvmail001.ir.intel.com ([163.33.26.43]) by orsmga001.jf.intel.com with ESMTP; 12 Dec 2014 04:24:05 -0800 Received: from sivswdev01.ir.intel.com (sivswdev01.ir.intel.com [10.237.217.45]) by irvmail001.ir.intel.com (8.14.3/8.13.6/MailSET/Hub) with ESMTP id sBCCO5ex028313; Fri, 12 Dec 2014 12:24:05 GMT Received: from sivswdev01.ir.intel.com (localhost [127.0.0.1]) by sivswdev01.ir.intel.com with ESMTP id sBCCO50S022614; Fri, 12 Dec 2014 12:24:05 GMT Received: (from bricha3@localhost) by sivswdev01.ir.intel.com with id sBCCO4bU022610; Fri, 12 Dec 2014 12:24:04 GMT From: Bruce Richardson To: dev@dpdk.org, cristian.dumitrescu@intel.com Date: Fri, 12 Dec 2014 12:24:04 +0000 Message-Id: <1418387044-22569-1-git-send-email-bruce.richardson@intel.com> X-Mailer: git-send-email 1.7.4.1 Subject: [dpdk-dev] [PATCH] examples: fix unchecked malloc return value in ip_pipeline X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Dec 2014 12:24:08 -0000 Static analysis shows that once instance of rte_zmalloc is missing a return value check in the code. This is fixed by adding a return value check. The malloc call itself is moved to earlier in the function so that no work is done unless all memory allocation requests have succeeded - thereby removing the need for rollback on error. Signed-off-by: Bruce Richardson --- examples/ip_pipeline/cmdline.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/examples/ip_pipeline/cmdline.c b/examples/ip_pipeline/cmdline.c index 13d565e..152acb5 100644 --- a/examples/ip_pipeline/cmdline.c +++ b/examples/ip_pipeline/cmdline.c @@ -1093,7 +1093,7 @@ cmd_firewall_add_parsed( __attribute__((unused)) void *data) { struct cmd_firewall_add_result *params = parsed_result; - struct app_rule rule, *old_rule; + struct app_rule rule, *old_rule, *new_rule = NULL; struct rte_mbuf *msg; struct app_msg_req *req; struct app_msg_resp *resp; @@ -1148,6 +1148,18 @@ cmd_firewall_add_parsed( if (msg == NULL) rte_panic("Unable to allocate new message\n"); + /* if we need a new rule structure, allocate it before we go further */ + if (old_rule == NULL) { + new_rule = rte_zmalloc_socket("CLI", sizeof(struct app_rule), + RTE_CACHE_LINE_SIZE, rte_socket_id()); + if (new_rule == NULL) { + printf("Cannot allocate memory for new rule\n"); + rte_ctrlmbuf_free(msg); + return; + } + } + + /* Fill request message */ req = (struct app_msg_req *)rte_ctrlmbuf_data(msg); req->type = APP_MSG_REQ_FW_ADD; @@ -1190,12 +1202,6 @@ cmd_firewall_add_parsed( printf("Request FIREWALL_ADD failed (%u)\n", resp->result); else { if (old_rule == NULL) { - struct app_rule *new_rule = (struct app_rule *) - rte_zmalloc_socket("CLI", - sizeof(struct app_rule), - RTE_CACHE_LINE_SIZE, - rte_socket_id()); - memcpy(new_rule, &rule, sizeof(rule)); TAILQ_INSERT_TAIL(&firewall_table, new_rule, entries); n_firewall_rules++; -- 1.9.3