Skip to content

Commit

Permalink
Do not throw error on Pop when stack size is 1 in lenient mode and fix
Browse files Browse the repository at this point in the history
  • Loading branch information
BobLd committed Jan 19, 2025
1 parent 92d3439 commit c4576e4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void PopSymbolCorrect()
Assert.Equal("Q", Pop.Value.Operator);
}

[Fact]
[Fact(Skip = "The stack size check has been moved out of the Pop Operation, and is now in BaseStreamProcessor.PopState().")]
public void CannotPopWithSingleFrame()
{
Action action = () => Pop.Value.Run(context);
Expand Down
26 changes: 25 additions & 1 deletion src/UglyToad.PdfPig.Tests/Integration/GithubIssuesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,34 @@
public class GithubIssuesTests
{
[Fact]
public void Issue959()
public void Issue973()
{
var path = IntegrationHelpers.GetSpecificTestDocumentPath("JD5008.pdf");

// Lenient parsing ON
using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true }))
{
var page = document.GetPage(2);
Assert.NotNull(page);
Assert.Equal(2, page.Number);
Assert.NotEmpty(page.Letters);
}

// Lenient parsing OFF
using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = false }))
{
var exception = Assert.Throws<InvalidOperationException>(() => document.GetPage(2));
Assert.Equal("Cannot execute a pop of the graphics state stack, it would leave the stack empty.", exception.Message);
}
}


[Fact]
public void Issue959()
{
var path = IntegrationHelpers.GetSpecificTestDocumentPath("algo.pdf");

// Lenient parsing ON
using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true }))
{
for (int i = 1; i <= document.NumberOfPages; ++i)
Expand Down
Binary file not shown.
16 changes: 15 additions & 1 deletion src/UglyToad.PdfPig/Graphics/BaseStreamProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,21 @@ public CurrentGraphicsState GetCurrentState()
/// <inheritdoc/>
public virtual void PopState()
{
GraphicsStack.Pop();
if (StackSize > 1)
{
GraphicsStack.Pop();
}
else
{
const string error = "Cannot execute a pop of the graphics state stack, it would leave the stack empty.";
ParsingOptions.Logger.Error(error);

if (!ParsingOptions.UseLenientParsing)
{
throw new InvalidOperationException(error);
}
}

ActiveExtendedGraphicsStateFont = null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState
{
using System;
using System.IO;

/// <inheritdoc />
Expand Down Expand Up @@ -29,15 +28,7 @@ private Pop()
/// <inheritdoc />
public void Run(IOperationContext operationContext)
{
var currentStackSize = operationContext.StackSize;
if (currentStackSize > 1)
{
operationContext.PopState();
}
else
{
throw new InvalidOperationException("Cannot execute a pop of the graphics state stack, it would leave the stack empty.");
}
operationContext.PopState();
}

/// <inheritdoc />
Expand Down

0 comments on commit c4576e4

Please sign in to comment.