linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Len Baker <len.baker@gmx.com>
To: Joe Perches <joe@perches.com>
Cc: Len Baker <len.baker@gmx.com>, Borislav Petkov <bp@alien8.de>,
	Mauro Carvalho Chehab <mchehab@kernel.org>,
	Tony Luck <tony.luck@intel.com>,
	James Morse <james.morse@arm.com>,
	Robert Richter <rric@kernel.org>,
	David Laight <David.Laight@ACULAB.COM>,
	Kees Cook <keescook@chromium.org>,
	linux-hardening@vger.kernel.org, linux-edac@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy
Date: Fri, 3 Sep 2021 16:21:46 +0200	[thread overview]
Message-ID: <20210903142146.GA1998@titan> (raw)
In-Reply-To: <7b6dc45194f28db52740c2a604550f6879dafe36.camel@perches.com>

Hi,

On Sun, Aug 29, 2021 at 09:38:37AM -0700, Joe Perches wrote:
> On Sun, 2021-08-29 at 18:15 +0200, Len Baker wrote:
> > strcpy() performs no bounds checking on the destination buffer. This
> > could result in linear overflows beyond the end of the buffer, leading
> > to all kinds of misbehaviors. The safe replacement is strscpy() [1].
> >
> > However, to simplify and clarify the code, to concatenate labels use
> > the scnprintf() function. This way it is not necessary to check the
> > return value of strscpy (-E2BIG if the parameter count is 0 or the src
> > was truncated) since the scnprintf returns always the number of chars
> > written into the buffer. This function returns always a nul-terminated
> > string even if it needs to be truncated.
> []
> > diff --git a/drivers/edac/edac_mc.c b/drivers/edac/edac_mc.c
> []
> > @@ -1113,12 +1116,11 @@ void edac_mc_handle_error(const enum hw_event_mc_err_type type,
> >  			p = e->label;
> >  			*p = '\0';
> >  		} else {
> > -			if (p != e->label) {
> > -				strcpy(p, OTHER_LABEL);
> > -				p += strlen(OTHER_LABEL);
> > -			}
> > -			strcpy(p, dimm->label);
> > -			p += strlen(p);
> > +			const char *or = (p != e->label) ? OTHER_LABEL : "";
> > +
> > +			n = scnprintf(p, len, "%s%s", or, dimm->label);
> > +			len -= n;
> > +			p += n;
>
> A very common and intelligible mechanism for this is:
>
> 	const char *prefix = "";
> 	int n = 0;
> 	...
> 			n += scnprintf(e->label + n, sizeof(e->label) - n,
> 				       "%s%s", prefix, dimm->label);
> 			prefix = " or ";
>
Ok, I will send a new version using this common mechanism. Thanks for the
advise.

Regards,
Len

      reply	other threads:[~2021-09-03 14:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-29 16:15 [PATCH v5] EDAC/mc: Prefer strscpy or scnprintf over strcpy Len Baker
2021-08-29 16:38 ` Joe Perches
2021-09-03 14:21   ` Len Baker [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=20210903142146.GA1998@titan \
    --to=len.baker@gmx.com \
    --cc=David.Laight@ACULAB.COM \
    --cc=bp@alien8.de \
    --cc=james.morse@arm.com \
    --cc=joe@perches.com \
    --cc=keescook@chromium.org \
    --cc=linux-edac@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchehab@kernel.org \
    --cc=rric@kernel.org \
    --cc=tony.luck@intel.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;
as well as URLs for NNTP newsgroup(s).