[PATCH] D105533: [clang] Fix an infinite loop during typo-correctionSee https://bugs.llvm.org/show_bug.cgi?id=50797#c6

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 6 23:48:54 PDT 2021


hokein created this revision.
hokein added a reviewer: dgoldman.
hokein requested review of this revision.
Herald added a project: clang.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105533

Files:
  clang/lib/Sema/SemaExprCXX.cpp
  clang/test/Sema/typo-correction-no-hang.c


Index: clang/test/Sema/typo-correction-no-hang.c
===================================================================
--- /dev/null
+++ clang/test/Sema/typo-correction-no-hang.c
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// PR50797
+struct a {
+  int xxx;
+};
+
+int g_107;
+int g_108;
+int g_109;
+
+struct a g_999; // expected-note 2{{'g_999' declared here}}
+
+void b() { (g_910.xxx = g_910.xxx); } //expected-error 2{{use of undeclared identifier 'g_910'; did you mean 'g_999'}}
Index: clang/lib/Sema/SemaExprCXX.cpp
===================================================================
--- clang/lib/Sema/SemaExprCXX.cpp
+++ clang/lib/Sema/SemaExprCXX.cpp
@@ -8326,6 +8326,8 @@
           // `TryTransform` which will iterate through corrections in
           // `TransformTypoExpr`.
           TransformCache.erase(TE);
+          const auto *CurrentCorrection =
+              &SemaRef.getTypoExprState(TE).Consumer->getCurrentCorrection();
           ExprResult AmbigRes = CheckForRecursiveTypos(TryTransform(E), IsAmbiguous);
 
           if (!AmbigRes.isInvalid() || IsAmbiguous) {
@@ -8335,6 +8337,11 @@
             IsAmbiguous = true;
             break;
           }
+          // Bail out if we didn't make any correction progress on the checking
+          // TypoExpr TE, otherwise we risk running the loop forever.
+          if (CurrentCorrection ==
+              &SemaRef.getTypoExprState(TE).Consumer->getCurrentCorrection())
+            break;
         } while ((Next = SemaRef.getTypoExprState(TE).Consumer->peekNextCorrection()) &&
                  Next.getEditDistance(false) == TC.getEditDistance(false));
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105533.356877.patch
Type: text/x-patch
Size: 1656 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210707/f9447241/attachment.bin>


More information about the cfe-commits mailing list