[Concept,34/35] malloc: Print mcheck registry-overflow message only once

Message ID 20251210000737.180797-35-sjg@u-boot.org
State New
Headers
Series malloc: Add heap debugging commands and mcheck caller tracking |

Commit Message

Simon Glass Dec. 10, 2025, 12:07 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Use a static bool flag to ensure the overflow warning is printed only
once, avoiding repeated messages when the registry is full.

Make sure to set the flag before calling printf(), which can itself do
allocations with sandbox / Truetype.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 common/mcheck_core.inc.h | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
  

Patch

diff --git a/common/mcheck_core.inc.h b/common/mcheck_core.inc.h
index 63dbeaa5f56..598a5d018ab 100644
--- a/common/mcheck_core.inc.h
+++ b/common/mcheck_core.inc.h
@@ -201,6 +201,7 @@  static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz,
 				     size_t alignment, int clean_content,
 				     const char *caller)
 {
+	static bool overflow_msg_shown;
 	const size_t slop = alignment ?
 		mcheck_evaluate_memalign_prefix_size(alignment) - sizeof(struct mcheck_hdr) : 0;
 	struct mcheck_hdr *hdr = (struct mcheck_hdr *)((char *)altoghether_ptr + slop);
@@ -239,10 +240,10 @@  static void *mcheck_allocated_helper(void *altoghether_ptr, size_t customer_sz,
 			return payload; // normal end
 		}
 
-	static char *overflow_msg = "\n\n\nERROR: mcheck registry overflow, pedantic check would be incomplete!!\n\n\n\n";
-
-	printf("%s", overflow_msg);
-	overflow_msg = "(mcheck registry full)";
+	if (!overflow_msg_shown) {
+		overflow_msg_shown = true;
+		printf("\n\nERROR: mcheck registry overflow, pedantic check would be incomplete!\n\n");
+	}
 	return payload;
 }