From 41952703d959f694f888ad47c731f70816038708 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Tue, 21 Apr 2026 02:27:33 +0300 Subject: [PATCH] Memory: use DEBUG for hex_dump output instead of printf --- lib/mem.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/lib/mem.c b/lib/mem.c index 878d42fa..25f2efb0 100644 --- a/lib/mem.c +++ b/lib/mem.c @@ -51,29 +51,28 @@ static void init_mutex() { static void* allocated_head = NULL; static size_t allocated_count = 0; -static void hex_dump_buf(const void* mem, size_t len, char* buf, size_t buf_size) { +static void hex_dump(const void* mem, size_t len) { const uint8_t* data = (const uint8_t*)mem; - size_t off = 0; - for (size_t i = 0; i < len && i < 256; i += 16) { - if (off >= buf_size - 80) break; - off += snprintf(buf + off, buf_size - off, "%08zx: ", i); + char line[128]; + for (size_t i = 0; i < len; i += 16) { + int off = snprintf(line, sizeof(line), "%08zx: ", i); for (size_t j = 0; j < 16; j++) { if (i + j < len) { - off += snprintf(buf + off, buf_size - off, "%02x ", data[i + j]); + off += snprintf(line + off, sizeof(line) - off, "%02x ", data[i + j]); } else { - off += snprintf(buf + off, buf_size - off, " "); + off += snprintf(line + off, sizeof(line) - off, " "); } } - off += snprintf(buf + off, buf_size - off, " "); + off += snprintf(line + off, sizeof(line) - off, " "); for (size_t j = 0; j < 16; j++) { if (i + j < len) { uint8_t c = data[i + j]; - off += snprintf(buf + off, buf_size - off, "%c", (c >= 32 && c <= 126) ? c : '.'); + off += snprintf(line + off, sizeof(line) - off, "%c", (c >= 32 && c <= 126) ? c : '.'); } else { - off += snprintf(buf + off, buf_size - off, " "); + off += snprintf(line + off, sizeof(line) - off, " "); } } - off += snprintf(buf + off, buf_size - off, "\n"); + DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s\n", line); } } @@ -117,9 +116,7 @@ void u_check(void* ptr, const char* text, const char* location) { printf("%s: %s: Allocated at: %s\n", location, text, alloc_loc); printf("%s: %s: Memory block size (user): %u bytes, total allocated: %u bytes\n", location, text, size, total); printf("%s: %s: Full memory dump at base %p:\n", location, text, base); - char dump_buf[4096]; - hex_dump_buf(base, total, dump_buf, sizeof(dump_buf)); - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s", dump_buf); + hex_dump(base, total); exit(EXIT_FAILURE); } } @@ -231,9 +228,7 @@ void u_report_unfreed_blocks(void) { u_check(user_ptr, "unfreed_check", "u_report_unfreed_blocks"); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " - Allocated at: %s, size: %u bytes\n", alloc_loc, size); DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, " Full memory dump at %p:\n", user_ptr); - char dump_buf[4096]; - hex_dump_buf(user_ptr, size, dump_buf, sizeof(dump_buf)); - DEBUG_ERROR(DEBUG_CATEGORY_MEMORY, "%s", dump_buf); + hex_dump(user_ptr, size); curr = *(void**)((uint8_t*)curr + NEXT_OFFSET); } }