QPen: QDebug-stream Pen{,Join}Style etc as enums

Coverity complained that PEN_STYLES[p.style()] may be an out-of-bounds
access, and it is, since setPen() doesn't sanitze its argument and we
have Qt::PenStyle::MPenStyle as an extra (hidden) enumerator that
Coverity doesn't know we never use.

Instead of fixing the issue with the array lookup, realize that all
three enums are Q_ENUM_NS in namespace Qt and use the "magic" QDebug
stream operator for Q_ENUMs to do the numeric -> string conversion,
making invalid enum values SEP.

Not picking to older branches, since it's changing the output.

[ChangeLog][QtGui][QPen] The debug stream operator now prints the
names of capStyle() and joinStyle() (was: numerical values).

Coverity-Id: 390701
Change-Id: Icc593fac3ab02c56a4b39b2a7c8a5d423d626ce3
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Øystein Heskestad <oystein.heskestad@qt.io>
This commit is contained in:
Marc Mutz 2025-03-05 13:47:24 +01:00
parent 7a32a2238f
commit dfaa43331a

View File

@ -1014,20 +1014,10 @@ QDataStream &operator>>(QDataStream &s, QPen &p)
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QPen &p)
{
const char *PEN_STYLES[] = {
"NoPen",
"SolidLine",
"DashLine",
"DotLine",
"DashDotLine",
"DashDotDotLine",
"CustomDashLine"
};
QDebugStateSaver saver(dbg);
dbg.nospace() << "QPen(" << p.width() << ',' << p.brush()
<< ',' << PEN_STYLES[p.style()] << ',' << int(p.capStyle())
<< ',' << int(p.joinStyle()) << ',' << p.dashPattern()
<< ',' << p.style() << ',' << p.capStyle()
<< ',' << p.joinStyle() << ',' << p.dashPattern()
<< ',' << p.dashOffset()
<< ',' << p.miterLimit() << ')';
return dbg;