From 8492ba01ba3e3bdf717c9e7f40ddad412c297945 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 4 Mar 2025 10:41:34 +0100 Subject: [PATCH] qdrawhelper: (attempt to) fix Coverity warning about `op` being uninit'ed Unlike in other cases of structs, Coverity doesn't say which field, exactly, it considers uninitialized in `op` upon return from getOperator(). Manual checking confirms that all fields are written in all branches (and all configs, btw), with the following exceptions: - src* fields are not written in QSpanData::None mode, but this branch is marked as Q_UNREACHABLE() - `linear`/`radial` are only written in QSpanData::Linear- or RadialGradient mode, which means that their (unnamed) union is not written in Solid/ConicalGradient/Texture modes Assuming it's the latter branch that trips Coverity, introduce a std::monostate member, noGradient, to said union, and initialize it Solid/ConicalGradient/Texture modes. This makes the union a kind of optional, but without the churn of actually introducing a std::optional. The effect is that in those modes the active union member is now the empty monostate, so static analyzers will not complain about `linear` or `radial` being uninitialized (because they're not active members). 'noGradient' being the active union member should also help statically and dynamically (ubsan, one day?) detect accesses to `linear` or `radial` when they're not active. Before, I believe the first declared of the two was implicitly active. Amends the start of the public history. Pick-to: 6.9 6.8 6.5 Coverity-Id: 11400 Change-Id: I743363cc06af3778f85c205bfb6880c696229f92 Reviewed-by: Allan Sandfeld Jensen --- src/gui/painting/qdrawhelper.cpp | 3 +++ src/gui/painting/qdrawhelper_p.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/gui/painting/qdrawhelper.cpp b/src/gui/painting/qdrawhelper.cpp index 92d3ad8d821..6ba94cfb171 100644 --- a/src/gui/painting/qdrawhelper.cpp +++ b/src/gui/painting/qdrawhelper.cpp @@ -3793,6 +3793,7 @@ static inline Operator getOperator(const QSpanData *data, const QT_FT_Span *span switch(data->type) { case QSpanData::Solid: solidSource = data->solidColor.alphaF() >= 1.0f; + op.noGradient = {}; op.srcFetch = nullptr; op.srcFetch64 = nullptr; op.srcFetchFP = nullptr; @@ -3821,6 +3822,7 @@ static inline Operator getOperator(const QSpanData *data, const QT_FT_Span *span break; case QSpanData::ConicalGradient: solidSource = !data->gradient.alphaColor; + op.noGradient = {}; // sic! op.srcFetch = qt_fetch_conical_gradient; #if QT_CONFIG(raster_64bit) op.srcFetch64 = qt_fetch_conical_gradient_rgb64; @@ -3831,6 +3833,7 @@ static inline Operator getOperator(const QSpanData *data, const QT_FT_Span *span break; case QSpanData::Texture: solidSource = !data->texture.hasAlpha; + op.noGradient = {}; op.srcFetch = getSourceFetch(getBlendType(data), data->texture.format); #if QT_CONFIG(raster_64bit) op.srcFetch64 = getSourceFetch64(getBlendType(data), data->texture.format); diff --git a/src/gui/painting/qdrawhelper_p.h b/src/gui/painting/qdrawhelper_p.h index 7363bfff590..646a51020db 100644 --- a/src/gui/painting/qdrawhelper_p.h +++ b/src/gui/painting/qdrawhelper_p.h @@ -30,6 +30,7 @@ #include #include +#include // std::monostate QT_BEGIN_NAMESPACE @@ -210,6 +211,7 @@ struct Operator CompositionFunctionFP funcFP; union { + std::monostate noGradient; LinearGradientValues linear; RadialGradientValues radial; };