From 06f996a139a5ac660e98163fac796f94c1a6468f Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Tue, 27 Jun 2017 20:57:31 +0200 Subject: Ensure we can deal with non-null terminated strings. --- common/contactpreprocessor.cpp | 3 +-- common/propertymapper.cpp | 16 +++++++------ tests/domainadaptortest.cpp | 51 +++++++++++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/common/contactpreprocessor.cpp b/common/contactpreprocessor.cpp index 68a8acb..d331421 100644 --- a/common/contactpreprocessor.cpp +++ b/common/contactpreprocessor.cpp @@ -36,8 +36,7 @@ void updatedProperties(Sink::ApplicationDomain::Contact &contact, const KContact } contact.setEmails(emails); - const auto photo = addressee.photo().rawData(); - contact.setPhoto(photo); + contact.setPhoto(addressee.photo().rawData()); } ContactPropertyExtractor::~ContactPropertyExtractor() diff --git a/common/propertymapper.cpp b/common/propertymapper.cpp index c14a62e..dbf93a3 100644 --- a/common/propertymapper.cpp +++ b/common/propertymapper.cpp @@ -58,7 +58,9 @@ template <> flatbuffers::uoffset_t variantToProperty(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) { if (property.isValid()) { - return fbb.CreateString(property.toByteArray().toStdString()).o; + const auto ba = property.toByteArray(); + const auto s = fbb.CreateString(ba.constData(), ba.size()); + return s.o; } return 0; } @@ -135,7 +137,7 @@ QString propertyToString(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid - return QString::fromStdString(property->c_str()); + return QString::fromStdString(property->str()); } return QString(); } @@ -145,7 +147,7 @@ QVariant propertyToVariant(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid - return QString::fromStdString(property->c_str()); + return QString::fromStdString(property->str()); } return QVariant(); } @@ -155,7 +157,7 @@ QVariant propertyToVariant(const flatbuffers::Str { if (property) { // We have to copy the memory, otherwise it would become eventually invalid - auto s = QString::fromStdString(property->c_str()); + auto s = QString::fromStdString(property->str()); auto ext = s.endsWith(":ext"); s.chop(4); @@ -171,7 +173,7 @@ QVariant propertyToVariant(const flatbuffers { if (property) { // We have to copy the memory, otherwise it would become eventually invalid - return QVariant::fromValue(Sink::ApplicationDomain::Reference{QString::fromStdString(property->c_str()).toUtf8()}); + return QVariant::fromValue(Sink::ApplicationDomain::Reference{QString::fromStdString(property->str()).toUtf8()}); } return QVariant(); } @@ -181,7 +183,7 @@ QVariant propertyToVariant(const flatbuffers::String *property) { if (property) { // We have to copy the memory, otherwise it would become eventually invalid - return QString::fromStdString(property->c_str()).toUtf8(); + return QByteArray(property->c_str(), property->Length()); } return QVariant(); } @@ -203,7 +205,7 @@ QVariant propertyToVariant(const flatbuffers::Vectorbegin(); it != property->end();) { // We have to copy the memory, otherwise it would become eventually invalid - list << QString::fromStdString((*it)->c_str()).toUtf8(); + list << QString::fromStdString((*it)->str()).toUtf8(); it.operator++(); } return QVariant::fromValue(list); diff --git a/tests/domainadaptortest.cpp b/tests/domainadaptortest.cpp index df44819..2aed0a9 100644 --- a/tests/domainadaptortest.cpp +++ b/tests/domainadaptortest.cpp @@ -17,17 +17,19 @@ class TestFactory : public DomainTypeAdaptorFactory { public: - TestFactory() - { - } + TestFactory() = default; }; class TestMailFactory : public DomainTypeAdaptorFactory { public: - TestMailFactory() - { - } + TestMailFactory() = default; +}; + +class TestContactFactory : public DomainTypeAdaptorFactory +{ +public: + TestContactFactory() = default; }; /** @@ -135,6 +137,43 @@ private slots: } } + + void testContact() + { + auto writeMapper = QSharedPointer::create(); + Sink::ApplicationDomain::TypeImplementation::configure(*writeMapper); + + auto binaryData = QByteArray::fromRawData("\xEF\xBF\xBD\x00\xEF\xBF", 5); + + Sink::ApplicationDomain::Contact contact; + contact.setPhoto(binaryData); + QVERIFY(!contact.getPhoto().isEmpty()); + + flatbuffers::FlatBufferBuilder metadataFbb; + auto metadataBuilder = Sink::MetadataBuilder(metadataFbb); + metadataBuilder.add_revision(1); + auto metadataBuffer = metadataBuilder.Finish(); + Sink::FinishMetadataBuffer(metadataFbb, metadataBuffer); + + flatbuffers::FlatBufferBuilder mailFbb; + auto pos = createBufferPart(contact, mailFbb, *writeMapper); + Sink::ApplicationDomain::Buffer::FinishContactBuffer(mailFbb, pos); + + flatbuffers::FlatBufferBuilder fbb; + Sink::EntityBuffer::assembleEntityBuffer( + fbb, metadataFbb.GetBufferPointer(), metadataFbb.GetSize(), mailFbb.GetBufferPointer(), mailFbb.GetSize(), mailFbb.GetBufferPointer(), mailFbb.GetSize()); + + { + std::string data(reinterpret_cast(fbb.GetBufferPointer()), fbb.GetSize()); + Sink::EntityBuffer buffer((void *)(data.data()), data.size()); + + TestContactFactory factory; + auto adaptor = factory.createAdaptor(buffer.entity()); + Sink::ApplicationDomain::Contact readContact{QByteArray{}, QByteArray{}, 0, adaptor}; + QCOMPARE(readContact.getPhoto(), contact.getPhoto()); + } + + } }; QTEST_MAIN(DomainAdaptorTest) -- cgit v1.2.3