diff mbox series

[net-next,v3,2/3] hexdump: Use for_each macro in print_hex_dump

Message ID 20250219211102.225324-3-nnac123@linux.ibm.com (mailing list archive)
State New
Delegated to: Netdev Maintainers
Headers show
Series Use new for_each macro to create hexdumps | expand

Checks

Context Check Description
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers fail 1 maintainers not CCed: akpm@linux-foundation.org
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 22 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 3 this patch: 3
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2025-02-20--12-00 (tests: 893)

Commit Message

Nick Child Feb. 19, 2025, 9:11 p.m. UTC
The looping logic in print_hex_dump can be handled by the macro
for_each_line_in_hex_dump.

Signed-off-by: Nick Child <nnac123@linux.ibm.com>
---
 lib/hexdump.c | 11 +++--------
 1 file changed, 3 insertions(+), 8 deletions(-)

Comments

Nick Child Feb. 20, 2025, 3:49 p.m. UTC | #1
Hi Dave,

Thanks for reviewing.

On 2/19/25 3:54 PM, Dave Marquardt wrote:
> Nick Child <nnac123@linux.ibm.com> writes:
>
>> diff --git a/lib/hexdump.c b/lib/hexdump.c
>> index c3db7c3a7643..181b82dfe40d 100644
>> --- a/lib/hexdump.c
>> +++ b/lib/hexdump.c
>> @@ -263,19 +263,14 @@ void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
>>   		    const void *buf, size_t len, bool ascii)
>>   {
>> -	for (i = 0; i < len; i += rowsize) {
>> -		linelen = min(remaining, rowsize);
>> -		remaining -= rowsize;
>> -
>> -		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
>> -				   linebuf, sizeof(linebuf), ascii);
>> -
>> +	for_each_line_in_hex_dump(i, rowsize, linebuf, sizeof(linebuf),
>> +				  groupsize, buf, len) {
> Several callers of print_hex_dump pass true for the ascii parameter,
> which gets passed along to hex_dump_to_buffer. But you ignore it in
> for_each_line_in_hex_dump and always use false:
>
> + #define for_each_line_in_hex_dump(i, rowsize, linebuf, linebuflen, groupsize, \
> +				   buf, len) \
> +	for ((i) = 0;							\
> +	     (i) < (len) &&						\
> +	     hex_dump_to_buffer((unsigned char *)(buf) + (i),		\
> +				(len) - (i), (rowsize), (groupsize),	\
> +				(linebuf), (linebuflen), false);	\
> +	     (i) += (rowsize) == 32 ? 32 : 16				\
> +	    )
>
> Is this behavior change intended?
>
> -Dave

Yes, for simplicity, I wanted to limit the number of parameters that the 
macro had.

Since the function does not do any printing, the user can do ascii 
conversion on their own

or even easier just ensure a \NULL term and print the string with the %s 
format specifier.


Also, allowing the user to specify the ascii argument makes it more 
difficult for them to

calculate the correct linebuflen.

For example, rowlen == 16, and groupsize == 1 with ascii = true would 
require a linebuflen

of 16 * 4 + 1 (2 chars, 1 space/NULL and 1 ascii per byte  + a extra 
space separating hexdump and ascii).

If ascii == false, linebuflen is very logically 16*3.


- Nick
David Laight Feb. 20, 2025, 9:41 p.m. UTC | #2
On Thu, 20 Feb 2025 09:49:04 -0600
Nick Child <nnac123@linux.ibm.com> wrote:

> Hi Dave,
> 
> Thanks for reviewing.
> 
> On 2/19/25 3:54 PM, Dave Marquardt wrote:
> > Nick Child <nnac123@linux.ibm.com> writes:
> >  
> >> diff --git a/lib/hexdump.c b/lib/hexdump.c
> >> index c3db7c3a7643..181b82dfe40d 100644
> >> --- a/lib/hexdump.c
> >> +++ b/lib/hexdump.c
> >> @@ -263,19 +263,14 @@ void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
> >>   		    const void *buf, size_t len, bool ascii)
> >>   {
> >> -	for (i = 0; i < len; i += rowsize) {
> >> -		linelen = min(remaining, rowsize);
> >> -		remaining -= rowsize;
> >> -
> >> -		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
> >> -				   linebuf, sizeof(linebuf), ascii);
> >> -
> >> +	for_each_line_in_hex_dump(i, rowsize, linebuf, sizeof(linebuf),
> >> +				  groupsize, buf, len) {  
> > Several callers of print_hex_dump pass true for the ascii parameter,
> > which gets passed along to hex_dump_to_buffer. But you ignore it in
> > for_each_line_in_hex_dump and always use false:
> >
> > + #define for_each_line_in_hex_dump(i, rowsize, linebuf, linebuflen, groupsize, \
> > +				   buf, len) \
> > +	for ((i) = 0;							\
> > +	     (i) < (len) &&						\
> > +	     hex_dump_to_buffer((unsigned char *)(buf) + (i),		\
> > +				(len) - (i), (rowsize), (groupsize),	\
> > +				(linebuf), (linebuflen), false);	\
> > +	     (i) += (rowsize) == 32 ? 32 : 16				\
> > +	    )
> >
> > Is this behavior change intended?
> >
> > -Dave  
> 
> Yes, for simplicity, I wanted to limit the number of parameters that the 
> macro had.
> 
> Since the function does not do any printing, the user can do ascii 
> conversion on their own
> 
> or even easier just ensure a \NULL term and print the string with the %s 
> format specifier.

That just isn't the same.
The hexdump code 'sanitises' the string for you.

> 
> 
> Also, allowing the user to specify the ascii argument makes it more 
> difficult for them to calculate the correct linebuflen.
> 
> For example, rowlen == 16, and groupsize == 1 with ascii = true would 
> require a linebuflen
> 
> of 16 * 4 + 1 (2 chars, 1 space/NULL and 1 ascii per byte  + a extra 
> space separating hexdump and ascii).
> 
> If ascii == false, linebuflen is very logically 16*3.

But the buffer only need to be 'big enough', a few spare bytes don't
matter.

You can't change the behaviour like that.

	David

> 
> 
> - Nick
> 
>
Nick Child Feb. 20, 2025, 9:56 p.m. UTC | #3
On Thu, Feb 20, 2025 at 09:41:18PM +0000, David Laight wrote:
> On Thu, 20 Feb 2025 09:49:04 -0600
> Nick Child <nnac123@linux.ibm.com> wrote:
> > On 2/19/25 3:54 PM, Dave Marquardt wrote:
> > > Nick Child <nnac123@linux.ibm.com> writes:
> > >  
> > >> diff --git a/lib/hexdump.c b/lib/hexdump.c
> > >> index c3db7c3a7643..181b82dfe40d 100644
> > >> --- a/lib/hexdump.c
> > >> +++ b/lib/hexdump.c
> > >> @@ -263,19 +263,14 @@ void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
> > >>   		    const void *buf, size_t len, bool ascii)
> > >>   {
> > >> -	for (i = 0; i < len; i += rowsize) {
> > >> -		linelen = min(remaining, rowsize);
> > >> -		remaining -= rowsize;
> > >> -
> > >> -		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
> > >> -				   linebuf, sizeof(linebuf), ascii);
> > >> -
> > >> +	for_each_line_in_hex_dump(i, rowsize, linebuf, sizeof(linebuf),
> > >> +				  groupsize, buf, len) {  
> > > Several callers of print_hex_dump pass true for the ascii parameter,
> > > which gets passed along to hex_dump_to_buffer. But you ignore it in
> > > for_each_line_in_hex_dump and always use false:
> > >
> > > + #define for_each_line_in_hex_dump(i, rowsize, linebuf, linebuflen, groupsize, \
> > > +				   buf, len) \
> > > +	for ((i) = 0;							\
> > > +	     (i) < (len) &&						\
> > > +	     hex_dump_to_buffer((unsigned char *)(buf) + (i),		\
> > > +				(len) - (i), (rowsize), (groupsize),	\
> > > +				(linebuf), (linebuflen), false);	\
> > > +	     (i) += (rowsize) == 32 ? 32 : 16				\
> > > +	    )
> > >
> > > Is this behavior change intended?
> > >
> > > -Dave  
> > 
> > Yes, for simplicity, I wanted to limit the number of parameters that the 
> > macro had.
> > 
> > Since the function does not do any printing, the user can do ascii 
> > conversion on their own
> > 
> > or even easier just ensure a \NULL term and print the string with the %s 
> > format specifier.
> 
> That just isn't the same.
> The hexdump code 'sanitises' the string for you.
> 

I see now, I forgot about print_hex_dump's use of
for_each_line_in_hexdump in my response the morning. Yes the
ascii argument needs to be respected and this patch forces
false, this is an oversight on my end, thanks for pointing it out.

I will address this issue in the next version by passing the ascii
argument into the for_each loop. Will wait a few weeks before sending
this time.

- Nick
diff mbox series

Patch

diff --git a/lib/hexdump.c b/lib/hexdump.c
index c3db7c3a7643..181b82dfe40d 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -263,19 +263,14 @@  void print_hex_dump(const char *level, const char *prefix_str, int prefix_type,
 		    const void *buf, size_t len, bool ascii)
 {
 	const u8 *ptr = buf;
-	int i, linelen, remaining = len;
+	int i;
 	unsigned char linebuf[32 * 3 + 2 + 32 + 1];
 
 	if (rowsize != 16 && rowsize != 32)
 		rowsize = 16;
 
-	for (i = 0; i < len; i += rowsize) {
-		linelen = min(remaining, rowsize);
-		remaining -= rowsize;
-
-		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
-				   linebuf, sizeof(linebuf), ascii);
-
+	for_each_line_in_hex_dump(i, rowsize, linebuf, sizeof(linebuf),
+				  groupsize, buf, len) {
 		switch (prefix_type) {
 		case DUMP_PREFIX_ADDRESS:
 			printk("%s%s%p: %s\n",