From 5a8b7573352549c0ddd07d477f6f4641f1599ea2 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 22 Oct 2024 12:18:14 -0700 Subject: [PATCH] Fix UB in line_hash_code * src/dispnew.c (line_hash_code): Avoid undefined behavior on integer overflow. (cherry picked from commit dac51f1f66ef93cec6d9ea17e74a193de9ae1ff3) --- src/dispnew.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/dispnew.c b/src/dispnew.c index 9caeb9d1694..1ece9cc1d45 100644 --- a/src/dispnew.c +++ b/src/dispnew.c @@ -1175,15 +1175,12 @@ line_hash_code (struct frame *f, struct glyph_row *row) while (glyph < end) { int c = glyph->u.ch; - int face_id = glyph->face_id; - /* A given row of a frame glyph matrix could have glyphs - from more than one frame, if child frames are displayed. - Since face_id of a face depends on the frame (it's an - index into the frame's face cache), we need the hash - value to include something specific to the frame, and we - use the frame cache's address for that purpose. */ + unsigned int face_id = glyph->face_id; + /* Struct frame can move with igc, and so on. But we need + something that takes different frames into account. Use the + face_cache pointer for that which is malloc'd. */ if (glyph->frame && glyph->frame != f) - face_id += (ptrdiff_t) glyph->frame->face_cache; + face_id += (uintptr_t) glyph->frame->face_cache; if (FRAME_MUST_WRITE_SPACES (f)) c -= SPACEGLYPH; hash = (((hash << 4) + (hash >> 24)) & 0x0fffffff) + c; -- 2.39.5