public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Markus Elfring <Markus.Elfring@web.de>
To: chrome-platform@lists.linux.dev, kernel-janitors@vger.kernel.org,
	"Benson Leung" <bleung@chromium.org>,
	"Günter Röck" <groeck@chromium.org>,
	"Tzung-Bi Shih" <tzungbi@kernel.org>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH] platform/chrome: cros_ec_i2c: Less function calls in cros_ec_cmd_xfer_i2c() after error detection
Date: Thu, 28 Dec 2023 08:56:38 +0100	[thread overview]
Message-ID: <5db36d2b-afe0-4027-b22e-ded163a409be@web.de> (raw)

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Thu, 28 Dec 2023 08:36:49 +0100

The kfree() function was called in up to two cases by
the cros_ec_cmd_xfer_i2c() function during error handling
even if the passed variable contained a null pointer.
This issue was detected by using the Coccinelle software.

* Adjust jump targets.

* Delete two initialisations which became unnecessary
  with this refactoring.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/platform/chrome/cros_ec_i2c.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_i2c.c b/drivers/platform/chrome/cros_ec_i2c.c
index e29c51cbfd71..2a6ec623e352 100644
--- a/drivers/platform/chrome/cros_ec_i2c.c
+++ b/drivers/platform/chrome/cros_ec_i2c.c
@@ -193,8 +193,7 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
 	int i;
 	int len;
 	int packet_len;
-	u8 *out_buf = NULL;
-	u8 *in_buf = NULL;
+	u8 *in_buf, *out_buf;
 	u8 sum;
 	struct i2c_msg i2c_msg[2];

@@ -210,7 +209,8 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
 	packet_len = msg->insize + 3;
 	in_buf = kzalloc(packet_len, GFP_KERNEL);
 	if (!in_buf)
-		goto done;
+		goto check_command;
+
 	i2c_msg[1].len = packet_len;
 	i2c_msg[1].buf = (char *)in_buf;

@@ -221,7 +221,8 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
 	packet_len = msg->outsize + 4;
 	out_buf = kzalloc(packet_len, GFP_KERNEL);
 	if (!out_buf)
-		goto done;
+		goto free_in_buf;
+
 	i2c_msg[0].len = packet_len;
 	i2c_msg[0].buf = (char *)out_buf;

@@ -278,8 +279,10 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,

 	ret = len;
 done:
-	kfree(in_buf);
 	kfree(out_buf);
+free_in_buf:
+	kfree(in_buf);
+check_command:
 	if (msg->command == EC_CMD_REBOOT_EC)
 		msleep(EC_REBOOT_DELAY_MS);

--
2.43.0


             reply	other threads:[~2023-12-28  7:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-28  7:56 Markus Elfring [this message]
2024-01-03  3:34 ` [PATCH] platform/chrome: cros_ec_i2c: Less function calls in cros_ec_cmd_xfer_i2c() after error detection Tzung-Bi Shih
2024-01-03  7:56   ` Markus Elfring

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5db36d2b-afe0-4027-b22e-ded163a409be@web.de \
    --to=markus.elfring@web.de \
    --cc=bleung@chromium.org \
    --cc=chrome-platform@lists.linux.dev \
    --cc=groeck@chromium.org \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tzungbi@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox