Linux USB
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Lee Jones' <lee@kernel.org>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"Linus Walleij" <linus.walleij@linaro.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Yuan-Hsin Chen <yhchen@faraday-tech.com>,
	Feng-Hsin Chiang <john453@faraday-tech.com>,
	Po-Yu Chuang <ratbert.chuang@gmail.com>,
	"linux-usb@vger.kernel.org" <linux-usb@vger.kernel.org>
Subject: RE: [PATCH 3/5] usb: fotg210-hcd: Replace snprintf() with the safer scnprintf() variant
Date: Thu, 30 Nov 2023 14:40:12 +0000	[thread overview]
Message-ID: <f79f570f12cf424b992a7bc87664588a@AcuMS.aculab.com> (raw)
In-Reply-To: <20231130105459.3208986-4-lee@kernel.org>

From: Lee Jones
> Sent: 30 November 2023 10:55
> 
> There is a general misunderstanding amongst engineers that {v}snprintf()
> returns the length of the data *actually* encoded into the destination
> array.  However, as per the C99 standard {v}snprintf() really returns
> the length of the data that *would have been* written if there were
> enough space for it.  This misunderstanding has led to buffer-overruns
> in the past.  It's generally considered safer to use the {v}scnprintf()
> variants in their place (or even sprintf() in simple cases).  So let's
> do that.
> 
> The uses in this file both seem to assume that data *has been* written!
...
> -		temp = snprintf(next, size,
> -				"\n\t%p%c%s len=%d %08x urb %p",
> -				td, mark, ({ char *tmp;
...
> -		if (size < temp)
> -			temp = size;

That is actually a bug - even though it is trying to be correct.
The trailing '\0' that snprintf() adds (unless you are using the
M$ one) will end up in the buffer.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


  reply	other threads:[~2023-11-30 14:41 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-30 10:54 [PATCH 0/5] usb: Replace {v}snprintf() variants with safer alternatives Lee Jones
2023-11-30 10:54 ` [PATCH 1/5] usb: atm: Remove snprintf() from sysfs call-backs and replace with sysfs_emit() Lee Jones
2023-11-30 10:54 ` [PATCH 2/5] usb: cdnsp: Replace snprintf() with the safer scnprintf() variant Lee Jones
2023-11-30 10:54 ` [PATCH 3/5] usb: fotg210-hcd: " Lee Jones
2023-11-30 14:40   ` David Laight [this message]
2023-11-30 14:48   ` Linus Walleij
2023-11-30 10:54 ` [PATCH 4/5] usb: gadget: Remove snprintf() from sysfs call-backs and replace with sysfs_emit() Lee Jones
2023-11-30 10:54 ` [PATCH 5/5] usb: gadget: f_tcm: " Lee Jones

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=f79f570f12cf424b992a7bc87664588a@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=john453@faraday-tech.com \
    --cc=lee@kernel.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=ratbert.chuang@gmail.com \
    --cc=yhchen@faraday-tech.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