git: 38d757b6c29c - main - linuxkpi: Avoid trailing whitespaces in lkpi_hex_dump()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 04 Jan 2026 09:56:19 UTC
The branch main has been updated by dumbbell:
URL: https://cgit.FreeBSD.org/src/commit/?id=38d757b6c29c990de72dc51ac123922d3f095f53
commit 38d757b6c29c990de72dc51ac123922d3f095f53
Author: Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
AuthorDate: 2025-07-26 15:30:40 +0000
Commit: Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
CommitDate: 2026-01-04 09:44:52 +0000
linuxkpi: Avoid trailing whitespaces in lkpi_hex_dump()
We use the return value of the callback to track the number of bytes
written. We use it to determine if a group of characters should be
prepended with a whitespace. This way, we never add a trailing
whitespace.
We need to pay attention to the return value of the callback: if it is
negative, it's an error and we return immediately. Otherwise, we would
decrease the number of written bytes and possibly make it negative.
Reviewed by: bz, christos
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D51558
---
sys/compat/linuxkpi/common/src/linux_compat.c | 66 +++++++++++++++++++++------
1 file changed, 53 insertions(+), 13 deletions(-)
diff --git a/sys/compat/linuxkpi/common/src/linux_compat.c b/sys/compat/linuxkpi/common/src/linux_compat.c
index 35430daf311d..03d866260dd9 100644
--- a/sys/compat/linuxkpi/common/src/linux_compat.c
+++ b/sys/compat/linuxkpi/common/src/linux_compat.c
@@ -1900,47 +1900,87 @@ lkpi_hex_dump(int(*_fpf)(void *, const char *, ...), void *arg1,
typedef const struct { uint32_t value; } __packed *print_32p_t;
typedef const struct { uint16_t value; } __packed *print_16p_t;
const void *buf_old = buf;
- int row;
+ int row, linelen, ret;
while (len > 0) {
- if (level != NULL)
- _fpf(arg1, "%s", level);
- if (prefix_str != NULL)
- _fpf(arg1, "%s ", prefix_str);
+ linelen = 0;
+ if (level != NULL) {
+ ret = _fpf(arg1, "%s", level);
+ if (ret < 0)
+ break;
+ linelen += ret;
+ }
+ if (prefix_str != NULL) {
+ ret = _fpf(
+ arg1, "%s%s", linelen ? " " : "", prefix_str);
+ if (ret < 0)
+ break;
+ linelen += ret;
+ }
switch (prefix_type) {
case DUMP_PREFIX_ADDRESS:
- _fpf(arg1, "[%p] ", buf);
+ ret = _fpf(
+ arg1, "%s[%p]", linelen ? " " : "", buf);
+ if (ret < 0)
+ return;
+ linelen += ret;
break;
case DUMP_PREFIX_OFFSET:
- _fpf(arg1, "[%#tx] ", ((const char *)buf -
- (const char *)buf_old));
+ ret = _fpf(
+ arg1, "%s[%#tx]", linelen ? " " : "",
+ ((const char *)buf - (const char *)buf_old));
+ if (ret < 0)
+ return;
+ linelen += ret;
break;
default:
break;
}
for (row = 0; row != rowsize; row++) {
if (groupsize == 8 && len > 7) {
- _fpf(arg1, "%016llx ", ((print_64p_t)buf)->value);
+ ret = _fpf(
+ arg1, "%s%016llx", linelen ? " " : "",
+ ((print_64p_t)buf)->value);
+ if (ret < 0)
+ return;
+ linelen += ret;
buf = (const uint8_t *)buf + 8;
len -= 8;
} else if (groupsize == 4 && len > 3) {
- _fpf(arg1, "%08x ", ((print_32p_t)buf)->value);
+ ret = _fpf(
+ arg1, "%s%08x", linelen ? " " : "",
+ ((print_32p_t)buf)->value);
+ if (ret < 0)
+ return;
+ linelen += ret;
buf = (const uint8_t *)buf + 4;
len -= 4;
} else if (groupsize == 2 && len > 1) {
- _fpf(arg1, "%04x ", ((print_16p_t)buf)->value);
+ ret = _fpf(
+ arg1, "%s%04x", linelen ? " " : "",
+ ((print_16p_t)buf)->value);
+ if (ret < 0)
+ return;
+ linelen += ret;
buf = (const uint8_t *)buf + 2;
len -= 2;
} else if (len > 0) {
- _fpf(arg1, "%02x ", *(const uint8_t *)buf);
+ ret = _fpf(
+ arg1, "%s%02x", linelen ? " " : "",
+ *(const uint8_t *)buf);
+ if (ret < 0)
+ return;
+ linelen += ret;
buf = (const uint8_t *)buf + 1;
len--;
} else {
break;
}
}
- _fpf(arg1, "\n");
+ ret = _fpf(arg1, "\n");
+ if (ret < 0)
+ break;
}
}