From 9c0c998698ed5dbba38a4de74991ee620ffea5e8 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 20 Jul 2022 16:22:13 +0200 Subject: [PATCH] QStringIterator: fix UB [1/2]: use has{Next,Previous}() more Replace - pos > i with hasPrevious() - pos < e with hasNext() Everything is inline, so there's no difference in assembly, but less source code lines to fix later. Change-Id: I3f9cf2716c96b811b29b75fa20f88cc3b461771a Reviewed-by: Mate Barany Reviewed-by: Thiago Macieira Reviewed-by: Edward Welbourne (cherry picked from commit 34800d1f09447e921203561c0e4804c4f095136f) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/text/qstringiterator_p.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/corelib/text/qstringiterator_p.h b/src/corelib/text/qstringiterator_p.h index 886b2047729..4665d738957 100644 --- a/src/corelib/text/qstringiterator_p.h +++ b/src/corelib/text/qstringiterator_p.h @@ -85,7 +85,7 @@ public: Q_ASSERT_X(hasNext(), Q_FUNC_INFO, "iterator hasn't a next item"); if (Q_UNLIKELY((pos++)->isHighSurrogate())) { - Q_ASSERT(pos < e && pos->isLowSurrogate()); + Q_ASSERT(hasNext() && pos->isLowSurrogate()); ++pos; } } @@ -124,7 +124,7 @@ public: const QChar cur = *pos++; if (Q_UNLIKELY(cur.isHighSurrogate())) { - Q_ASSERT(pos < e && pos->isLowSurrogate()); + Q_ASSERT(hasNext() && pos->isLowSurrogate()); return QChar::surrogateToUcs4(cur, *pos++); } return cur.unicode(); @@ -136,7 +136,7 @@ public: const QChar uc = *pos++; if (Q_UNLIKELY(uc.isSurrogate())) { - if (Q_LIKELY(uc.isHighSurrogate() && pos < e && pos->isLowSurrogate())) + if (Q_LIKELY(uc.isHighSurrogate() && hasNext() && pos->isLowSurrogate())) return QChar::surrogateToUcs4(uc, *pos++); return invalidAs; } @@ -167,7 +167,7 @@ public: Q_ASSERT_X(hasPrevious(), Q_FUNC_INFO, "iterator hasn't a previous item"); if (Q_UNLIKELY((--pos)->isLowSurrogate())) { - Q_ASSERT(pos > i && pos[-1].isHighSurrogate()); + Q_ASSERT(hasPrevious() && pos[-1].isHighSurrogate()); --pos; } } @@ -205,7 +205,7 @@ public: const QChar cur = *--pos; if (Q_UNLIKELY(cur.isLowSurrogate())) { - Q_ASSERT(pos > i && pos[-1].isHighSurrogate()); + Q_ASSERT(hasPrevious() && pos[-1].isHighSurrogate()); return QChar::surrogateToUcs4(*--pos, cur); } return cur.unicode(); @@ -217,7 +217,7 @@ public: const QChar uc = *--pos; if (Q_UNLIKELY(uc.isSurrogate())) { - if (Q_LIKELY(uc.isLowSurrogate() && pos > i && pos[-1].isHighSurrogate())) + if (Q_LIKELY(uc.isLowSurrogate() && hasPrevious() && pos[-1].isHighSurrogate())) return QChar::surrogateToUcs4(*--pos, uc); return invalidAs; }