Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/main/java/org/verapdf/cos/COSArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,51 @@ private COSObject _at(final int i) {
return this.entries.get(i);
}

@Override
public boolean isEquivalentTo(Object o) {
if (this == o) return true;
if (o == null) return false;

if (o instanceof COSObject) {
return this.isEquivalentTo(((COSObject) o).get());
}
if (!(o instanceof COSArray)) return false;
List<COSBasePair> checkedObjects = new LinkedList<>();
return isEquivalentTo(o, checkedObjects);
}

@Override
boolean isEquivalentTo(Object o, List<COSBasePair> checkedObjects) {
if (this == o) return true;
if (o == null) return false;

if (o instanceof COSObject) {
return this.isEquivalentTo(((COSObject) o).get(), checkedObjects);
}
if (!(o instanceof COSArray)) return false;

COSArray that = (COSArray) o;
if (COSBasePair.listContainsPair(checkedObjects, this, that)) {
return true;
}
COSBasePair.addPairToList(checkedObjects, this, that);

if (this.size() != that.size()) return false;

for (int i = 0; i < this.size(); ++i) {
COSBase thisElem = this.at(i).getDirectBase();
COSBase thatElem = that.at(i).getDirectBase();

if (thisElem == null && thatElem == null) continue;
if (thisElem == null || thatElem == null) return false;

if (!thisElem.isEquivalentTo(thatElem, checkedObjects)) {
return false;
}
}
return true;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/verapdf/cos/COSBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ public void setObjectKey(COSKey indirectKey) {

public abstract void mark();

public abstract boolean isEquivalentTo(Object obj);

boolean isEquivalentTo(Object o, List<COSBasePair> checkedObjects) {
return isEquivalentTo(o);
}
Comment on lines +145 to +147

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🔴 Critical | 🏗️ Heavy lift

Preserve cycle-detection state across indirect dispatch.

The list-aware recursive callers pass checkedObjects, but the default and indirect implementations fall back to one-argument methods and restart traversal.

  • src/main/java/org/verapdf/cos/COSBase.java#L145-L147: do not discard the supplied list for recursive types.
  • src/main/java/org/verapdf/cos/COSIndirect.java#L483-L486: propagate the list when comparing indirect targets.
  • src/main/java/org/verapdf/cos/COSIndirect.java#L492-L497: propagate the list when comparing an indirect target with a COSObject.
📍 Affects 2 files
  • src/main/java/org/verapdf/cos/COSBase.java#L145-L147 (this comment)
  • src/main/java/org/verapdf/cos/COSIndirect.java#L483-L486
  • src/main/java/org/verapdf/cos/COSIndirect.java#L492-L497
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/org/verapdf/cos/COSBase.java` around lines 145 - 147, Preserve
the supplied cycle-detection state during equivalent comparisons: update
COSBase.isEquivalentTo(Object, List<COSBasePair>) to dispatch recursive
comparisons with the existing checkedObjects list, and update both COSIndirect
comparison paths at src/main/java/org/verapdf/cos/COSIndirect.java lines 483-486
and 492-497 to pass that same list when comparing indirect targets, including
COSObject targets; do not restart traversal through one-argument methods.


boolean equals(Object obj, List<COSBasePair> checkedObjects) {
return this.equals(obj);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/org/verapdf/cos/COSBoolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ public boolean setBoolean(final boolean value) {
return true;
}

@Override
public boolean isEquivalentTo(Object o) {
if (this == o) return true;
if (!(o instanceof COSBoolean)) return false;

COSBoolean that = (COSBoolean) o;

return value == that.value;

}

public boolean get() {
return this.value;
}
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/org/verapdf/cos/COSDictionary.java
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,70 @@ public Collection<COSObject> getValues() {
return this.entries.values();
}

private Set<ASAtom> getNonNullKeySet() {
Set<ASAtom> nonNullKeys = new HashSet<>();
for (ASAtom key : getKeySet()) {
COSBase value = getKey(key).get(); // assume getKey returns a COSBase wrapper
if (value != null) {
nonNullKeys.add(key);
}
}
return nonNullKeys;
}

@Override
public boolean isEquivalentTo(Object o) {
if (this == o) return true;
if (o == null) return false;
if (o instanceof COSObject) {
return this.isEquivalentTo(((COSObject) o).get());
}
if (!(o instanceof COSDictionary)) return false;

List<COSBasePair> checkedObjects = new LinkedList<>();
return isEquivalentTo(o, checkedObjects);
}

/**
* Internal recursive comparison with cycle detection.
*/
boolean isEquivalentTo(Object o, List<COSBasePair> checkedObjects) {
if (this == o) return true;
if (o == null) return false;
if (o instanceof COSObject) {
return this.isEquivalentTo(((COSObject) o).get(), checkedObjects);
}
if (!(o instanceof COSDictionary)) return false;
Comment on lines +313 to +335

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Confirm COSStream's superclass
rg -n 'class COSStream' -A2 --type=java

Repository: veraPDF/veraPDF-parser

Length of output: 160


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'Java files named COS*.java:\n'
fd '^COS.*\.java$' . || true

printf '\nSearch symbols:\n'
rg -n 'class COSStream|COSClassCOSStream|class COSDictionary|isEquivalentTo|function equivalent|byteContent|COSStream' -S --type=java . || true

Repository: veraPDF/veraPDF-parser

Length of output: 25872


🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'COSDictionary isEquivalentTo and equals:\n'
sed -n '310,420p' src/main/java/org/verapdf/cos/COSDictionary.java | cat -n

printf '\nCOSStream isEquivalentTo and equals:\n'
sed -n '290,415p' src/main/java/org/verapdf/cos/COSStream.java | cat -n

printf '\nCOSDirect and COSBase relevant data members:\n'
sed -n '1,120p' src/main/java/org/verapdf/cos/COSDirect.java | cat -n
sed -n '260,315p' src/main/java/org/verapdf/cos/COSDirect.java | cat -n

Repository: veraPDF/veraPDF-parser

Length of output: 15896


Reject COSStream in COSDictionary.isEquivalentTo().

COSStream extends COSDictionary, so the current instanceof COSDictionary gate lets a plain dictionary pass equality with a stream whenever their key/values match, ignoring the stream data that COSStream.isEquivalentTo() compares. Use an explicit cross-type boundary, e.g. if (o.getClass() != COSDictionary.class && !(o instanceof COSObject)) return false;.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/org/verapdf/cos/COSDictionary.java` around lines 313 - 335,
Update both overloads of COSDictionary.isEquivalentTo, including the recursive
variant, to reject COSStream and any other COSDictionary subclass before
comparing entries; preserve COSObject unwrapping and allow only exact
COSDictionary instances through the dictionary comparison path.


COSDictionary that = (COSDictionary) o;

if (COSBasePair.listContainsPair(checkedObjects, this, that)) {
return true;
}
COSBasePair.addPairToList(checkedObjects, this, that);

Set<ASAtom> thisKeys = this.getNonNullKeySet();
Set<ASAtom> thatKeys = that.getNonNullKeySet();

if (!thisKeys.equals(thatKeys)) {
return false;
}

for (ASAtom key : thisKeys) {
COSBase thisVal = this.getKey(key).get();
COSBase thatVal = that.getKey(key).get();

if (thisVal == null && thatVal == null) continue;
if (thisVal == null || thatVal == null) return false;

if (!thisVal.isEquivalentTo(thatVal, checkedObjects)) {
return false;
}
}

return true;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/verapdf/cos/COSIndirect.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,22 @@ public void mark() {
}
}

@Override
public boolean isEquivalentTo(Object o) {
if (this == o) return true;

if (o instanceof COSIndirect) {
COSIndirect that = (COSIndirect) o;
return this.getDirect().isEquivalentTo(that.getDirect());
}

if (o instanceof COSObject) {
return this.getDirect().isEquivalentTo(o);
}

return false;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/verapdf/cos/COSInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.verapdf.cos.visitor.ICOSVisitor;
import org.verapdf.cos.visitor.IVisitor;

import java.math.BigDecimal;

/**
* @author Timur Kamalov
*/
Expand Down Expand Up @@ -75,6 +77,22 @@ public boolean setReal(final double value) {
return true;
}

@Override
public boolean isEquivalentTo(Object o) {
if (this == o) return true;

if (o instanceof COSInteger) {
return this.value == ((COSInteger) o).value;
}

if (o instanceof COSReal) {
COSReal thatReal = (COSReal) o;
return BigDecimal.valueOf(this.value).compareTo(thatReal.getDecimalValue()) == 0;
}

return false;
}

public long get() {
return this.value;
}
Expand All @@ -97,4 +115,9 @@ public boolean equals(Object o) {
return value == that.value;

}

@Override
public BigDecimal getDecimalValue() {
return BigDecimal.valueOf(value);
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/verapdf/cos/COSName.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.verapdf.cos.visitor.IVisitor;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Objects;

/**
Expand Down Expand Up @@ -91,6 +92,19 @@ public boolean setName(final ASAtom value) {
return true;
}

@Override
public boolean isEquivalentTo(Object o) {
if (this == o) return true;
if (!(o instanceof COSName)) return false;

COSName that = (COSName) o;

byte[] thisBytes = this.getName().getValue().getBytes(StandardCharsets.ISO_8859_1);
byte[] thatBytes = that.getName().getValue().getBytes(StandardCharsets.ISO_8859_1);

return Arrays.equals(thisBytes, thatBytes);
}

Comment on lines +95 to +107

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Handle null ASAtom values before byte canonicalization.

COSName.construct(ASAtom) can store a null value, but this method dereferences getName().getValue() for both operands. Comparing null-valued names therefore throws an NPE; define null semantics before converting to bytes.

🧰 Tools
🪛 ast-grep (0.45.0)

[warning] 101-101: Use a randomly-generated IV
Context: byte[] thisBytes = this.getName().getValue().getBytes(StandardCharsets.ISO_8859_1);
Note: [CWE-329] Generation of Predictable IV with CBC Mode.

(random-iv)


[warning] 102-102: Use a randomly-generated IV
Context: byte[] thatBytes = that.getName().getValue().getBytes(StandardCharsets.ISO_8859_1);
Note: [CWE-329] Generation of Predictable IV with CBC Mode.

(random-iv)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/org/verapdf/cos/COSName.java` around lines 95 - 107, Update
COSName.isEquivalentTo to handle null ASAtom values before calling getValue() or
converting to bytes. Ensure two null values compare as equivalent, a null versus
non-null value returns false, and retain the existing byte comparison for
non-null values.

public ASAtom get() {
return value;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/verapdf/cos/COSNull.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public Object accept(final ICOSVisitor visitor) {
return visitor.visitFromNull(this);
}

@Override
public boolean isEquivalentTo(Object o) {
return equals(o);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/verapdf/cos/COSNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package org.verapdf.cos;

import java.math.BigDecimal;

/**
* @author Timur Kamalov
*/
Expand All @@ -29,4 +31,6 @@ public COSNumber() {
super();
}

public abstract BigDecimal getDecimalValue();

}
13 changes: 13 additions & 0 deletions src/main/java/org/verapdf/cos/COSObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,19 @@ public void setIsHeaderFormatComplyPDFA(Boolean isHeaderFormatComplyPDFA) {
this.isHeaderFormatComplyPDFA = isHeaderFormatComplyPDFA;
}

public boolean isEquivalentTo(Object o) {
if (o == this) return true;
if (o instanceof COSObject) {
COSObject cosObject = (COSObject) o;
return base.isEquivalentTo(cosObject.base);
}

if (o instanceof COSIndirect) {
return base.isEquivalentTo(((COSIndirect) o).getDirect().base);
Comment on lines +497 to +505

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🔴 Critical | ⚡ Quick win

Define behavior for empty and unresolved wrappers before dereferencing.

Both implementations assume a non-null underlying object, but the surrounding APIs permit empty COSObjects and nullable indirect children.

  • src/main/java/org/verapdf/cos/COSObject.java#L497-L505: handle null base values before delegation.
  • src/main/java/org/verapdf/cos/COSIndirect.java#L487-L494: handle null/unresolved getDirect() results before calling equivalence.
📍 Affects 2 files
  • src/main/java/org/verapdf/cos/COSObject.java#L497-L505 (this comment)
  • src/main/java/org/verapdf/cos/COSIndirect.java#L487-L494
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/org/verapdf/cos/COSObject.java` around lines 497 - 505, Update
COSObject.isEquivalentTo in src/main/java/org/verapdf/cos/COSObject.java:497-505
to handle null base values before delegating, preserving equivalent
empty-wrapper behavior. Also update COSIndirect.isEquivalentTo in
src/main/java/org/verapdf/cos/COSIndirect.java:487-494 to check getDirect() for
null or unresolved results before accessing base, and define equivalence
consistently without dereferencing null objects.

}
return false;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/verapdf/cos/COSReal.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.verapdf.cos.visitor.ICOSVisitor;
import org.verapdf.cos.visitor.IVisitor;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;

Expand All @@ -45,6 +46,11 @@ public class COSReal extends COSNumber {
protected COSReal() {
}

@Override
public BigDecimal getDecimalValue() {
return BigDecimal.valueOf(value);
}
Comment on lines +49 to +52

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

printf 'Files:\n'
fd -a 'COSReal.java|COSInteger.java' .

printf '\nCOSReal outline:\n'
ast-grep outline src/main/java/org/verapdf/cos/COSReal.java || true

printf '\nCOSInteger outline:\n'
ast-grep outline src/main/java/org/verapdf/cos/COSInteger.java || true

printf '\nRelevant code snippets:\n'
sed -n '1,140p' src/main/java/org/verapdf/cos/COSReal.java
printf '\n---\n'
sed -n '1,125p' src/main/java/org/verapdf/cos/COSInteger.java

printf '\nSearch for COSReal/COSInteger creation and getDecimalValue usage:\n'
rg -n "new COSReal|COSReal\.|getDecimalValue|compareTo.*getDecimalValue" src/main/java/org/verapdf/cos src/main/java -g '*.java' | head -200

printf '\nJava BigDecimal.valueOf behavior probe:\n'
python3 - <<'PY'
import math
values = [float('nan'), float('inf'), float('-inf'), 42.0]
for v in values:
    try:
        print(f"{v}: finite={math.isfinite(v)}")
        print(f"  Double.toString={float_repr(v)}")
    except NameError as e:
        print(f"{v}: finite={math.isfinite(v)}")
PY
printf '\nBehavior note: BigDecimal.valueOf(float/double) rejects NaN/infinity via Double.toString.\n'

Repository: veraPDF/veraPDF-parser

Length of output: 14662


🏁 Script executed:

#!/bin/bash
set -euo pipefail

provider=$(mvn help:evaluate -Dexpression=project.properties.pdfbox-provider -q -DforceStdout 2>/dev/null || true)
if [ -n "$provider" ]; then
    echo "current pdfbox-provider=${provider}"
else
    echo "pdfbox-provider not available"
fi

# Read the parser/token code around real-number construction without running repo code.
printf '\nPDFStreamParser token.real context:\n'
sed -n '120,190p' src/main/java/org/verapdf/parser/PDFStreamParser.java

printf '\nPostscript operators around division/log/sqrt context:\n'
sed -n '360,505p' src/main/java/org/verapdf/parser/postscript/PSOperator.java

printf '\nPDFStreamParser number token pattern references:\n'
rg -n "real|NUMBER|Double\\.|parseFloat" src/main/java/org/verapdf/parser src/main/java/org/verapdf/pd/function src/main/java/org/verapdf/pd/colors -g '*.java' | head -120

Repository: veraPDF/veraPDF-parser

Length of output: 11491


🌐 Web query:

Java BigDecimal.valueOf double NaN Infinity IllegalArgumentException source Double.toString

💡 Result:

In Java, the BigDecimal.valueOf(double) method explicitly throws a NumberFormatException (not an IllegalArgumentException) when the provided double is NaN (Not-a-Number), Double.POSITIVE_INFINITY, or Double.NEGATIVE_INFINITY [1][2][3][4]. The rationale for this behavior and its relationship to Double.toString(double) is as follows: 1. Method Behavior: The BigDecimal.valueOf(double) method is designed to be equivalent to calling new BigDecimal(Double.toString(val)) [1][4][5]. Since Double.toString() returns the strings "NaN", "Infinity", or "-Infinity" for non-finite values, and the BigDecimal(String) constructor cannot parse these specific strings into a numeric value, a NumberFormatException is thrown [1][3][6]. 2. Explicit Validation: Historically, this exception was a consequence of passing those strings to the BigDecimal(String) constructor [5]. However, modern implementations of BigDecimal.valueOf(double) (such as in recent OpenJDK versions) include explicit checks—such as !Double.isFinite(val)—to throw a NumberFormatException with a descriptive message ("Infinite or NaN") before any string conversion takes place [6]. 3. API Distinction: This behavior aligns with the standard design of BigDecimal as a class representing finite, arbitrary-precision signed decimal numbers, which inherently does not support non-finite floating-point concepts [2].

Citations:


Prevent finite-only real values from being exposed through BigDecimal conversion.

COSReal.setValue(double) also accepts NaN/infinity via set(), so getDecimalValue() will throw NumberFormatException when called from equivalent-comparison paths. Reject non-finite inputs in set / setReal / construct, or handle them explicitly before calling BigDecimal.valueOf(value) in getDecimalValue() and COSInteger.isEquivalentTo(...).

📍 Affects 2 files
  • src/main/java/org/verapdf/cos/COSReal.java#L49-L52 (this comment)
  • src/main/java/org/verapdf/cos/COSInteger.java#L88-L90
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main/java/org/verapdf/cos/COSReal.java` around lines 49 - 52, Ensure
non-finite values accepted by COSReal.setValue(double) cannot reach BigDecimal
conversion: update COSReal’s set/setReal/construct paths to reject NaN and
infinities, or explicitly handle them in COSReal.getDecimalValue() before
BigDecimal.valueOf(value). Also update COSInteger.isEquivalentTo(...) to handle
non-finite COSReal values without invoking BigDecimal conversion; apply the
changes in src/main/java/org/verapdf/cos/COSReal.java lines 49-52 and
src/main/java/org/verapdf/cos/COSInteger.java lines 88-90.


protected COSReal(final double value) {
this.value = value;
}
Expand Down Expand Up @@ -88,6 +94,23 @@ public boolean setReal(final double value) {
return true;
}

@Override
public boolean isEquivalentTo(Object o) {
if (this == o) return true;

if (o instanceof COSReal) {
COSReal thatReal = (COSReal) o;
return this.getDecimalValue().compareTo(thatReal.getDecimalValue()) == 0;
}

if (o instanceof COSInteger) {
COSInteger thatInt = (COSInteger) o;
return this.getDecimalValue().compareTo(thatInt.getDecimalValue()) == 0;
}

return false;
}

public double get() {
return this.value;
}
Expand Down
Loading
Loading