From a82bd03c61014b1fe3e27826fa4b0f5d91486010 Mon Sep 17 00:00:00 2001 From: Sergei Ilinykh Date: Wed, 12 Jun 2024 14:01:13 +0300 Subject: [PATCH] Don't close file in vcard::fromDevice on read failure --- src/xmpp/xmpp-im/xmpp_vcard4.cpp | 18 +++++++++--------- src/xmpp/xmpp-im/xmpp_vcard4.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/xmpp/xmpp-im/xmpp_vcard4.cpp b/src/xmpp/xmpp-im/xmpp_vcard4.cpp index 0a2ea0f9..2542b025 100644 --- a/src/xmpp/xmpp-im/xmpp_vcard4.cpp +++ b/src/xmpp/xmpp-im/xmpp_vcard4.cpp @@ -715,29 +715,29 @@ VCard VCard::fromFile(const QString &filename) if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return VCard(); - return fromFile(file); + auto vcard = fromDevice(&file); + file.close(); + return vcard; } -VCard VCard::fromFile(QFile &file) +VCard VCard::fromDevice(QIODevice *dev) { QDomDocument doc; #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - if (!doc.setContent(&file, true)) { + if (!doc.setContent(dev, true)) { #else - if (!doc.setContent(&file, QDomDocument::ParseOption::UseNamespaceProcessing)) { + if (!doc.setContent(dev, QDomDocument::ParseOption::UseNamespaceProcessing)) { #endif - file.close(); - return VCard(); + return {}; } - file.close(); QDomElement root = doc.documentElement(); if (root.tagName() != QLatin1String("vcards") || root.namespaceURI() != VCARD_NAMESPACE) - return VCard(); + return {}; QDomElement vCardElement = root.firstChildElement(QLatin1String("vcard")); if (vCardElement.isNull()) - return VCard(); + return {}; return VCard(vCardElement); } diff --git a/src/xmpp/xmpp-im/xmpp_vcard4.h b/src/xmpp/xmpp-im/xmpp_vcard4.h index 9a5790ac..e4ce7d98 100644 --- a/src/xmpp/xmpp-im/xmpp_vcard4.h +++ b/src/xmpp/xmpp-im/xmpp_vcard4.h @@ -252,7 +252,7 @@ class VCard { QDomElement toXmlElement(QDomDocument &document) const; static VCard fromFile(const QString &filename); - static VCard fromFile(QFile &file); + static VCard fromDevice(QIODevice *dev); bool save(const QString &filename) const; void fromVCardTemp(const XMPP::VCard &tempVCard);