From 22009b69674a8679a1a90bcea4b9914538287f8b Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Thu, 7 Sep 2023 11:39:41 +0200 Subject: [PATCH] QmlDesigner: Add PropertyNameLiteral That hopefully makes it really hard to create a literal from dynamic or automatic memory. Only global string literals should be used. In header you should use: MakeHeaderPropertyViewLiteral(foo, "Foo"); Makes foo constexpr and provides internal linkage. In cpp files you should use: MakeHeaderPropertyViewLiteral(foo, "Foo"); Makes foo constexpr and provides external linkage. In a function scope you should use: MakeCppPropertyViewLiteral(foo, "Foo"); Makes foo constexpr but does not add it to the stack. All three provide a const QString& on demand. So no code is executed as the program is started. Change-Id: I924efbb1f5c2b2c811f3eeae042ff2eec8de3faf Reviewed-by: Aleksei German Reviewed-by: Reviewed-by: Qt CI Patch Build Bot --- .../designercore/include/modelfwd.h | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/plugins/qmldesigner/designercore/include/modelfwd.h b/src/plugins/qmldesigner/designercore/include/modelfwd.h index 59c5c467691..eedc46e3b56 100644 --- a/src/plugins/qmldesigner/designercore/include/modelfwd.h +++ b/src/plugins/qmldesigner/designercore/include/modelfwd.h @@ -7,11 +7,46 @@ #include "qmldesignercorelib_exports.h" #include +#include namespace QmlDesigner { using PropertyName = QByteArray; using PropertyNameView = QByteArrayView; using PropertyNameList = QList; + +template +class ByteArrayViewLiteral : public QByteArrayView +{ +public: + constexpr ByteArrayViewLiteral() noexcept + : QByteArrayView{Text, Size - 1} + {} + + const QByteArray &toByteArray() const noexcept + { + static auto b = QByteArray::fromRawData(Text, Size - 1); + + return b; + } + + operator const QByteArray &() const noexcept { return toByteArray(); } +}; + +#define MakeCppPropertyViewLiteral(name, text) \ + constexpr char name##InternalString[] = text; \ + constexpr ByteArrayViewLiteral name{}; \ + /* */ + +#define MakeHeaderPropertyViewLiteral(name, text) \ + inline constexpr char name##InternalString[] = text; \ + inline constexpr ByteArrayViewLiteral name{}; \ + /* */ + +#define MakeFunctionPropertyViewLiteral(name, text) \ + static constexpr char name##InternalString[] = text; \ + static constexpr ByteArrayViewLiteral name{}; \ + /* */ + using TypeName = QByteArray; using PropertyTypeList = QList; using IdName = QByteArray;