From 5365018c35e0a496376bde9bf7e4bb4e9df6de2a Mon Sep 17 00:00:00 2001 From: Linus Jahn Date: Wed, 28 Dec 2022 20:35:34 +0100 Subject: StanzaError: Add NoType and NoCondition for -1 values Previsously static_cast(-1) was used when no condition was set (or type). This adds real enum values with that integer value to avoid undefined behaviour. Fixes #495. --- src/base/QXmppStanza.cpp | 13 ++++++------- src/base/QXmppStanza.h | 2 ++ 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 'src/base') diff --git a/src/base/QXmppStanza.cpp b/src/base/QXmppStanza.cpp index e7cb0208..fa170740 100644 --- a/src/base/QXmppStanza.cpp +++ b/src/base/QXmppStanza.cpp @@ -399,12 +399,12 @@ void QXmppStanza::Error::setCode(int code) /// The conditions QXmppStanza::Error::Gone and QXmppStanza::Error::Redirect /// can be used in combination with redirectUri(). /// -/// \warning Due to compatibility this returns \c Condition(-1) when no -/// condition is set. When possible you should use conditionOpt(). +/// \warning This returns NoCondition when no condition is set. When possible you should use +/// conditionOpt(). /// QXmppStanza::Error::Condition QXmppStanza::Error::condition() const { - return d->condition.value_or(QXmppStanza::Error::Condition(-1)); + return d->condition.value_or(NoCondition); } /// @@ -426,7 +426,7 @@ auto QXmppStanza::Error::conditionOpt() const -> std::optional /// The conditions QXmppStanza::Error::Gone and QXmppStanza::Error::Redirect /// can be used in combination with setRedirectUri(). /// -void QXmppStanza::Error::setCondition(QXmppStanza::Error::Condition cond) +void QXmppStanza::Error::setCondition(Condition cond) { if (int(cond) < 0) { d->condition = std::nullopt; @@ -451,12 +451,11 @@ void QXmppStanza::Error::setCondition(std::optional cond) /// /// Returns the type of the error. /// -/// \warning Due to compatibility this returns \c Type(-1) when no type is set. -/// When possible you should use typeOpt(). +/// \warning This returns NoType when no type is set. When possible you should use typeOpt(). /// QXmppStanza::Error::Type QXmppStanza::Error::type() const { - return d->type.value_or(QXmppStanza::Error::Type(-1)); + return d->type.value_or(NoType); } /// diff --git a/src/base/QXmppStanza.h b/src/base/QXmppStanza.h index 394bc6c6..320bab26 100644 --- a/src/base/QXmppStanza.h +++ b/src/base/QXmppStanza.h @@ -98,6 +98,7 @@ public: /// The error descriptions are not detailed in here. The exact meaning /// can be found in the particular protocols using them. enum Type { + NoType = -1, Cancel, ///< The error is not temporary. Continue, ///< The error was only a warning. Modify, ///< The request needs to be changed and resent. @@ -107,6 +108,7 @@ public: /// A detailed condition of the error enum Condition { + NoCondition = -1, BadRequest, ///< The request does not contain a valid schema. Conflict, ///< The request conflicts with another. FeatureNotImplemented, ///< The feature is not implemented. -- cgit v1.2.3