fix infinite loop

This commit is contained in:
Pete Matsyburka
2026-08-10 06:01:20 +03:00
parent b9018fa442
commit 8e254b7d3c
2 changed files with 31 additions and 1 deletions
+28
View File
@@ -110,4 +110,32 @@ module HexaPDF
end
end
end
module CycleSafeInheritedValue
def inherited_value(field, name)
seen = Set.new.compare_by_identity
seen << field.value
while field.value[name].nil? && (parent = field[:Parent]) && seen.add?(parent.value)
field = parent
end
field.value[name].nil? ? nil : field[name]
end
end
module CycleSafeFullFieldName
def full_field_name(seen = Set.new.compare_by_identity)
return field_name unless seen.add?(value)
if key?(:Parent)
[self[:Parent].full_field_name(seen), field_name].compact.join('.')
else
field_name
end
end
end
end
HexaPDF::Type::AcroForm::Field.singleton_class.prepend(HexaPDF::CycleSafeInheritedValue)
HexaPDF::Type::AcroForm::Field.prepend(HexaPDF::CycleSafeFullFieldName)
+3 -1
View File
@@ -251,7 +251,9 @@ module Templates
fields_index[annot.hash] ||= HexaPDF::Type::AcroForm::Field.wrap(pdf, annot)
elsif annot.key?(:Parent)
field = annot[:Parent]
field = field[:Parent] while field[:Parent]
seen = Set.new.compare_by_identity
field = field[:Parent] while field[:Parent] && seen.add?(field.value)
annots_index[field.hash] ||= page
fields_index[field.hash] ||= HexaPDF::Type::AcroForm::Field.wrap(pdf, field)