mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-08-08 11:07:08 +00:00
lib: fixup zlog_hexdump
* Allocate correct amount of memory * Use snprintf() instead of sprintf() Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
This commit is contained in:
parent
9d16566bb3
commit
2cf63c3561
39
lib/log.c
39
lib/log.c
@ -1092,41 +1092,48 @@ void zlog_hexdump(const void *mem, unsigned int len)
|
|||||||
unsigned long i = 0;
|
unsigned long i = 0;
|
||||||
unsigned int j = 0;
|
unsigned int j = 0;
|
||||||
unsigned int columns = 8;
|
unsigned int columns = 8;
|
||||||
char buf[(len * 4) + ((len / 4) * 20) + 30];
|
/* 19 bytes for 0xADDRESS: */
|
||||||
|
/* 24 bytes for data; 2 chars plus a space per data byte */
|
||||||
|
/* 1 byte for space */
|
||||||
|
/* 8 bytes for ASCII representation */
|
||||||
|
/* 1 byte for a newline */
|
||||||
|
/* ===================== */
|
||||||
|
/* 53 bytes per 8 bytes of data */
|
||||||
|
/* 1 byte for null term */
|
||||||
|
size_t bs = ((len / 8) + 1) * 53 + 1;
|
||||||
|
char buf[bs];
|
||||||
char *s = buf;
|
char *s = buf;
|
||||||
|
|
||||||
for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0);
|
for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0);
|
||||||
i++) {
|
i++) {
|
||||||
/* print offset */
|
/* print offset */
|
||||||
if (i % columns == 0)
|
if (i % columns == 0)
|
||||||
s += sprintf(s, "0x%016lx: ", (unsigned long)mem + i);
|
s += snprintf(s, bs - (s - buf),
|
||||||
|
"0x%016lx: ", (unsigned long)mem + i);
|
||||||
|
|
||||||
/* print hex data */
|
/* print hex data */
|
||||||
if (i < len)
|
if (i < len)
|
||||||
s += sprintf(s, "%02x ", 0xFF & ((const char *)mem)[i]);
|
s += snprintf(s, bs - (s - buf), "%02x ",
|
||||||
|
0xFF & ((const char *)mem)[i]);
|
||||||
|
|
||||||
/* end of block, just aligning for ASCII dump */
|
/* end of block, just aligning for ASCII dump */
|
||||||
else
|
else
|
||||||
s += sprintf(s, " ");
|
s += snprintf(s, bs - (s - buf), " ");
|
||||||
|
|
||||||
/* print ASCII dump */
|
/* print ASCII dump */
|
||||||
if (i % columns == (columns - 1)) {
|
if (i % columns == (columns - 1)) {
|
||||||
for (j = i - (columns - 1); j <= i; j++) {
|
for (j = i - (columns - 1); j <= i; j++) {
|
||||||
if (j >= len) /* end of block, not really
|
/* end of block not really printing */
|
||||||
printing */
|
if (j >= len)
|
||||||
s += sprintf(s, " ");
|
s += snprintf(s, bs - (s - buf), " ");
|
||||||
|
else if (isprint((int)((const char *)mem)[j]))
|
||||||
else if (isprint((int)((const char *)mem)
|
s += snprintf(
|
||||||
[j])) /* printable char
|
s, bs - (s - buf), "%c",
|
||||||
*/
|
|
||||||
s += sprintf(
|
|
||||||
s, "%c",
|
|
||||||
0xFF & ((const char *)mem)[j]);
|
0xFF & ((const char *)mem)[j]);
|
||||||
|
|
||||||
else /* other char */
|
else /* other char */
|
||||||
s += sprintf(s, ".");
|
s += snprintf(s, bs - (s - buf), ".");
|
||||||
}
|
}
|
||||||
s += sprintf(s, "\n");
|
s += snprintf(s, bs - (s - buf), "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
zlog_debug("\n%s", buf);
|
zlog_debug("\n%s", buf);
|
||||||
|
Loading…
Reference in New Issue
Block a user