[YAMLIO] Correctly diagnose empty alias/anchor
authorScott Linder <Scott.Linder@amd.com>
Mon, 16 Nov 2020 18:41:25 +0000 (18:41 +0000)
committerScott Linder <Scott.Linder@amd.com>
Mon, 16 Nov 2020 18:45:05 +0000 (18:45 +0000)
The `Range` of an alias/anchor token includes the leading `&` or `*`,
but it is skipped while parsing the name. The check for an empty name
fails to account for the skipped leading character and so the error is
never hit.

Fix the off-by-one and add a couple regression tests.

Reviewed By: dexonsmith

Differential Revision: https://reviews.llvm.org/D91462

llvm/lib/Support/YAMLParser.cpp
llvm/unittests/Support/YAMLIOTest.cpp

index b9bbdc3..06c3120 100644 (file)
@@ -1423,7 +1423,7 @@ bool Scanner::scanAliasOrAnchor(bool IsAlias) {
     ++Column;
   }
 
-  if (Start == Current) {
+  if (Start + 1 == Current) {
     setError("Got empty alias or anchor", Start);
     return false;
   }
index c7df4b9..e4e3fe0 100644 (file)
@@ -3101,3 +3101,15 @@ TEST(YAMLIO, TestUnknownDirective) {
   EXPECT_FALSE(yin2.setCurrentDocument());
   EXPECT_TRUE(yin2.error());
 }
+
+TEST(YAMLIO, TestEmptyAlias) {
+  Input yin("&");
+  EXPECT_FALSE(yin.setCurrentDocument());
+  EXPECT_TRUE(yin.error());
+}
+
+TEST(YAMLIO, TestEmptyAnchor) {
+  Input yin("*");
+  EXPECT_FALSE(yin.setCurrentDocument());
+  EXPECT_TRUE(yin.error());
+}