From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Kees Cook <keescook@chromium.org>,
linux-kernel@vger.kernel.org,
Andy Shevchenko <andy.shevchenko@gmail.com>,
Dmitry Torokhov <dmitry.torokhov@gmail.com>
Subject: Re: [RFC 0/7] eliminate snprintf with overlapping src and dst
Date: Wed, 09 Mar 2016 23:19:31 +0100 [thread overview]
Message-ID: <877fhb9tvw.fsf@rasmusvillemoes.dk> (raw)
In-Reply-To: <20160309124940.7d870c59a7a7117c6c6d7937@linux-foundation.org> (Andrew Morton's message of "Wed, 9 Mar 2016 12:49:40 -0800")
On Wed, Mar 09 2016, Andrew Morton <akpm@linux-foundation.org> wrote:
> On Tue, 8 Mar 2016 21:40:47 +0100 Rasmus Villemoes <linux@rasmusvillemoes.dk> wrote:
>
>> Doing snprintf(buf, len, "%s...", buf, ...) for appending to a buffer
>> currently works, but it is somewhat fragile, and any other overlap
>> between source and destination buffers would be a definite bug. This
>> is an attempt at eliminating the relatively few occurences of this
>> pattern in the kernel.
>
> I dunno,
>
> snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
>
> is pretty damn convenient. Can we instead state that "sprintf shall
> support this"? Maybe add a little __init testcase to vsprintf.c to
> check that it continues to work OK.
As Andy points out (thanks!), we actually already have an interface for
simple managing of a user-supplied buffer, seq_buf, which is at least as
convenient, and also avoids the manual bookkeeping that I changed it
into.
OK, one problem is that seq_buf_puts doesn't actually produce a
'\0'-terminated string, but since there's no in-tree users of
seq_buf_puts currently, I think we can easily fix that. Then the rule
would be that as long as one only uses the "string" functions
seq_buf_puts and seq_buf_printf one gets a '\0'-terminated string, while
any use of seq_buf_putc, seq_buf_putmem etc. will void that property.
For the joystick case, this is roughly what it would look like. I think
it's nice to avoid passing the analog->name, sizeof(analog->name) pair
every time.
diff --git a/drivers/input/joystick/analog.c b/drivers/input/joystick/analog.c
index 6f8b084e13d0..e69ff4d3e31a 100644
--- a/drivers/input/joystick/analog.c
+++ b/drivers/input/joystick/analog.c
@@ -37,6 +37,7 @@
#include <linux/jiffies.h>
#include <linux/timex.h>
#include <linux/timekeeping.h>
+#include <linux/seq_buf.h>
#define DRIVER_DESC "Analog joystick and gamepad driver"
@@ -435,23 +436,24 @@ static void analog_calibrate_timer(struct analog_port *port)
static void analog_name(struct analog *analog)
{
- snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
- hweight8(analog->mask & ANALOG_AXES_STD),
- hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
- hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
+ struct seq_buf sb;
+
+ seq_buf_init(&sb, analog->name, sizeof(analog->name));
+
+ seq_buf_printf(&sb, "Analog %d-axis %d-button",
+ hweight8(analog->mask & ANALOG_AXES_STD),
+ hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
+ hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
if (analog->mask & ANALOG_HATS_ALL)
- snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
- analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
+ seq_buf_printf(&sb, " %d-hat", hweight16(analog->mask & ANALOG_HATS_ALL));
if (analog->mask & ANALOG_HAT_FCS)
- strlcat(analog->name, " FCS", sizeof(analog->name));
+ seq_buf_puts(&sb, " FCS");
if (analog->mask & ANALOG_ANY_CHF)
- strlcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF",
- sizeof(analog->name));
+ seq_buf_puts(&sb, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
- strlcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick",
- sizeof(analog->name));
+ seq_buf_puts(&sb, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick");
}
/*
next prev parent reply other threads:[~2016-03-09 22:19 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-05 20:38 snprintf, overlapping destination and source Rasmus Villemoes
2015-12-05 20:42 ` Julia Lawall
2015-12-07 22:04 ` Kees Cook
2015-12-16 13:14 ` Rasmus Villemoes
2016-03-08 20:40 ` [RFC 0/7] eliminate snprintf with overlapping src and dst Rasmus Villemoes
2016-03-08 20:40 ` [RFC 1/7] drm/amdkfd: avoid fragile and inefficient snprintf use Rasmus Villemoes
2016-03-14 14:33 ` Oded Gabbay
2016-03-14 19:18 ` Rasmus Villemoes
2016-03-08 20:40 ` [RFC 2/7] Input: joystick - avoid fragile " Rasmus Villemoes
2016-03-09 6:49 ` Andy Shevchenko
2016-03-08 20:40 ` [RFC 3/7] leds: avoid fragile sprintf use Rasmus Villemoes
2016-03-08 20:40 ` [RFC 4/7] drivers/media/pci/zoran: avoid fragile snprintf use Rasmus Villemoes
2016-03-08 20:40 ` [RFC 5/7] wlcore: " Rasmus Villemoes
2016-03-09 11:49 ` Kalle Valo
2016-03-08 20:40 ` [RFC 6/7] [media] ati_remote: " Rasmus Villemoes
2016-03-08 20:40 ` [RFC 7/7] USB: usbatm: avoid fragile and inefficient " Rasmus Villemoes
2016-03-08 21:01 ` Joe Perches
2016-03-10 23:56 ` Greg Kroah-Hartman
2016-03-09 13:08 ` Sergei Shtylyov
2016-03-08 23:07 ` [RFC 0/7] eliminate snprintf with overlapping src and dst Kees Cook
2016-03-08 23:13 ` Kees Cook
2016-03-09 6:51 ` Andy Shevchenko
2016-03-09 20:49 ` Andrew Morton
2016-03-09 22:19 ` Rasmus Villemoes [this message]
2016-03-10 13:59 ` One Thousand Gnomes
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=877fhb9tvw.fsf@rasmusvillemoes.dk \
--to=linux@rasmusvillemoes.dk \
--cc=akpm@linux-foundation.org \
--cc=andy.shevchenko@gmail.com \
--cc=dmitry.torokhov@gmail.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.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;
as well as URLs for NNTP newsgroup(s).