Linux Power Management development
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: xueqin Luo <luoxueqin@kylinos.cn>
Cc: rafael@kernel.org, pavel@ucw.cz, len.brown@intel.com,
	linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [RESEND PATCH] drivers: base: power: Optimize array out-of-bounds access logic
Date: Mon, 2 Dec 2024 07:21:45 +0100	[thread overview]
Message-ID: <2024120206-gigolo-parish-2be3@gregkh> (raw)
In-Reply-To: <20241202032306.24671-1-luoxueqin@kylinos.cn>

On Mon, Dec 02, 2024 at 11:23:06AM +0800, xueqin Luo wrote:
> The code previously used snprintf to format a string into a buffer and
> manually checked for potential buffer overflows by comparing the returned
> length with the buffer size. This approach introduced unnecessary
> complexity and was prone to subtle errors.

What errors are in the original code here?  Is it incorrect?

> Replaced snprintf with scnprintf, which directly returns the actual number
> of characters written to the buffer (excluding the null terminator). This
> change eliminates the need for manual overflow checks and simplifies the
> buffer offset and size adjustment logic.

Your lines should be wrapped at 72 columns, right?

> 
> Signed-off-by: xueqin Luo <luoxueqin@kylinos.cn>

Why is this a resend?  What was wrong with the first version?

> ---
>  drivers/base/power/trace.c | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/base/power/trace.c b/drivers/base/power/trace.c
> index cd6e559648b2..d8da7195bb00 100644
> --- a/drivers/base/power/trace.c
> +++ b/drivers/base/power/trace.c
> @@ -238,10 +238,8 @@ int show_trace_dev_match(char *buf, size_t size)
>  		unsigned int hash = hash_string(DEVSEED, dev_name(dev),
>  						DEVHASH);
>  		if (hash == value) {
> -			int len = snprintf(buf, size, "%s\n",
> +			int len = scnprintf(buf, size, "%s\n",
>  					    dev_driver_string(dev));
> -			if (len > size)
> -				len = size;

How was this tested?  I think if code could just be cleaned up
automatically like this, it would have already, right?

thanks,

greg k-h

      reply	other threads:[~2024-12-02  6:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-02  3:23 [RESEND PATCH] drivers: base: power: Optimize array out-of-bounds access logic xueqin Luo
2024-12-02  6:21 ` Greg KH [this message]

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=2024120206-gigolo-parish-2be3@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=len.brown@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=luoxueqin@kylinos.cn \
    --cc=pavel@ucw.cz \
    --cc=rafael@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