mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Thorsten Blum <thorsten.blum@linux.dev>
To: Doug Anderson <dianders@chromium.org>
Cc: Jason Wessel <jason.wessel@windriver.com>,
	Daniel Thompson <danielt@kernel.org>,
	Nir Lichtman <nir@lichtman.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Yuran Pereira <yuran.pereira@hotmail.com>,
	linux-hardening@vger.kernel.org,
	Daniel Thompson <daniel@riscstar.com>,
	kgdb-bugreport@lists.sourceforge.net,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] kdb: Replace deprecated strcpy() with strscpy() and memcpy()
Date: Fri, 15 Aug 2025 12:48:02 +0200	[thread overview]
Message-ID: <0BB3AC5A-5B9B-4149-90CA-80171B8B0A48@linux.dev> (raw)
In-Reply-To: <CAD=FV=Udf3pZjhpPxEuHsFynP7GoHnZ7RG=NYZ2gLzm=E_4V4A@mail.gmail.com>

Hi Doug,

On 15. Aug 2025, at 04:05, Doug Anderson wrote:
> Let's think about some test cases...
> 
> Old code:
> mp->usage = kdb_strdup(argv[2], GFP_KDB);
> if (mp->usage[0] == '"') {
>  strcpy(mp->usage, argv[2]+1);
>  mp->usage[strlen(mp->usage)-1] = '\0';
> }
> 
> New code:
> mp->usage = kdb_strdup(argv[2], GFP_KDB);
> if (mp->usage[0] == '"')
>  strscpy(mp->usage, argv[2] + 1, strlen(argv[2]) - 1);
> 
> Example string: argv[2] = "\"xyz\""
> 
> Old:
>  mp->usage = strdup("\"xyz\"")
>  mp->usage becomes "xyz\""
>  mp->usage becomes "xyz"
> 
> New:
>  mp->usage = strdup("\"xyz\"")
>  mp->usage becomes "xyz\""
>  mp->usage doesn't change (!)
> 
> To match old behavior, I think you'd need "strlen(argv[2]) - 2", right?

No, it should be "strlen(argv[2]) - 1" to match the old behavior.

In the new code, there are only two steps instead of three.

With your example source string "\"xyz\"" in argv[2]:

	strscpy(mp->usage, argv[2] + 1, strlen(argv[2]) - 1)

evaluates to:

	strscpy(mp->usage, "xyz\"", strlen("\"xyz\"") - 1)

strlen("\"xyz\"") is 5, so this becomes:

	strscpy(mp->usage, "xyz\"", 4)

Unlike strcpy(), strscpy() copies at most 'size - 1' characters and then
appends a NUL terminator. In the example, it copies only the first three
bytes (xyz) and then appends a NUL terminator, effectively replacing the
trailing quote. The result is "xyz", the same as before.

Thanks,
Thorsten


  reply	other threads:[~2025-08-15 10:48 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-14 22:01 Thorsten Blum
2025-08-15  2:05 ` Doug Anderson
2025-08-15 10:48   ` Thorsten Blum [this message]
2025-08-15 14:32     ` Doug Anderson
2025-08-15  8:57 ` Daniel Thompson
2025-08-15 11:28   ` Thorsten Blum
2025-08-15 11:40     ` Daniel Thompson
2025-08-15 13:16       ` Thorsten Blum
2025-08-15 14:29         ` Daniel Thompson
2025-08-15 14:40           ` Thorsten Blum

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=0BB3AC5A-5B9B-4149-90CA-80171B8B0A48@linux.dev \
    --to=thorsten.blum@linux.dev \
    --cc=daniel@riscstar.com \
    --cc=danielt@kernel.org \
    --cc=dianders@chromium.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jason.wessel@windriver.com \
    --cc=kgdb-bugreport@lists.sourceforge.net \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nir@lichtman.org \
    --cc=yuran.pereira@hotmail.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®