From 5861bd8b788578c3823c2e562f3a355a30d4ddf0 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Thu, 26 Nov 2020 18:58:50 +0000 Subject: [PATCH] fix some QByteArrayMatcher and QStringMatcher bugs Signed-off-by: Ivailo Monev --- src/core/tools/qbytearraymatcher.cpp | 27 +++++++++++---------------- src/core/tools/qbytearraymatcher.h | 9 +-------- src/core/tools/qstringmatcher.cpp | 30 ++++++++++++------------------ src/core/tools/qstringmatcher.h | 7 +------ 4 files changed, 25 insertions(+), 48 deletions(-) diff --git a/src/core/tools/qbytearraymatcher.cpp b/src/core/tools/qbytearraymatcher.cpp index 7312e2a2e..2c33f0590 100644 --- a/src/core/tools/qbytearraymatcher.cpp +++ b/src/core/tools/qbytearraymatcher.cpp @@ -110,9 +110,7 @@ static inline int bm_find(const uchar *cc, int l, int index, const uchar *puc, u */ QByteArrayMatcher::QByteArrayMatcher() { - p.p = 0; - p.l = 0; - memset(p.q_skiptable, 0, sizeof(p.q_skiptable)); + ::memset(q_skiptable, 0, sizeof(q_skiptable)); } /*! @@ -121,10 +119,9 @@ QByteArrayMatcher::QByteArrayMatcher() the destructor does not delete \a pattern. */ QByteArrayMatcher::QByteArrayMatcher(const char *pattern, int length) + : q_pattern(QByteArray(pattern, length)) { - p.p = reinterpret_cast(pattern); - p.l = length; - bm_init_skiptable(p.p, p.l, p.q_skiptable); + bm_init_skiptable(reinterpret_cast(pattern), length, q_skiptable); } /*! @@ -134,9 +131,7 @@ QByteArrayMatcher::QByteArrayMatcher(const char *pattern, int length) QByteArrayMatcher::QByteArrayMatcher(const QByteArray &pattern) : q_pattern(pattern) { - p.p = reinterpret_cast(pattern.constData()); - p.l = pattern.size(); - bm_init_skiptable(p.p, p.l, p.q_skiptable); + bm_init_skiptable(reinterpret_cast(pattern.constData()), pattern.size(), q_skiptable); } /*! @@ -159,8 +154,10 @@ QByteArrayMatcher::~QByteArrayMatcher() */ QByteArrayMatcher &QByteArrayMatcher::operator=(const QByteArrayMatcher &other) { - q_pattern = other.q_pattern; - memcpy(&p, &other.p, sizeof(p)); + if (this != &other) { + q_pattern = other.q_pattern; + ::memcpy(&q_skiptable, &other.q_skiptable, sizeof(q_skiptable)); + } return *this; } @@ -173,9 +170,7 @@ QByteArrayMatcher &QByteArrayMatcher::operator=(const QByteArrayMatcher &other) void QByteArrayMatcher::setPattern(const QByteArray &pattern) { q_pattern = pattern; - p.p = reinterpret_cast(pattern.constData()); - p.l = pattern.size(); - bm_init_skiptable(p.p, p.l, p.q_skiptable); + bm_init_skiptable(reinterpret_cast(pattern.constData()), pattern.size(), q_skiptable); } /*! @@ -190,7 +185,7 @@ int QByteArrayMatcher::indexIn(const QByteArray &ba, int from) const if (from < 0) from = 0; return bm_find(reinterpret_cast(ba.constData()), ba.size(), from, - p.p, p.l, p.q_skiptable); + reinterpret_cast(q_pattern.constData()), q_pattern.size(), q_skiptable); } /*! @@ -205,7 +200,7 @@ int QByteArrayMatcher::indexIn(const char *str, int len, int from) const if (from < 0) from = 0; return bm_find(reinterpret_cast(str), len, from, - p.p, p.l, p.q_skiptable); + reinterpret_cast(q_pattern.constData()), q_pattern.size(), q_skiptable); } /*! diff --git a/src/core/tools/qbytearraymatcher.h b/src/core/tools/qbytearraymatcher.h index 2ec9468ce..30a814c85 100644 --- a/src/core/tools/qbytearraymatcher.h +++ b/src/core/tools/qbytearraymatcher.h @@ -57,19 +57,12 @@ public: int indexIn(const char *str, int len, int from = 0) const; inline QByteArray pattern() const { - if (q_pattern.isNull()) - return QByteArray(reinterpret_cast(p.p), p.l); return q_pattern; } private: QByteArray q_pattern; - struct Data { - uchar q_skiptable[256]; - const uchar *p; - int l; - }; - Data p; + uchar q_skiptable[256]; }; QT_END_NAMESPACE diff --git a/src/core/tools/qstringmatcher.cpp b/src/core/tools/qstringmatcher.cpp index b11f81712..f2f4d812c 100644 --- a/src/core/tools/qstringmatcher.cpp +++ b/src/core/tools/qstringmatcher.cpp @@ -144,6 +144,7 @@ static inline int bm_find(const ushort *uc, uint l, int index, const ushort *puc QStringMatcher::QStringMatcher() : q_cs(Qt::CaseSensitive) { + ::memset(q_skiptable, 0, sizeof(q_skiptable)); } /*! @@ -155,9 +156,7 @@ QStringMatcher::QStringMatcher() QStringMatcher::QStringMatcher(const QString &pattern, Qt::CaseSensitivity cs) : q_pattern(pattern), q_cs(cs) { - p.uc = pattern.unicode(); - p.len = pattern.size(); - bm_init_skiptable((const ushort *)p.uc, p.len, p.q_skiptable, cs); + bm_init_skiptable((const ushort *)pattern.unicode(), pattern.size(), q_skiptable, cs); } /*! @@ -168,11 +167,9 @@ QStringMatcher::QStringMatcher(const QString &pattern, Qt::CaseSensitivity cs) by \a uc with the given \a length and case sensitivity specified by \a cs. */ QStringMatcher::QStringMatcher(const QChar *uc, int len, Qt::CaseSensitivity cs) - : q_cs(cs) + : q_pattern(QString(uc, len)), q_cs(cs) { - p.uc = uc; - p.len = len; - bm_init_skiptable((const ushort *)p.uc, len, p.q_skiptable, cs); + bm_init_skiptable((const ushort *)uc, len, q_skiptable, cs); } /*! @@ -198,6 +195,7 @@ QStringMatcher &QStringMatcher::operator=(const QStringMatcher &other) if (this != &other) { q_pattern = other.q_pattern; q_cs = other.q_cs; + ::memcpy(&q_skiptable, &other.q_skiptable, sizeof(q_skiptable)); } return *this; } @@ -211,9 +209,7 @@ QStringMatcher &QStringMatcher::operator=(const QStringMatcher &other) void QStringMatcher::setPattern(const QString &pattern) { q_pattern = pattern; - p.uc = pattern.unicode(); - p.len = pattern.size(); - bm_init_skiptable((const ushort *)p.uc, pattern.size(), p.q_skiptable, q_cs); + bm_init_skiptable((const ushort *)pattern.unicode(), pattern.size(), q_skiptable, q_cs); } /*! @@ -227,9 +223,7 @@ void QStringMatcher::setPattern(const QString &pattern) QString QStringMatcher::pattern() const { - if (!q_pattern.isEmpty()) - return q_pattern; - return QString(p.uc, p.len); + return q_pattern; } /*! @@ -242,7 +236,7 @@ void QStringMatcher::setCaseSensitivity(Qt::CaseSensitivity cs) { if (cs == q_cs) return; - bm_init_skiptable((const ushort *)q_pattern.unicode(), q_pattern.size(), p.q_skiptable, cs); + bm_init_skiptable((const ushort *)q_pattern.unicode(), q_pattern.size(), q_skiptable, cs); q_cs = cs; } @@ -260,8 +254,8 @@ int QStringMatcher::indexIn(const QString &str, int from) const if (from < 0) from = 0; return bm_find((const ushort *)str.unicode(), str.size(), from, - (const ushort *)p.uc, p.len, - p.q_skiptable, q_cs); + (const ushort *)q_pattern.unicode(), q_pattern.size(), + q_skiptable, q_cs); } /*! @@ -281,8 +275,8 @@ int QStringMatcher::indexIn(const QChar *str, int length, int from) const if (from < 0) from = 0; return bm_find((const ushort *)str, length, from, - (const ushort *)p.uc, p.len, - p.q_skiptable, q_cs); + (const ushort *)q_pattern.unicode(), q_pattern.size(), + q_skiptable, q_cs); } /*! diff --git a/src/core/tools/qstringmatcher.h b/src/core/tools/qstringmatcher.h index 6f32ddc0a..6c2770dce 100644 --- a/src/core/tools/qstringmatcher.h +++ b/src/core/tools/qstringmatcher.h @@ -65,12 +65,7 @@ public: private: QString q_pattern; Qt::CaseSensitivity q_cs; - struct Data { - uchar q_skiptable[256]; - const QChar *uc; - int len; - }; - Data p; + uchar q_skiptable[256]; }; QT_END_NAMESPACE -- 2.11.0