Skip to content

Commit 47a8df6

Browse files
authored
Merge pull request #9420 from imaqsood/PUP-11515
(PUP-11515) Negative Lookbehind Regex Causes Duplicate Node
2 parents bda54a7 + 5d09d7f commit 47a8df6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Diff for: lib/puppet/resource/type.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ class Puppet::Resource::Type
3333
DOUBLE_COLON = '::'
3434
EMPTY_ARRAY = [].freeze
3535

36+
LOOKAROUND_OPERATORS = {
37+
"(" => 'LP',
38+
"?" => "QU",
39+
"<" => "LT",
40+
">" => "GT",
41+
"!" => "EX",
42+
"=" => "EQ",
43+
")" => 'RP'
44+
}.freeze
45+
3646
attr_accessor :file, :line, :doc, :code, :parent, :resource_type_collection, :override
3747
attr_reader :namespace, :arguments, :behaves_like, :module_name
3848

@@ -196,7 +206,11 @@ def instantiate_resource(scope, resource)
196206

197207
def name
198208
if type == :node && name_is_regex?
199-
"__node_regexp__#{@name.source.downcase.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}"
209+
# Normalize lookarround regex patthern
210+
internal_name = @name.source.downcase.gsub(/\(\?[^)]*\)/) do |str|
211+
str.gsub(/./) { |ch| LOOKAROUND_OPERATORS[ch] || ch }
212+
end
213+
"__node_regexp__#{internal_name.gsub(/[^-\w:.]/, '').sub(/^\.+/, '')}"
200214
else
201215
@name
202216
end

Diff for: spec/integration/parser/node_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ class foo {
8585
end.not_to raise_error
8686
end
8787

88+
it 'does not raise an error with 2 regex node names are the same due to lookarround pattern' do
89+
expect do
90+
compile_to_catalog(<<-MANIFEST, Puppet::Node.new("async"))
91+
node /(?<!a)sync/ { }
92+
node /async/ { }
93+
MANIFEST
94+
end.not_to raise_error
95+
end
96+
8897
it 'errors when two nodes with regexes collide after some regex syntax is removed' do
8998
expect do
9099
compile_to_catalog(<<-MANIFEST)

0 commit comments

Comments
 (0)