From fd43b1694f1382a3a47f5dc546ebe3d39fcd6e7d Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 20 Jan 2020 14:14:49 +0100 Subject: Implement short patterns (patterns starting with ~) Also make pattern detector in cacheset and private's list accept such patterns. We probably should just try to parse and see if it is a (start of a) pattern. --- test/libapt/pattern_test.cc | 66 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'test/libapt/pattern_test.cc') diff --git a/test/libapt/pattern_test.cc b/test/libapt/pattern_test.cc index de2fbceb9..492a29eac 100644 --- a/test/libapt/pattern_test.cc +++ b/test/libapt/pattern_test.cc @@ -93,3 +93,69 @@ TEST(TreeParserTest, ParseWithManyArgsWithSpacesWithTrailingComma) EXPECT_EQ(patternNode->term, "?hello"); EXPECT_EQ(2u, patternNode->arguments.size()); } + +// Helper +static bool samePattern(const std::unique_ptr &a, const std::unique_ptr &b) +{ + auto pa = dynamic_cast(a.get()); + auto pb = dynamic_cast(b.get()); + + if (pa && pb) + { + if (pa->term != pb->term || pa->haveArgumentList != pb->haveArgumentList || pa->arguments.size() != pb->arguments.size()) + return false; + + for (size_t i = 0; i < pa->arguments.size(); i++) + { + if (!samePattern(pa->arguments[i], pb->arguments[i])) + return false; + } + return true; + } + + auto wa = dynamic_cast(a.get()); + auto wb = dynamic_cast(b.get()); + if (wa && wb) + return wa->word == wb->word && wa->quoted == wb->quoted; + + return false; +} + +#define EXPECT_PATTERN_EQ(shrt, lng) \ + EXPECT_TRUE(samePattern(PatternTreeParser(shrt).parseTop(), PatternTreeParser(lng).parseTop())) +#define EXPECT_PATTERN_EQ_ATOMIC(shrt, lng) \ + EXPECT_TRUE(PatternTreeParser(shrt).parseTop()); \ + caught = false; \ + try \ + { \ + PatternTreeParser(shrt "XXX").parseTop(); \ + } \ + catch (PatternTreeParser::Error & e) \ + { \ + caught = true; \ + }; \ + EXPECT_TRUE(caught) << shrt "XXX should have thrown an exception"; \ + EXPECT_PATTERN_EQ(shrt, lng) + +TEST(TreeParserTest, ParseShortPattern) +{ + bool caught; + EXPECT_PATTERN_EQ("~ramd64", "?architecture(amd64)"); + EXPECT_PATTERN_EQ("~AanArchive", "?archive(anArchive)"); + EXPECT_PATTERN_EQ_ATOMIC("~M", "?automatic"); + EXPECT_PATTERN_EQ_ATOMIC("~b", "?broken"); + EXPECT_PATTERN_EQ_ATOMIC("~c", "?config-files"); + EXPECT_PATTERN_EQ_ATOMIC("~E", "?essential"); + EXPECT_PATTERN_EQ_ATOMIC("~F", "?false"); + EXPECT_PATTERN_EQ_ATOMIC("~g", "?garbage"); + EXPECT_PATTERN_EQ_ATOMIC("~i", "?installed"); + EXPECT_PATTERN_EQ("~napt", "?name(apt)"); + EXPECT_PATTERN_EQ_ATOMIC("~o", "?obsolete"); + EXPECT_PATTERN_EQ("~Obar", "?origin(bar)"); + EXPECT_PATTERN_EQ("~sfoo", "?section(foo)"); + EXPECT_PATTERN_EQ("~esourcename", "?source-package(sourcename)"); + EXPECT_PATTERN_EQ_ATOMIC("~T", "?true"); + EXPECT_PATTERN_EQ_ATOMIC("~U", "?upgradable"); + EXPECT_PATTERN_EQ("~Vverstr", "?version(verstr)"); + EXPECT_PATTERN_EQ_ATOMIC("~v", "?virtual"); +} -- cgit v1.2.3-70-g09d2 From 8d4967d3a187dd66cf14b070a9db63f8ea21b21f Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 28 Jan 2020 21:46:10 +0100 Subject: patterns: Implement unary ! --- apt-pkg/cachefilter-patterns.cc | 28 +++++++++++++++++++++++++++- apt-pkg/cachefilter-patterns.h | 2 ++ doc/apt-patterns.7.xml | 2 +- test/libapt/pattern_test.cc | 1 + 4 files changed, 31 insertions(+), 2 deletions(-) (limited to 'test/libapt/pattern_test.cc') diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index 11ad5d723..9fab0281d 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -66,6 +66,32 @@ std::unique_ptr PatternTreeParser::parseTop() // Parse any pattern std::unique_ptr PatternTreeParser::parse() +{ + return parseUnary(); +} + +std::unique_ptr PatternTreeParser::parseUnary() +{ + + if (sentence[state.offset] != '!') + return parsePrimary(); + + auto start = ++state.offset; + auto primary = parsePrimary(); + + if (primary == nullptr) + throw Error{Node{start, sentence.size()}, "Expected pattern"}; + + auto node = std::make_unique(); + node->start = start; + node->end = primary->end; + node->term = "?not"; + node->arguments.push_back(std::move(primary)); + node->haveArgumentList = true; + return node; +} + +std::unique_ptr PatternTreeParser::parsePrimary() { std::unique_ptr node; if ((node = parseShortPattern()) != nullptr) @@ -198,7 +224,7 @@ std::unique_ptr PatternTreeParser::parseQuotedWord() // Parse a bare word atom std::unique_ptr PatternTreeParser::parseWord() { - static const constexpr auto DISALLOWED_START = "?~,()\0"_sv; + static const constexpr auto DISALLOWED_START = "!?~,()\0"_sv; static const constexpr auto DISALLOWED = ",()\0"_sv; if (DISALLOWED_START.find(sentence[state.offset]) != APT::StringView::npos) return nullptr; diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index 0d6e9d99e..76318eafa 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -95,6 +95,8 @@ struct PatternTreeParser private: std::unique_ptr parse(); + std::unique_ptr parseUnary(); + std::unique_ptr parsePrimary(); std::unique_ptr parsePattern(); std::unique_ptr parseShortPattern(); std::unique_ptr parseWord(); diff --git a/doc/apt-patterns.7.xml b/doc/apt-patterns.7.xml index f18fe6a19..72f1ccbce 100644 --- a/doc/apt-patterns.7.xml +++ b/doc/apt-patterns.7.xml @@ -49,7 +49,7 @@ ?false~F Selects nothing. - ?not(PATTERN) + ?not(PATTERN)!PATTERN Selects objects where PATTERN does not match. ?or(PATTERN, PATTERN, ...) diff --git a/test/libapt/pattern_test.cc b/test/libapt/pattern_test.cc index 492a29eac..39959cd31 100644 --- a/test/libapt/pattern_test.cc +++ b/test/libapt/pattern_test.cc @@ -158,4 +158,5 @@ TEST(TreeParserTest, ParseShortPattern) EXPECT_PATTERN_EQ_ATOMIC("~U", "?upgradable"); EXPECT_PATTERN_EQ("~Vverstr", "?version(verstr)"); EXPECT_PATTERN_EQ_ATOMIC("~v", "?virtual"); + EXPECT_PATTERN_EQ("!foo", "?not(foo)"); } -- cgit v1.2.3-70-g09d2 From ebe5f39bfbb64921d5d31e0a6e49287356a5e6e2 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 28 Jan 2020 23:06:08 +0100 Subject: patterns: Allow bare words only in arguments This changes the syntax from approximately expr = unary unary = '!'? primary primary = pattern | short-pattern | word | quoted-word pattern = '?' name [ '(' expr [',' expr]* ')' ] short-pattern = ~ name | ~name expr to: primary = pattern | short-pattern argument = word | quoted-word | expr pattern = '?' name [ '(' argument [',' argument]* ')' ] short-pattern = ~ name | ~name argument --- apt-pkg/cachefilter-patterns.cc | 18 +++++++++++++++--- apt-pkg/cachefilter-patterns.h | 1 + test/libapt/pattern_test.cc | 31 ++++++++++++++++++++++++------- 3 files changed, 40 insertions(+), 10 deletions(-) (limited to 'test/libapt/pattern_test.cc') diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index 9fab0281d..cf3e59ac6 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -58,6 +58,9 @@ std::unique_ptr PatternTreeParser::parseTop() auto node = parse(); skipSpace(); + if (node == nullptr) + throw Error{Node{0, sentence.size()}, "Expected pattern"}; + if (node->end != sentence.size()) throw Error{Node{node->end, sentence.size()}, "Expected end of file"}; @@ -98,10 +101,19 @@ std::unique_ptr PatternTreeParser::parsePrimary() return node; if ((node = parsePattern()) != nullptr) return node; + + return nullptr; +} + +std::unique_ptr PatternTreeParser::parseArgument() +{ + std::unique_ptr node; if ((node = parseQuotedWord()) != nullptr) return node; if ((node = parseWord()) != nullptr) return node; + if ((node = parse()) != nullptr) + return node; throw Error{Node{state.offset, sentence.size()}, "Expected pattern, quoted word, or word"}; @@ -125,7 +137,7 @@ std::unique_ptr PatternTreeParser::parseShortPattern() state.offset += sp.shortName.size() + 1; if (sp.takesArgument) { - node->arguments.push_back(parse()); + node->arguments.push_back(parseArgument()); node->haveArgumentList = true; } node->end = state.offset; @@ -173,7 +185,7 @@ std::unique_ptr PatternTreeParser::parsePattern() return node; } - node->arguments.push_back(parse()); + node->arguments.push_back(parseArgument()); skipSpace(); while (sentence[state.offset] == ',') { @@ -182,7 +194,7 @@ std::unique_ptr PatternTreeParser::parsePattern() // This was a trailing comma - allow it and break the loop if (sentence[state.offset] == ')') break; - node->arguments.push_back(parse()); + node->arguments.push_back(parseArgument()); skipSpace(); } diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index 76318eafa..1770c7307 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -99,6 +99,7 @@ struct PatternTreeParser std::unique_ptr parsePrimary(); std::unique_ptr parsePattern(); std::unique_ptr parseShortPattern(); + std::unique_ptr parseArgument(); std::unique_ptr parseWord(); std::unique_ptr parseQuotedWord(); }; diff --git a/test/libapt/pattern_test.cc b/test/libapt/pattern_test.cc index 39959cd31..7fc6a1f8f 100644 --- a/test/libapt/pattern_test.cc +++ b/test/libapt/pattern_test.cc @@ -16,19 +16,25 @@ using namespace APT::Internal; TEST(TreeParserTest, ParseWord) { - auto node = PatternTreeParser("word").parseTop(); - auto wordNode = dynamic_cast(node.get()); + auto node = PatternTreeParser("?word(word)").parseTop(); + auto patternNode = dynamic_cast(node.get()); + + ASSERT_EQ(patternNode->arguments.size(), 1u); + auto wordNode = dynamic_cast(patternNode->arguments[0].get()); - EXPECT_EQ(node.get(), wordNode); + EXPECT_EQ(patternNode->arguments[0].get(), wordNode); EXPECT_EQ(wordNode->word, "word"); } TEST(TreeParserTest, ParseQuotedWord) { - auto node = PatternTreeParser("\"a word\"").parseTop(); - auto wordNode = dynamic_cast(node.get()); + auto node = PatternTreeParser("?word(\"a word\")").parseTop(); + auto patternNode = dynamic_cast(node.get()); + + ASSERT_EQ(patternNode->arguments.size(), 1u); + auto wordNode = dynamic_cast(patternNode->arguments[0].get()); - EXPECT_EQ(node.get(), wordNode); + EXPECT_EQ(patternNode->arguments[0].get(), wordNode); EXPECT_EQ(wordNode->word, "a word"); } @@ -158,5 +164,16 @@ TEST(TreeParserTest, ParseShortPattern) EXPECT_PATTERN_EQ_ATOMIC("~U", "?upgradable"); EXPECT_PATTERN_EQ("~Vverstr", "?version(verstr)"); EXPECT_PATTERN_EQ_ATOMIC("~v", "?virtual"); - EXPECT_PATTERN_EQ("!foo", "?not(foo)"); + EXPECT_PATTERN_EQ("!?foo", "?not(?foo)"); + + caught = false; + try + { + PatternTreeParser("!x").parseTop(); + } + catch (PatternTreeParser::Error &e) + { + caught = true; + }; + EXPECT_TRUE(caught) << "!X should have thrown an exception"; } -- cgit v1.2.3-70-g09d2 From 250119362e44599aad7e75462fa4298ad1ab1ad9 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 28 Jan 2020 22:38:46 +0100 Subject: patterns: Parse sequence of patterns as ?and --- apt-pkg/cachefilter-patterns.cc | 32 +++++++++++++++++++++++++++++++- apt-pkg/cachefilter-patterns.h | 1 + doc/apt-patterns.7.xml | 2 +- test/libapt/pattern_test.cc | 8 ++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) (limited to 'test/libapt/pattern_test.cc') diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index cf3e59ac6..dbf58e2a9 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -70,7 +70,37 @@ std::unique_ptr PatternTreeParser::parseTop() // Parse any pattern std::unique_ptr PatternTreeParser::parse() { - return parseUnary(); + return parseAnd(); +} + +std::unique_ptr PatternTreeParser::parseAnd() +{ + auto start = state.offset; + std::vector> nodes; + + for (skipSpace(); state.offset < sentence.size(); skipSpace()) + { + auto node = parseUnary(); + + if (node == nullptr) + break; + + nodes.push_back(std::move(node)); + } + + if (nodes.size() == 0) + return nullptr; + if (nodes.size() == 1) + return std::move(nodes[0]); + + auto node = std::make_unique(); + node->start = start; + node->end = nodes[nodes.size() - 1]->end; + node->term = "?and"; + node->arguments = std::move(nodes); + node->haveArgumentList = true; + + return node; } std::unique_ptr PatternTreeParser::parseUnary() diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index 1770c7307..7f30a3ea0 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -95,6 +95,7 @@ struct PatternTreeParser private: std::unique_ptr parse(); + std::unique_ptr parseAnd(); std::unique_ptr parseUnary(); std::unique_ptr parsePrimary(); std::unique_ptr parsePattern(); diff --git a/doc/apt-patterns.7.xml b/doc/apt-patterns.7.xml index 72f1ccbce..3de9b00fc 100644 --- a/doc/apt-patterns.7.xml +++ b/doc/apt-patterns.7.xml @@ -43,7 +43,7 @@ patterns. - ?and(PATTERN, PATTERN, ...) + ?and(PATTERN, PATTERN, ...)PATTERN PATTERN ... Selects objects where all specified patterns match. ?false~F diff --git a/test/libapt/pattern_test.cc b/test/libapt/pattern_test.cc index 7fc6a1f8f..00d356d47 100644 --- a/test/libapt/pattern_test.cc +++ b/test/libapt/pattern_test.cc @@ -176,4 +176,12 @@ TEST(TreeParserTest, ParseShortPattern) caught = true; }; EXPECT_TRUE(caught) << "!X should have thrown an exception"; + + EXPECT_PATTERN_EQ("?a?b", "?and(?a, ?b)"); + EXPECT_PATTERN_EQ("~T~F", "?and(?true, ?false)"); + EXPECT_PATTERN_EQ("~T ~F", "?and(?true, ?false)"); + EXPECT_PATTERN_EQ("~T !~F", "?and(?true, ?not(?false))"); + EXPECT_PATTERN_EQ("!~F ~T", "?and(?not(?false), ?true)"); + EXPECT_PATTERN_EQ("!~F~T", "?and(?not(?false), ?true)"); + } -- cgit v1.2.3-70-g09d2 From d6f38436a229dc4421e77b58bf42d07bdb28b808 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 1 Feb 2020 17:12:35 +0100 Subject: Implement | as or --- apt-pkg/cachefilter-patterns.cc | 44 ++++++++++++++++++++++++++++++++++++++--- apt-pkg/cachefilter-patterns.h | 1 + test/libapt/pattern_test.cc | 3 +++ 3 files changed, 45 insertions(+), 3 deletions(-) (limited to 'test/libapt/pattern_test.cc') diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index dbe42b83f..8c0b35de2 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -70,7 +70,45 @@ std::unique_ptr PatternTreeParser::parseTop() // Parse any pattern std::unique_ptr PatternTreeParser::parse() { - return parseAnd(); + return parseOr(); +} + +std::unique_ptr PatternTreeParser::parseOr() +{ + auto start = state.offset; + std::vector> nodes; + + auto firstNode = parseAnd(); + + if (firstNode == nullptr) + return nullptr; + + nodes.push_back(std::move(firstNode)); + for (skipSpace(); sentence[state.offset] == '|'; skipSpace()) + { + state.offset++; + skipSpace(); + auto node = parseAnd(); + + if (node == nullptr) + throw Error{Node{state.offset, sentence.size()}, "Expected pattern after |"}; + + nodes.push_back(std::move(node)); + } + + if (nodes.size() == 0) + return nullptr; + if (nodes.size() == 1) + return std::move(nodes[0]); + + auto node = std::make_unique(); + node->start = start; + node->end = nodes[nodes.size() - 1]->end; + node->term = "?or"; + node->arguments = std::move(nodes); + node->haveArgumentList = true; + + return node; } std::unique_ptr PatternTreeParser::parseAnd() @@ -266,8 +304,8 @@ std::unique_ptr PatternTreeParser::parseQuotedWord() // Parse a bare word atom std::unique_ptr PatternTreeParser::parseWord() { - static const constexpr auto DISALLOWED_START = "!?~,()\0"_sv; - static const constexpr auto DISALLOWED = ",()\0"_sv; + static const constexpr auto DISALLOWED_START = "!?~|,()\0"_sv; + static const constexpr auto DISALLOWED = "|,()\0"_sv; if (DISALLOWED_START.find(sentence[state.offset]) != APT::StringView::npos) return nullptr; diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index 7f30a3ea0..c6e701880 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -95,6 +95,7 @@ struct PatternTreeParser private: std::unique_ptr parse(); + std::unique_ptr parseOr(); std::unique_ptr parseAnd(); std::unique_ptr parseUnary(); std::unique_ptr parsePrimary(); diff --git a/test/libapt/pattern_test.cc b/test/libapt/pattern_test.cc index 00d356d47..d8d962758 100644 --- a/test/libapt/pattern_test.cc +++ b/test/libapt/pattern_test.cc @@ -184,4 +184,7 @@ TEST(TreeParserTest, ParseShortPattern) EXPECT_PATTERN_EQ("!~F ~T", "?and(?not(?false), ?true)"); EXPECT_PATTERN_EQ("!~F~T", "?and(?not(?false), ?true)"); + EXPECT_PATTERN_EQ("!~F~T | ~T", "?or(?and(?not(?false), ?true), ?true)"); + EXPECT_PATTERN_EQ("~ramd64|~rall", "?or(?architecture(amd64), ?architecture(all))"); + } -- cgit v1.2.3-70-g09d2 From 8886ea163032fb8bf64211a94c5dc252a4572a9c Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 1 Feb 2020 17:21:40 +0100 Subject: patterns: Implement parsing of (...) groups --- apt-pkg/cachefilter-patterns.cc | 26 ++++++++++++++++++++++++++ apt-pkg/cachefilter-patterns.h | 1 + test/libapt/pattern_test.cc | 4 ++++ 3 files changed, 31 insertions(+) (limited to 'test/libapt/pattern_test.cc') diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index 8c0b35de2..c6875d995 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -169,10 +169,36 @@ std::unique_ptr PatternTreeParser::parsePrimary() return node; if ((node = parsePattern()) != nullptr) return node; + if ((node = parseGroup()) != nullptr) + return node; return nullptr; } +std::unique_ptr PatternTreeParser::parseGroup() +{ + if (sentence[state.offset] != '(') + return nullptr; + + auto start = state.offset++; + + skipSpace(); + auto node = parse(); + if (node == nullptr) + throw Error{Node{state.offset, sentence.size()}, + "Expected pattern after '('"}; + skipSpace(); + + if (sentence[state.offset] != ')') + throw Error{Node{state.offset, sentence.size()}, + "Expected closing parenthesis"}; + + auto end = ++state.offset; + node->start = start; + node->end = end; + return node; +} + std::unique_ptr PatternTreeParser::parseArgument() { std::unique_ptr node; diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index c6e701880..1b7e70da5 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -99,6 +99,7 @@ struct PatternTreeParser std::unique_ptr parseAnd(); std::unique_ptr parseUnary(); std::unique_ptr parsePrimary(); + std::unique_ptr parseGroup(); std::unique_ptr parsePattern(); std::unique_ptr parseShortPattern(); std::unique_ptr parseArgument(); diff --git a/test/libapt/pattern_test.cc b/test/libapt/pattern_test.cc index d8d962758..ca77959e3 100644 --- a/test/libapt/pattern_test.cc +++ b/test/libapt/pattern_test.cc @@ -187,4 +187,8 @@ TEST(TreeParserTest, ParseShortPattern) EXPECT_PATTERN_EQ("!~F~T | ~T", "?or(?and(?not(?false), ?true), ?true)"); EXPECT_PATTERN_EQ("~ramd64|~rall", "?or(?architecture(amd64), ?architecture(all))"); + EXPECT_PATTERN_EQ("(?A|?B)?C", "?and(?or(?A, ?B), ?C)"); + EXPECT_PATTERN_EQ("?A|?B?C", "?or(?A, ?and(?B, ?C))"); + EXPECT_PATTERN_EQ("?A|(?B?C)", "?or(?A, ?and(?B, ?C))"); + EXPECT_PATTERN_EQ("(?B?C)|?A", "?or(?and(?B, ?C), ?A)"); } -- cgit v1.2.3-70-g09d2 From 11a40ab11f72f85e905bdba4d3274870fbcaeaee Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 1 Feb 2020 17:33:08 +0100 Subject: Correctly stop parsing short form arguments on space, also on ? we have to stop parsing on space so that things like ~ramd64 | ~rall work correctly. aptitude does not stop parsing on ?, but we'll do as it gets very confusing otherwise if you write stuff like ~ramd64?name(foo), and it resolves to ?and(?architecture(amd64?name), (foo))... --- apt-pkg/cachefilter-patterns.cc | 19 +++++++++++-------- apt-pkg/cachefilter-patterns.h | 4 ++-- test/libapt/pattern_test.cc | 2 ++ 3 files changed, 15 insertions(+), 10 deletions(-) (limited to 'test/libapt/pattern_test.cc') diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index c6875d995..1c92a7b1f 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -199,12 +199,12 @@ std::unique_ptr PatternTreeParser::parseGroup() return node; } -std::unique_ptr PatternTreeParser::parseArgument() +std::unique_ptr PatternTreeParser::parseArgument(bool shrt) { std::unique_ptr node; if ((node = parseQuotedWord()) != nullptr) return node; - if ((node = parseWord()) != nullptr) + if ((node = parseWord(shrt)) != nullptr) return node; if ((node = parse()) != nullptr) return node; @@ -231,7 +231,7 @@ std::unique_ptr PatternTreeParser::parseShortPattern() state.offset += sp.shortName.size() + 1; if (sp.takesArgument) { - node->arguments.push_back(parseArgument()); + node->arguments.push_back(parseArgument(true)); node->haveArgumentList = true; } node->end = state.offset; @@ -279,7 +279,7 @@ std::unique_ptr PatternTreeParser::parsePattern() return node; } - node->arguments.push_back(parseArgument()); + node->arguments.push_back(parseArgument(false)); skipSpace(); while (sentence[state.offset] == ',') { @@ -288,7 +288,7 @@ std::unique_ptr PatternTreeParser::parsePattern() // This was a trailing comma - allow it and break the loop if (sentence[state.offset] == ')') break; - node->arguments.push_back(parseArgument()); + node->arguments.push_back(parseArgument(false)); skipSpace(); } @@ -328,10 +328,13 @@ std::unique_ptr PatternTreeParser::parseQuotedWord() } // Parse a bare word atom -std::unique_ptr PatternTreeParser::parseWord() +std::unique_ptr PatternTreeParser::parseWord(bool shrt) { - static const constexpr auto DISALLOWED_START = "!?~|,()\0"_sv; - static const constexpr auto DISALLOWED = "|,()\0"_sv; + static const constexpr auto DISALLOWED_START = "!?~|,() \0"_sv; + static const constexpr auto DISALLOWED_LONG = "|,()\0"_sv; + static const constexpr auto DISALLOWED_SHRT = "|,() ?\0"_sv; + const auto DISALLOWED = shrt ? DISALLOWED_SHRT : DISALLOWED_LONG; + if (DISALLOWED_START.find(sentence[state.offset]) != APT::StringView::npos) return nullptr; diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index 1b7e70da5..e79702af8 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -102,8 +102,8 @@ struct PatternTreeParser std::unique_ptr parseGroup(); std::unique_ptr parsePattern(); std::unique_ptr parseShortPattern(); - std::unique_ptr parseArgument(); - std::unique_ptr parseWord(); + std::unique_ptr parseArgument(bool shrt); + std::unique_ptr parseWord(bool shrt); std::unique_ptr parseQuotedWord(); }; diff --git a/test/libapt/pattern_test.cc b/test/libapt/pattern_test.cc index ca77959e3..84d09351c 100644 --- a/test/libapt/pattern_test.cc +++ b/test/libapt/pattern_test.cc @@ -186,6 +186,8 @@ TEST(TreeParserTest, ParseShortPattern) EXPECT_PATTERN_EQ("!~F~T | ~T", "?or(?and(?not(?false), ?true), ?true)"); EXPECT_PATTERN_EQ("~ramd64|~rall", "?or(?architecture(amd64), ?architecture(all))"); + EXPECT_PATTERN_EQ("~ramd64 | ~rall", "?or(?architecture(amd64), ?architecture(all))"); + EXPECT_PATTERN_EQ("~ramd64?name(foo)", "?and(?architecture(amd64), ?name(foo))"); EXPECT_PATTERN_EQ("(?A|?B)?C", "?and(?or(?A, ?B), ?C)"); EXPECT_PATTERN_EQ("?A|?B?C", "?or(?A, ?and(?B, ?C))"); -- cgit v1.2.3-70-g09d2 From 404771d0ec11f26a0b631018719e2918a049455b Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 3 Feb 2020 12:15:07 +0100 Subject: patterns: test for empty terms, reject them --- apt-pkg/cachefilter-patterns.cc | 3 +++ apt-pkg/cachefilter-patterns.h | 2 +- test/libapt/pattern_test.cc | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) (limited to 'test/libapt/pattern_test.cc') diff --git a/apt-pkg/cachefilter-patterns.cc b/apt-pkg/cachefilter-patterns.cc index 1c92a7b1f..5a58a9767 100644 --- a/apt-pkg/cachefilter-patterns.cc +++ b/apt-pkg/cachefilter-patterns.cc @@ -263,6 +263,9 @@ std::unique_ptr PatternTreeParser::parsePattern() node->term = sentence.substr(node->start, state.offset - node->start); + if (node->term.size() <= 1) + throw Error{*node, "Pattern must have a term/name"}; + node->end = skipSpace(); // We don't have any arguments, return node; if (sentence[state.offset] != '(') diff --git a/apt-pkg/cachefilter-patterns.h b/apt-pkg/cachefilter-patterns.h index e79702af8..4eeb68594 100644 --- a/apt-pkg/cachefilter-patterns.h +++ b/apt-pkg/cachefilter-patterns.h @@ -92,9 +92,9 @@ struct PatternTreeParser /// There may not be anything before or after the pattern, except for /// whitespace. std::unique_ptr parseTop(); + std::unique_ptr parse(); // public for test cases only private: - std::unique_ptr parse(); std::unique_ptr parseOr(); std::unique_ptr parseAnd(); std::unique_ptr parseUnary(); diff --git a/test/libapt/pattern_test.cc b/test/libapt/pattern_test.cc index 84d09351c..bfcaf2093 100644 --- a/test/libapt/pattern_test.cc +++ b/test/libapt/pattern_test.cc @@ -14,6 +14,32 @@ using namespace APT::Internal; +#define EXPECT_EXCEPTION(exp, exc, msg) \ + caught = false; \ + try \ + { \ + exp; \ + } \ + catch (exc & e) \ + { \ + caught = true; \ + EXPECT_TRUE(e.message.find(msg) != std::string::npos) << msg << " not in " << e.message; \ + }; \ + EXPECT_TRUE(caught) << #exp "should have thrown an exception" + +TEST(TreeParserTest, ParseInvalid) +{ + bool caught = false; + + // Not a valid pattern: Reject + EXPECT_EXCEPTION(PatternTreeParser("?").parse(), PatternTreeParser::Error, "Pattern must have a term"); + EXPECT_EXCEPTION(PatternTreeParser("?AB?").parse(), PatternTreeParser::Error, "Pattern must have a term"); + EXPECT_EXCEPTION(PatternTreeParser("~").parse(), PatternTreeParser::Error, "Unknown short pattern"); + + // Not a pattern at all: Report nullptr + EXPECT_EQ(PatternTreeParser("A?").parse(), nullptr); +} + TEST(TreeParserTest, ParseWord) { auto node = PatternTreeParser("?word(word)").parseTop(); -- cgit v1.2.3-70-g09d2