Skip to content

Commit

Permalink
Fix type 4 function Copy() stack operator and fix #724
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Nov 12, 2023
1 parent 62bc32a commit 1bc0ee7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/UglyToad.PdfPig.Tests/Integration/ColorSpaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ public void SeparationIccColorSpacesWithForm()
}
}

[Fact]
public void Issue724()
{
// 11194059_2017-11_de_s
var path = IntegrationHelpers.GetDocumentPath("11194059_2017-11_de_s.pdf");
using (var document = PdfDocument.Open(path))
{
// Should not throw an exception.
// Fixed an issue in the Type 4 function Copy() StackOperators
Page page1 = document.GetPage(1);
Assert.NotNull(page1);

Page page2 = document.GetPage(2);
Assert.NotNull(page2);
}
}

private static byte ConvertToByte(double componentValue)
{
var rounded = Math.Round(componentValue * 255, MidpointRounding.AwayFromZero);
Expand Down
Binary file not shown.
16 changes: 12 additions & 4 deletions src/UglyToad.PdfPig/Functions/Type4/StackOperators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ internal sealed class Copy : Operator
{
public void Execute(ExecutionContext context)
{
int n = ((int)context.Stack.Pop());
// Duplicate top n stack items
int n = (int)context.Stack.Pop();
if (n > 0)
{
int size = context.Stack.Count;
// Need to copy to a new list to avoid ConcurrentModificationException
List<object> copy = context.Stack.ToList().GetRange(size - n - 1, n);
context.AddAllToStack(copy);
context.AddAllToStack(context.Stack.ToList().Take(n));

/* For reference, the PdfBox code:
* https://github.com/apache/pdfbox/blob/18a1931bae5b8c1766cd4976d0fb0e28649190b5/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/type4/StackOperators.java#L41C1-L50C14
* int size = stack.size();
//Need to copy to a new list to avoid ConcurrentModificationException
List<Object> copy = new java.util.ArrayList<>(
stack.subList(size - n, size));
stack.addAll(copy);
*/
}
}
}
Expand Down

0 comments on commit 1bc0ee7

Please sign in to comment.