Do not throw error on Pop when stack size is 1 in lenient mode and fix #973

This commit is contained in:
BobLd 2025-01-19 11:00:43 +00:00
parent 92d3439465
commit c4576e4ffa
5 changed files with 674 additions and 13 deletions

View File

@ -14,7 +14,7 @@
Assert.Equal("Q", Pop.Value.Operator); 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() public void CannotPopWithSingleFrame()
{ {
Action action = () => Pop.Value.Run(context); Action action = () => Pop.Value.Run(context);

View File

@ -7,11 +7,35 @@
public class GithubIssuesTests public class GithubIssuesTests
{ {
[Fact]
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] [Fact]
public void Issue959() public void Issue959()
{ {
// Lenient parsing ON
var path = IntegrationHelpers.GetSpecificTestDocumentPath("algo.pdf"); var path = IntegrationHelpers.GetSpecificTestDocumentPath("algo.pdf");
// Lenient parsing ON
using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true })) using (var document = PdfDocument.Open(path, new ParsingOptions() { UseLenientParsing = true }))
{ {
for (int i = 1; i <= document.NumberOfPages; ++i) for (int i = 1; i <= document.NumberOfPages; ++i)

File diff suppressed because one or more lines are too long

View File

@ -195,7 +195,21 @@
/// <inheritdoc/> /// <inheritdoc/>
public virtual void PopState() 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; ActiveExtendedGraphicsStateFont = null;
} }

View File

@ -1,6 +1,5 @@
namespace UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState namespace UglyToad.PdfPig.Graphics.Operations.SpecialGraphicsState
{ {
using System;
using System.IO; using System.IO;
/// <inheritdoc /> /// <inheritdoc />
@ -29,15 +28,7 @@
/// <inheritdoc /> /// <inheritdoc />
public void Run(IOperationContext operationContext) public void Run(IOperationContext operationContext)
{ {
var currentStackSize = operationContext.StackSize; operationContext.PopState();
if (currentStackSize > 1)
{
operationContext.PopState();
}
else
{
throw new InvalidOperationException("Cannot execute a pop of the graphics state stack, it would leave the stack empty.");
}
} }
/// <inheritdoc /> /// <inheritdoc />