Re: [PATCH next 1/1] lib: Optimise hex_dump_to_buffer()

David Laight
2025-02-19T19:29:48Z

On Tue, 18 Feb 2025 16:52:38 -0600
Nick Child <nnac123@linux.ibm.com> wrote:

> On Sun, Feb 16, 2025 at 08:19:01PM +0000, David Laight wrote:
> > Fastpath the normal case of single byte output that fits in the buffer.
> > Output byte groups (byteswapped on little-endian) without calling snprintf().
> > Remove the restriction that rowsize must be 16 or 32.

That lets dump_hex_to_buffer(buf, len, len, 1, output, size, true) be used
to format hex+ascii without a gap between the hex and ascii.

> > Remove the restriction that groupsize must be 8 or less.
> > If groupsize isn't a power of 2 or doesn't divide into both len and
> > rowsize it is set to 1 (otherwise byteswapping is hard).
> > Change the types of the rowsize and groupsize parameters to be unsigned types.
> >
>
> Thank you!
>
....
> > + out_len += pad_len + len;
> > + if (dst + pad_len >= dst_end)
> > + pad_len = dst_end - dst - 1;
>
> Why not jump to hex_truncate here? This feels like an error case and
> if I am understanding correctly, this will pad the rest of the buffer
> leaving no room for ascii.

I've had a look at the code again.
The truncating error path would be: dst[0] = 0; return out_len;
But that would be confusing because it would be a truncated output that
doesn't fill the buffer.
This is different from snprintf(), so I think it is best to pad to the
end of the buffer.

David