From 6819f58d018246f298f26e1ba8594d833e798345 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 21 Jan 2025 22:16:22 -0800 Subject: [PATCH] When debugging image.c, abort if silent truncation * src/image.c (image_build_heuristic_mask, png_load_body): Abort if snprintf truncated. (If truncation is not possible here we should use sprintf instead, as that simplifies automatic runtime checking.) (cherry picked from commit 8ff7338fdd05fe6d21765711327a99c87cfd7613) --- src/image.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/image.c b/src/image.c index 2b898e12805..1db2df736a5 100644 --- a/src/image.c +++ b/src/image.c @@ -7386,8 +7386,9 @@ image_build_heuristic_mask (struct frame *f, struct image *img, { #ifndef USE_CAIRO char color_name[30]; - snprintf (color_name, sizeof color_name, "#%04x%04x%04x", - rgb[0] + 0u, rgb[1] + 0u, rgb[2] + 0u); + int len = snprintf (color_name, sizeof color_name, "#%04x%04x%04x", + rgb[0] + 0u, rgb[1] + 0u, rgb[2] + 0u); + eassert (len < sizeof color_name); bg = ( #ifdef HAVE_NTGUI 0x00ffffff & /* Filter out palette info. */ @@ -8537,8 +8538,9 @@ png_load_body (struct frame *f, struct image *img, struct png_load_context *c) img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue); #else /* USE_CAIRO */ char color_name[30]; - snprintf (color_name, sizeof color_name, "#%04x%04x%04x", - bg->red, bg->green, bg->blue); + int len = snprintf (color_name, sizeof color_name, "#%04x%04x%04x", + bg->red, bg->green, bg->blue); + eassert (len < sizeof color_name); img->background = image_alloc_image_color (f, img, build_string (color_name), 0); #endif /* USE_CAIRO */ -- 2.39.5