Skip to content

Commit

Permalink
Fix AcroForm::Form#recalculate_fields to only consider real fields
Browse files Browse the repository at this point in the history
Some invalid files contain entries in the /CO array which are invalid,
e.g. nil, numbers, arrays or random dictionaries. Handle this situation
by making sure that only fields are processed.
  • Loading branch information
gettalong committed Dec 3, 2024
1 parent 5557736 commit 1ee7a70
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Offsets and lengths of revisions shown using the `inspect rev` CLI command for
linearized PDF files
* [HexaPDF::Type::AcroForm::Form#recalculate_fields] to only consider real
fields


## 1.0.1 - 2024-11-05
Expand Down
2 changes: 1 addition & 1 deletion lib/hexapdf/type/acro_form/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ def flatten(fields: nil, create_appearances: true)
#
# See: JavaScriptActions
def recalculate_fields
self[:CO]&.each do |field|
(each_field.to_a & self[:CO].to_a).each do |field|
field = Field.wrap(document, field)
next unless field && (calculation_action = field[:AA]&.[](:C))
result = JavaScriptActions.calculate(self, calculation_action)
Expand Down
8 changes: 8 additions & 0 deletions test/hexapdf/type/acro_form/test_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,14 @@ def applies_variable_text_properties(method, **args)
@acro_form.recalculate_fields
assert_equal("10", @text3.field_value)
end

it "ensures that only entries in /CO that are actually fields are used" do
@text1.field_value = "10"
@text3.set_calculate_action(:sfn, fields: 'text1')
@acro_form[:CO] = [nil, 5, [:some, :array], @doc.pages.root, @text3]
@acro_form.recalculate_fields
assert_equal("10", @text3.field_value)
end
end

describe "perform_validation" do
Expand Down

0 comments on commit 1ee7a70

Please sign in to comment.