From 34f465add2c95aaba012d0f6444ad7478c420327 Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Thu, 31 Oct 2024 18:38:50 +0100 Subject: [PATCH] Windows: fix compiler warning [-Wstrict-aliasing] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In function ‘void innards(...)’: src/drivers/GDI/Fl_GDI_Graphics_Driver_image.cxx:132:23: dereferencing type-punned pointer will break strict-aliasing rules BITMAPINFO &bmi = *((BITMAPINFO*)bmibuffer); ~^~~~~~~~~~~~~~~~~~~~~~~ Found with gcc 12 (MinGW cross compiler) Also: use correct sizes (sizeof) rather than hardcoded values. --- .../GDI/Fl_GDI_Graphics_Driver_image.cxx | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/drivers/GDI/Fl_GDI_Graphics_Driver_image.cxx b/src/drivers/GDI/Fl_GDI_Graphics_Driver_image.cxx index 356400da4..3a8e70689 100644 --- a/src/drivers/GDI/Fl_GDI_Graphics_Driver_image.cxx +++ b/src/drivers/GDI/Fl_GDI_Graphics_Driver_image.cxx @@ -1,7 +1,7 @@ // // Windows image drawing code for the Fast Light Tool Kit (FLTK). // -// Copyright 1998-2020 by Bill Spitzak and others. +// Copyright 1998-2024 by Bill Spitzak and others. // // This library is free software. Distribution and use rights are outlined in // the file "COPYING" which should have been included with this file. If this @@ -128,43 +128,44 @@ static void innards(const uchar *buf, int X, int Y, int W, int H, if (w<=0 || h<=0) return; if (buf) buf += (x-X)*delta + (y-Y)*linedelta; - static U32 bmibuffer[256+12]; - BITMAPINFO &bmi = *((BITMAPINFO*)bmibuffer); - if (!bmi.bmiHeader.biSize) { - bmi.bmiHeader.biSize = sizeof(bmi)-4; // does it use this to determine type? - bmi.bmiHeader.biPlanes = 1; - bmi.bmiHeader.biCompression = BI_RGB; - bmi.bmiHeader.biXPelsPerMeter = 0; - bmi.bmiHeader.biYPelsPerMeter = 0; - bmi.bmiHeader.biClrUsed = 0; - bmi.bmiHeader.biClrImportant = 0; + // bmibuffer: BITMAPINFOHEADER + 256 colors (RGBQUAD) + 1 (rounding effects ?) + static U32 bmibuffer[sizeof(BITMAPINFOHEADER)/4 + 257]; + BITMAPINFO *bmi = (BITMAPINFO*)bmibuffer; + if (!bmi->bmiHeader.biSize) { + bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + bmi->bmiHeader.biPlanes = 1; + bmi->bmiHeader.biCompression = BI_RGB; + bmi->bmiHeader.biXPelsPerMeter = 0; + bmi->bmiHeader.biYPelsPerMeter = 0; + bmi->bmiHeader.biClrUsed = 0; + bmi->bmiHeader.biClrImportant = 0; } #if USE_COLORMAP if (indexed) { for (short i=0; i<256; i++) { - *((short*)(bmi.bmiColors)+i) = i; + *((short*)(bmi->bmiColors)+i) = i; } } else #endif if (depth<3) { - RGBQUAD *bmi_colors = &bmi.bmiColors[0]; // suppress warning (STR #3199) + RGBQUAD *bmi_colors = &(bmi->bmiColors[0]); // use pointer to suppress warning (STR #3199) for (int i=0; i<256; i++) { - bmi_colors[i].rgbBlue = (uchar)i; // bmi.bmiColors[i]... + bmi_colors[i].rgbBlue = (uchar)i; // = bmi->bmiColors[i] bmi_colors[i].rgbGreen = (uchar)i; bmi_colors[i].rgbRed = (uchar)i; bmi_colors[i].rgbReserved = (uchar)0; // must be zero } } - bmi.bmiHeader.biWidth = w; + bmi->bmiHeader.biWidth = w; #if USE_COLORMAP - bmi.bmiHeader.biBitCount = indexed ? 8 : depth*8; + bmi->bmiHeader.biBitCount = indexed ? 8 : depth*8; int pixelsize = indexed ? 1 : depth; #else - bmi.bmiHeader.biBitCount = depth*8; + bmi->bmiHeader.biBitCount = depth*8; int pixelsize = depth; #endif if (depth==2) { // special case: gray with alpha - bmi.bmiHeader.biBitCount = 32; + bmi->bmiHeader.biBitCount = 32; pixelsize = 4; } int linesize = (pixelsize*w+3)&~3; @@ -185,7 +186,7 @@ static void innards(const uchar *buf, int X, int Y, int W, int H, buffer = new U32[(size + 3) / 4]; } } - bmi.bmiHeader.biHeight = blocking; + bmi->bmiHeader.biHeight = blocking; static U32* line_buffer; if (!buf) { int size = W*delta; @@ -259,7 +260,7 @@ static void innards(const uchar *buf, int X, int Y, int W, int H, // does not do the expected job, whereas StretchDIBits does it. StretchDIBits(gc, x, y+j-k, w, k, 0, 0, w, k, (LPSTR)((uchar*)buffer+(blocking-k)*linesize), - &bmi, + bmi, #if USE_COLORMAP indexed ? DIB_PAL_COLORS : DIB_RGB_COLORS #else @@ -273,7 +274,7 @@ static void innards(const uchar *buf, int X, int Y, int W, int H, else { SetDIBitsToDevice(gc, x, y+j-k, w, k, 0, 0, 0, k, (LPSTR)((uchar*)buffer+(blocking-k)*linesize), - &bmi, + bmi, #if USE_COLORMAP indexed ? DIB_PAL_COLORS : DIB_RGB_COLORS #else