Martin Mirchev created PDFBOX-6331:
--------------------------------------

             Summary: Type 4 functions: integer "not", "roll" with |j| > n and 
"gt"/"ge" on large integers give wrong results
                 Key: PDFBOX-6331
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6331
             Project: PDFBox
          Issue Type: Bug
          Components: Rendering
    Affects Versions: 4.0.0, 3.0.8 PDFBox
            Reporter: Martin Mirchev
         Attachments: t4-gt.pdf, t4-not.pdf, t4-roll.pdf

Three PostScript calculator operators in 
{{org.apache.pdfbox.pdmodel.common.function.type4}} differ from the PostScript 
Language Reference that ISO 32000 refers to:

* {{not}} on an integer pushes {{-x}} instead of the bitwise complement {{~x}} 
({{BitwiseOperators.Not}}): {{0 not}} gives 0 instead of -1, {{5 not}} gives -5 
instead of -6.
* {{roll}} uses {{j}} as is instead of {{j mod n}} ({{StackOperators.Roll}}). 
With {{|j| > n}} it pops more than {{n}} operands: {{9 1 2 3 3 4 roll}} leaves 
{{9 1 2 3}} instead of {{9 3 1 2}}, and {{1 2 3 3 4 roll}} underflows the stack.
* {{gt}} and {{ge}} compare integers as floats ({{RelationalOperators}}), so 
integers above 2^24 can compare wrongly: {{16777217 16777216 gt}} is false. The 
other comparison operators convert the same way, so the change below uses 
{{doubleValue()}} in all of them.

Attached are three pages with an axial shading whose type 4 function returns 0 
(black) when the operator behaves as specified and 1 (white) otherwise: 
{{t4-not.pdf}} ({{0 not -1 eq}}) and {{t4-gt.pdf}} render white; 
{{t4-roll.pdf}} ({{1 2 3 3 4 roll}}) fails with an EmptyStackException, which 
since r1940489 (PDFBOX-6320) becomes an IOException, so the shading isn't 
drawn. Each result was the same in 3 runs.

A possible change; with it, all three pages render black, and rolls with {{|j| 
<= n}} give the same results as before:

{code}
--- 
a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/type4/BitwiseOperators.java
+++ 
b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/type4/BitwiseOperators.java
@@ -135,7 +135,7 @@
             else if (op1 instanceof Integer)
             {
                 int int1 = (Integer)op1;
-                int result = -int1;
+                int result = ~int1;
                 stack.push(result);
             }
             else
--- 
a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/type4/StackOperators.java
+++ 
b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/type4/StackOperators.java
@@ -126,6 +126,16 @@
             {
                 throw new IllegalArgumentException("rangecheck: " + n);
             }
+            if (n == 0)
+            {
+                return;
+            }
+            // rolling by j is the same as rolling by j mod n
+            j %= n;
+            if (j == 0)
+            {
+                return;
+            }
 
             LinkedList<Object> rolled = new LinkedList<>();
             LinkedList<Object> moved = new LinkedList<>();
--- 
a/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/type4/RelationalOperators.java
+++ 
b/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/common/function/type4/RelationalOperators.java
@@ -50,7 +50,7 @@
             {
                 Number num1 = (Number)op1;
                 Number num2 = (Number)op2;
-                result = Float.compare(num1.floatValue(),num2.floatValue()) == 
0;
+                result = Double.compare(num1.doubleValue(), 
num2.doubleValue()) == 0;
             }
             else
             {
@@ -87,7 +87,7 @@
         @Override
         protected boolean compare(Number num1, Number num2)
         {
-            return num1.floatValue() >= num2.floatValue();
+            return num1.doubleValue() >= num2.doubleValue();
         }
 
     }
@@ -99,7 +99,7 @@
         @Override
         protected boolean compare(Number num1, Number num2)
         {
-            return num1.floatValue() > num2.floatValue();
+            return num1.doubleValue() > num2.doubleValue();
         }
 
     }
@@ -111,7 +111,7 @@
         @Override
         protected boolean compare(Number num1, Number num2)
         {
-            return num1.floatValue() <= num2.floatValue();
+            return num1.doubleValue() <= num2.doubleValue();
         }
 
     }
@@ -123,7 +123,7 @@
         @Override
         protected boolean compare(Number num1, Number num2)
         {
-            return num1.floatValue() < num2.floatValue();
+            return num1.doubleValue() < num2.doubleValue();
         }
 
     }
{code}

This issue was found by an LLM-assisted tool and verified manually.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to