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
43 changes: 43 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,49 @@ private COSObject _at(final int i) {
return this.entries.get(i);
}

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

if (o.isIndirect()) {
return this.isEquivalentTo(o.getDirectBase());
}

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

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

if (!(o instanceof COSArray)) return false;

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

if (!Objects.equals(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(COSBase obj);

boolean isEquivalentTo(COSBase o, List<COSBasePair> checkedObjects) {
return isEquivalentTo(o);
}

boolean equals(Object obj, List<COSBasePair> checkedObjects) {
return this.equals(obj);
}
Expand Down
17 changes: 17 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,23 @@ public boolean setBoolean(final boolean value) {
return true;
}

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

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
}

if (!(o instanceof COSBoolean)) return false;

COSBoolean that = (COSBoolean) o;

return value == that.value;

}

public boolean get() {
return this.value;
}
Expand Down
67 changes: 67 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,73 @@ 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).getDirectBase();
if (value != null) {
nonNullKeys.add(key);
}
}
return nonNullKeys;
}

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

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
}

if (!(o instanceof COSDictionary)) return false;

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

/**
* Internal recursive comparison with cycle detection.
*/
boolean isEquivalentTo(COSBase o, List<COSBasePair> checkedObjects) {
if (this == o) return true;
if (o == null) return false;
if (!(o instanceof COSDictionary)) return false;
Comment thread
LonelyMidoriya marked this conversation as resolved.

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;
}

return isEquivalentKeys(that, thisKeys, checkedObjects);
}

protected boolean isEquivalentKeys(COSDictionary that, Set<ASAtom> thisKeys, List<COSBasePair> checkedObjects) {
for (ASAtom key : thisKeys) {
COSBase thisVal = this.getKey(key).getDirectBase();
COSBase thatVal = that.getKey(key).getDirectBase();

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
11 changes: 11 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,17 @@ public void mark() {
}
}

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

COSBase thisBase = this.getDirectBase();
if (thisBase == null && o == null) return true;
if (thisBase == null || o == null) return false;

return thisBase.isEquivalentTo(o.getDirectBase());
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
28 changes: 28 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,27 @@ public boolean setReal(final double value) {
return true;
}

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

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

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 +120,9 @@ public boolean equals(Object o) {
return value == that.value;

}

@Override
public BigDecimal getDecimalValue() {
return BigDecimal.valueOf(value);
}
}
17 changes: 17 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,22 @@ public boolean setName(final ASAtom value) {
return true;
}

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

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
}

if (!(o instanceof COSName)) return false;

COSName that = (COSName) o;

return Objects.equals(value, that.value);
}

Comment thread
LonelyMidoriya marked this conversation as resolved.
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(COSBase 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();

}
12 changes: 12 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,18 @@ public void setIsHeaderFormatComplyPDFA(Boolean isHeaderFormatComplyPDFA) {
this.isHeaderFormatComplyPDFA = isHeaderFormatComplyPDFA;
}

public boolean isEquivalentTo(COSObject o) {
if (o == this) return true;
if (o == null) return false;

COSBase thisBase = this.getDirectBase();
COSBase otherBase = o.getDirectBase();
if (thisBase == null && otherBase == null) return true;
if (thisBase == null || otherBase == null) return false;

return thisBase.isEquivalentTo(otherBase);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
27 changes: 27 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,27 @@ public boolean setReal(final double value) {
return true;
}

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

if (o.isIndirect()) {
return isEquivalentTo(o.getDirectBase());
}

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