From b877c35d4b2cc67f7c3d96698fcd3845683ce5e2 Mon Sep 17 00:00:00 2001 From: Scott Linder Date: Mon, 16 Nov 2020 18:41:25 +0000 Subject: [PATCH] [YAMLIO] Correctly diagnose empty alias/anchor 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 | 2 +- llvm/unittests/Support/YAMLIOTest.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Support/YAMLParser.cpp b/llvm/lib/Support/YAMLParser.cpp index b9bbdc3..06c3120 100644 --- a/llvm/lib/Support/YAMLParser.cpp +++ b/llvm/lib/Support/YAMLParser.cpp @@ -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; } diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp index c7df4b9..e4e3fe0 100644 --- a/llvm/unittests/Support/YAMLIOTest.cpp +++ b/llvm/unittests/Support/YAMLIOTest.cpp @@ -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()); +} -- 2.7.4