From 6eeba8b6b90bcb56e453582f8efefb706af1138d Mon Sep 17 00:00:00 2001 From: Benjamin Gilbert Date: Sat, 23 May 2026 16:35:37 -0500 Subject: [PATCH] premultiplied-argb: avoid undefined behavior Don't left shift into the sign bit after integer promotion. Signed-off-by: Benjamin Gilbert --- docs/premultiplied-argb/index.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/premultiplied-argb/index.md b/docs/premultiplied-argb/index.md index a0e2657..f6a5d4e 100644 --- a/docs/premultiplied-argb/index.md +++ b/docs/premultiplied-argb/index.md @@ -71,7 +71,9 @@ for (int64_t i = 0; i < w * h; i++) { uint8_t r = 255 * ((pixel >> 16) & 0xff) / a; uint8_t g = 255 * ((pixel >> 8) & 0xff) / a; uint8_t b = 255 * (pixel & 0xff) / a; - out[i] = GUINT32_TO_BE(r << 24 | g << 16 | b << 8); + // uint32_t cast avoids undefined behavior: left shift into sign bit + // after r is promoted to int. + out[i] = GUINT32_TO_BE((uint32_t) r << 24 | g << 16 | b << 8); } } ```