From: Kunwu Chan <chentao@kylinos.cn>
To: jesse.brandeburg@intel.com, anthony.l.nguyen@intel.com,
davem@davemloft.net, edumazet@google.com, kuba@kernel.org,
pabeni@redhat.com, jeffrey.t.kirsher@intel.com,
shannon.nelson@amd.com
Cc: kunwu.chan@hotmail.com, intel-wired-lan@lists.osuosl.org,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
Kunwu Chan <chentao@kylinos.cn>, Simon Horman <horms@kernel.org>,
Alexander Lobakin <aleksander.lobakin@intel.com>
Subject: [PATCH v3 iwl-next] i40e: Use correct buffer size in i40e_dbg_command_read
Date: Mon, 4 Dec 2023 09:44:55 +0800 [thread overview]
Message-ID: <20231204014455.2444734-1-chentao@kylinos.cn> (raw)
The size of "i40e_dbg_command_buf" is 256, the size of "name"
depends on "IFNAMSIZ", plus a null character and format size,
the total size is more than 256.
Improve readability and maintainability by replacing a hardcoded string
allocation and formatting by the use of the kasprintf() helper.
Fixes: 02e9c290814c ("i40e: debugfs interface")
Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
Suggested-by: Simon Horman <horms@kernel.org>
Suggested-by: Alexander Lobakin <aleksander.lobakin@intel.com>
---
v2
- Update the size calculation with IFNAMSIZ and sizeof(i40e_dbg_command_buf)
v3
- Use kasprintf to improve readability and maintainability
---
drivers/net/ethernet/intel/i40e/i40e_debugfs.c | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
diff --git a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
index 88240571721a..a176de89de9c 100644
--- a/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
+++ b/drivers/net/ethernet/intel/i40e/i40e_debugfs.c
@@ -72,23 +72,22 @@ static ssize_t i40e_dbg_command_read(struct file *filp, char __user *buffer,
{
struct i40e_pf *pf = filp->private_data;
int bytes_not_copied;
- int buf_size = 256;
- char *buf;
+ char *buf = NULL;
int len;
/* don't allow partial reads */
if (*ppos != 0)
return 0;
- if (count < buf_size)
- return -ENOSPC;
- buf = kzalloc(buf_size, GFP_KERNEL);
+ buf = kasprintf(GFP_KERNEL, "%s: %s\n",
+ pf->vsi[pf->lan_vsi]->netdev->name,
+ i40e_dbg_command_buf);
if (!buf)
return -ENOSPC;
- len = snprintf(buf, buf_size, "%s: %s\n",
- pf->vsi[pf->lan_vsi]->netdev->name,
- i40e_dbg_command_buf);
+ len = strlen(buf);
+ if (count < len)
+ return -ENOSPC;
bytes_not_copied = copy_to_user(buffer, buf, len);
kfree(buf);
--
2.34.1
next reply other threads:[~2023-12-04 1:45 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-04 1:44 Kunwu Chan [this message]
2023-12-04 12:51 ` Alexander Lobakin
2023-12-05 3:05 ` Kunwu Chan
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=20231204014455.2444734-1-chentao@kylinos.cn \
--to=chentao@kylinos.cn \
--cc=aleksander.lobakin@intel.com \
--cc=anthony.l.nguyen@intel.com \
--cc=davem@davemloft.net \
--cc=edumazet@google.com \
--cc=horms@kernel.org \
--cc=intel-wired-lan@lists.osuosl.org \
--cc=jeffrey.t.kirsher@intel.com \
--cc=jesse.brandeburg@intel.com \
--cc=kuba@kernel.org \
--cc=kunwu.chan@hotmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=shannon.nelson@amd.com \
/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
all inboxes | Powered by JetHome®