From 8e878604ddc92dbaa57e34ced67ccbc3ae8f90db Mon Sep 17 00:00:00 2001
From: Ivan Solovev <ivan.solovev@qt.io>
Date: Fri, 09 Oct 2026 15:14:39 +0200
Subject: [PATCH] QNdefRecord: fix construction from an empty record

A default-constructed QNdefRecord has its d_ptr == nullptr.
However, all classes derived from QNdefRecord implemented their copy
constructors with the assumption that d_ptr is non-null.
This caused a crash when copy-constructing from a default-constructed
QNdefRecord.

Fix by adding proper checks.

Amends 29ba8297443cf76d4f647bde329d62c2a567c709 (beginning of the
public history for the module)
and db2fc63155e8c821f5c6c751a33974cab9cb4ed3 (5.2)

Fixes: QTBUG-151318
Pick-to: 6.8 6.5 5.15
Change-Id: I385d89dcd8742bb96203c19e45ff26c407c00519
Reviewed-by: Mate Barany <mate.barany@qt.io>
(cherry picked from commit 05a16fe103624202d2b0ed37fe1a894f672c5afb)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
---

diff --git a/src/nfc/qndefrecord.cpp b/src/nfc/qndefrecord.cpp
index 51df2ab..b38d0d9 100644
--- a/src/nfc/qndefrecord.cpp
+++ b/src/nfc/qndefrecord.cpp
@@ -144,7 +144,7 @@
 QNdefRecord::QNdefRecord(const QNdefRecord &other, TypeNameFormat typeNameFormat,
                          const QByteArray &type)
 {
-    if (other.d->typeNameFormat == quint8(typeNameFormat) && other.d->type == type) {
+    if (other.d && other.d->typeNameFormat == quint8(typeNameFormat) && other.d->type == type) {
         d = other.d;
     } else {
         d = new QNdefRecordPrivate;
@@ -162,7 +162,7 @@
 */
 QNdefRecord::QNdefRecord(const QNdefRecord &other, TypeNameFormat typeNameFormat)
 {
-    if (other.d->typeNameFormat == quint8(typeNameFormat)) {
+    if (other.d && other.d->typeNameFormat == quint8(typeNameFormat)) {
         d = other.d;
     } else {
         d = new QNdefRecordPrivate;
diff --git a/tests/auto/qndefnfcsmartposterrecord/tst_qndefnfcsmartposterrecord.cpp b/tests/auto/qndefnfcsmartposterrecord/tst_qndefnfcsmartposterrecord.cpp
index 602946a..5b2ab2d 100644
--- a/tests/auto/qndefnfcsmartposterrecord/tst_qndefnfcsmartposterrecord.cpp
+++ b/tests/auto/qndefnfcsmartposterrecord/tst_qndefnfcsmartposterrecord.cpp
@@ -491,6 +491,15 @@
 
     QVERIFY(record == sprecord);
     QVERIFY(!(record != sprecord));
+
+    // Construct from an empty QNdefRecrod
+    {
+        QNdefRecord empty;
+        QNdefNfcSmartPosterRecord rec(empty);
+        QCOMPARE(rec.type(), "Sp");
+        QCOMPARE(rec.typeNameFormat(), QNdefRecord::NfcRtd);
+        QVERIFY(rec.payload().isEmpty());
+    }
 }
 
 void tst_QNdefNfcSmartPosterRecord::tst_assign()
diff --git a/tests/auto/qndefrecord/tst_qndefrecord.cpp b/tests/auto/qndefrecord/tst_qndefrecord.cpp
index c6ec6d6..374f343 100644
--- a/tests/auto/qndefrecord/tst_qndefrecord.cpp
+++ b/tests/auto/qndefrecord/tst_qndefrecord.cpp
@@ -36,6 +36,8 @@
 
     void tst_ndefRecord_data();
     void tst_ndefRecord();
+
+    void fromOtherType();
 };
 
 tst_QNdefRecord::tst_QNdefRecord()
@@ -471,6 +473,51 @@
     }
 }
 
+void tst_QNdefRecord::fromOtherType()
+{
+    QNdefRecord empty;
+
+    QNdefNfcTextRecord text;
+    text.setText("Hello, world!");
+    text.setLocale("en_US");
+    text.setEncoding(QNdefNfcTextRecord::Utf16);
+
+    QNdefNfcUriRecord uri;
+    uri.setUri(QUrl("http://qt-project.org/"));
+
+    // All constructors from incompatible type should yield records of
+    // the corresponding type with an empty payload
+
+    {
+        // text from empty
+        QNdefNfcTextRecord copy = empty;
+        QCOMPARE(copy.type(), "T");
+        QCOMPARE(copy.typeNameFormat(), QNdefRecord::NfcRtd);
+        QVERIFY(copy.payload().isEmpty());
+    }
+    {
+        // text from uri
+        QNdefNfcTextRecord copy = uri;
+        QCOMPARE(copy.type(), "T");
+        QCOMPARE(copy.typeNameFormat(), QNdefRecord::NfcRtd);
+        QVERIFY(copy.payload().isEmpty());
+    }
+    {
+        // uri from empty
+        QNdefNfcUriRecord copy = empty;
+        QCOMPARE(copy.type(), "U");
+        QCOMPARE(copy.typeNameFormat(), QNdefRecord::NfcRtd);
+        QVERIFY(copy.payload().isEmpty());
+    }
+    {
+        // uri from text
+        QNdefNfcUriRecord copy = text;
+        QCOMPARE(copy.type(), "U");
+        QCOMPARE(copy.typeNameFormat(), QNdefRecord::NfcRtd);
+        QVERIFY(copy.payload().isEmpty());
+    }
+}
+
 QTEST_MAIN(tst_QNdefRecord)
 
 #include "tst_qndefrecord.moc"
