mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: Alex Dewar <alex.dewar90@gmail.com>
Cc: "Rafael J. Wysocki" <rafael@kernel.org>,
	Christian Brauner <christian.brauner@ubuntu.com>,
	"David S. Miller" <davem@davemloft.net>,
	Nayna Jain <nayna@linux.ibm.com>,
	Dan Williams <dan.j.williams@intel.com>,
	Mauro Carvalho Chehab <mchehab+huawei@kernel.org>,
	Sourabh Jain <sourabhjain@linux.ibm.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH RFC 2/2] sysfs: add helper macro for showing simple integer values
Date: Sun, 30 Aug 2020 11:13:53 +0200	[thread overview]
Message-ID: <20200830091353.GA119062@kroah.com> (raw)
In-Reply-To: <20200829233720.42640-3-alex.dewar90@gmail.com>

On Sun, Aug 30, 2020 at 12:37:17AM +0100, Alex Dewar wrote:
> sysfs attributes are supposed to be only single values, which are
> printed into a buffer of PAGE_SIZE. Accordingly, for many simple
> attributes, sprintf() can be used like so:
> 	static ssize_t my_show(..., char *buf)
> 	{
> 		...
> 		return sprintf("%d\n", my_integer);
> 	}
> 
> The problem is that whilst this use of sprintf() is memory safe, other
> cases where e.g. a possibly unterminated string is passed as input, are
> not and so use of sprintf() here might make it more difficult to
> identify these problematic cases.
> 
> Define a macro, sysfs_sprinti(), which outputs the value of a single
> integer to a buffer (with terminating "\n\0") and returns the size written.
> This way, we can convert over the some of the trivially correct users of
> sprintf() and decrease its usage in the kernel source tree.
> 
> Another advantage of this approach is that we can now statically check
> the type of the integer so that e.g. an unsigned long long will be
> formatted as %llu. This will fix cases where the wrong format string has
> been passed to sprintf().
> 
> Signed-off-by: Alex Dewar <alex.dewar90@gmail.com>
> ---
>  include/linux/sysfs.h | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)

Did you try this out?  Don't you need to return the number of bytes
written?

I like Joe's patches better, this feels like more work...

thanks,

greg k-h

  reply	other threads:[~2020-08-30  9:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-29 23:37 [PATCH RFC 0/2] simple sysfs wrappers for single values Alex Dewar
2020-08-29 23:37 ` [PATCH RFC 1/2] sysfs: add helpers for safely showing simple strings Alex Dewar
2020-08-29 23:37 ` [PATCH RFC 2/2] sysfs: add helper macro for showing simple integer values Alex Dewar
2020-08-30  9:13   ` Greg Kroah-Hartman [this message]
2020-09-02 15:31     ` Alex Dewar
2020-08-30  0:28 ` [PATCH RFC 0/2] simple sysfs wrappers for single values Joe Perches
2020-08-30  1:23   ` Joe Perches
2020-09-02 15:29     ` Alex Dewar

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=20200830091353.GA119062@kroah.com \
    --to=gregkh@linuxfoundation.org \
    --cc=alex.dewar90@gmail.com \
    --cc=christian.brauner@ubuntu.com \
    --cc=dan.j.williams@intel.com \
    --cc=davem@davemloft.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mchehab+huawei@kernel.org \
    --cc=nayna@linux.ibm.com \
    --cc=rafael@kernel.org \
    --cc=sourabhjain@linux.ibm.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

Powered by JetHome