Re: [PATCH v2 04/12] sysinfo: Make sysinfo_get_str() behave like snprintf()

Marek Behún
2021-11-05T11:19:30Z

On Thu, 4 Nov 2021 20:02:25 -0600
Simon Glass <sjg@chromium.org> wrote:

> Hi Marek,
>
> On Wed, 3 Nov 2021 at 17:23, Marek Behún <kabel@kernel.org> wrote:
> >
> > From: Marek Behún <marek.behun@nic.cz>
> >
> > Currently sysinfo_get_str() returns 0 if a string is filled in the
> > given buffer, and otherwise gives no simple mechanism to determine
> > actual string length.
> >
> > One implementation returns -ENOSPC if buffer is not large enough.
> >
> > Change the behaviour of the function to that of snprintf(): i.e. the
> > buffer is always filled in as much as possible if the string exists, and
> > the function returns the actual length of the string (excluding the
> > terminating NULL-byte).
> >
> > Signed-off-by: Marek Behún <marek.behun@nic.cz>
> > ---
> > board/google/chromebook_coral/coral.c | 13 ++++---------
> > common/board_info.c | 2 +-
> > drivers/sysinfo/gpio.c | 2 +-
> > drivers/sysinfo/rcar3.c | 2 +-
> > drivers/sysinfo/sandbox.c | 5 +++--
> > include/sysinfo.h | 16 ++++++++++++----
> > lib/smbios.c | 2 +-
> > test/dm/sysinfo-gpio.c | 12 ++++++------
> > test/dm/sysinfo.c | 12 ++++++------
> > 9 files changed, 35 insertions(+), 31 deletions(-)
>
> So how do we know if the size is too small? The string is silently truncated?

The same way as in snprintf.
If the return value is >= size, then size is too small.
(The return value is the length of the whole string (excluding \0 at
end), not just the part that was copied to buffer.)

Marek