From 395d2af80de7817dd2b092c2c7d9dfa3fa3f2744 Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Wed, 16 Mar 2022 19:29:04 +0100 Subject: StartTlsPacket: Fix UB when parsing invalid type --- src/base/QXmppStartTlsPacket.cpp | 14 ++++++++++---- src/base/QXmppStartTlsPacket.h | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'src/base') diff --git a/src/base/QXmppStartTlsPacket.cpp b/src/base/QXmppStartTlsPacket.cpp index f4c5e2fb..445f519e 100644 --- a/src/base/QXmppStartTlsPacket.cpp +++ b/src/base/QXmppStartTlsPacket.cpp @@ -65,14 +65,20 @@ void QXmppStartTlsPacket::parse(const QDomElement &element) if (!QXmppStartTlsPacket::isStartTlsPacket(element)) return; - m_type = Type(STARTTLS_TYPES.indexOf(element.tagName())); + if (auto index = STARTTLS_TYPES.indexOf(element.tagName()); index >= 0) { + m_type = Type(index); + } else { + m_type = Invalid; + } } void QXmppStartTlsPacket::toXml(QXmlStreamWriter *writer) const { - writer->writeStartElement(STARTTLS_TYPES.at(int(m_type))); - writer->writeDefaultNamespace(ns_tls); - writer->writeEndElement(); + if (m_type != Invalid) { + writer->writeStartElement(STARTTLS_TYPES.at(int(m_type))); + writer->writeDefaultNamespace(ns_tls); + writer->writeEndElement(); + } } /// \endcond diff --git a/src/base/QXmppStartTlsPacket.h b/src/base/QXmppStartTlsPacket.h index 9b91bef9..5396f5d9 100644 --- a/src/base/QXmppStartTlsPacket.h +++ b/src/base/QXmppStartTlsPacket.h @@ -39,7 +39,8 @@ public: enum Type { StartTls, ///< Used by the client to initiate STARTTLS. Proceed, ///< Used by the server to accept STARTTLS. - Failure ///< Used by the server to reject STARTTLS. + Failure, ///< Used by the server to reject STARTTLS. + Invalid, ///< Invalid type }; QXmppStartTlsPacket(Type type = StartTls); -- cgit v1.2.3