From: David Laight <david.laight.linux@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: linux-kernel@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>,
Arnd Bergmann <arnd@arndb.de>,
Linus Torvalds <torvalds@linux-foundation.org>,
Christophe Leroy <christophe.leroy@c-s.fr>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
nnac123@linux.ibm.com, horms@kernel.org
Subject: Re: [PATCH v2 1/1] lib: Optimise hex_dump_to_buffer()
Date: Wed, 12 Mar 2025 21:01:12 +0000 [thread overview]
Message-ID: <20250312210112.63e3e207@pumpkin> (raw)
In-Reply-To: <Z9HhLr8zD5M1tdGw@smile.fi.intel.com>
On Wed, 12 Mar 2025 21:31:58 +0200
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> On Wed, Mar 12, 2025 at 07:18:16PM +0000, David Laight wrote:
> > On Mon, 10 Mar 2025 11:05:13 +0200
> > Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > > On Sat, Mar 08, 2025 at 09:34:21AM +0000, David Laight wrote:
>
> ...
>
> > > > -extern int hex_dump_to_buffer(const void *buf, size_t len, int rowsize,
> > > > - int groupsize, char *linebuf, size_t linebuflen,
> > > > - bool ascii);
> > > > +extern size_t hex_dump_to_buffer(const void *buf, size_t len, size_t rowsize,
> > > > + size_t groupsize, char *linebuf,
> > > > + size_t linebuflen, bool ascii);
> > >
> > > int - > size_t in the returned value is incorrect change.
> > > This is explained in the comments to the test cases patch series.
> >
> > I don't see you mentioning why.
> > The return value is 'the number of bytes that would be output if the buffer
> > were large enough' - it is never negative.
>
> True...
>
> > Although given 'a large enough buffer' length is trivially calculable
> > it would have been safer to return the actual number of bytes added
> > (excluding the '\0').
>
> ...but the functions keep the snprintf() semantics, which returns an int.
> This makes it more-or-less 1:1 snprintf() substitute in cases where it can
> be done in general.
And scnprintf() has been added because the return value of snprintf()
isn't the one most code wanted.
I've looked through all the code that uses the result of hex_dump_to_buffer().
The only code that needs the 'overflow' result is the test code.
Everything else will work just the same if it returns the number of characters
added to the buffer.
The code in drivers/platform/chrome/wilco_ec/debugfs.c uses the return
value without checking - hard to say whether the buffer is big enough (or whether
the code has the required locking to allow for multiple readers.
>
> > There were no tests for 'len == 0 && linebuflen == 0', with !ascii the
> > existing hex_dump_to_buffer() even manages to return -1.
> > (and the function than generates the 'test compare data' is also broken.)
>
> Then you can start with fixes of those?
No one calls it like that.
I could split it into multiple patches, but they don't overlap and it just
makes more work for everyone.
>
> > Note that libc snprintf() has the same return type as fprintf() which can
> > be -1, but any code the looks at is probably broken!
> >
> > So an unsigned return type it better.
>
> Maybe, but this will deviate from the prototype and use cases.
The use cases all want a 'length' never an 'error'.
Having an unsigned return type makes it absolutely clear that -1 (or -errno)
won't be returned.
It isn't the sort of function where you want to have to 'go through hoops'
to write valid code.
David
prev parent reply other threads:[~2025-03-12 21:01 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-08 9:34 [PATCH v2 1/1] lib: Optimise hex_dump_to_buffer() David Laight
2025-03-10 9:05 ` Andy Shevchenko
2025-03-12 19:18 ` David Laight
2025-03-12 19:31 ` Andy Shevchenko
2025-03-12 21:01 ` David Laight [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=20250312210112.63e3e207@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=arnd@arndb.de \
--cc=christophe.leroy@c-s.fr \
--cc=horms@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=nnac123@linux.ibm.com \
--cc=torvalds@linux-foundation.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