Skip to content

Add checking to avoid unexpected ArrayIndexOutOfBoundException #3395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

arthurscchan
Copy link

This is a proposed fix to stability issue discovered by OSS-Fuzz when fuzzing the powsybl-core module. The original OSS-Fuzz issue can be found in https://issues.oss-fuzz.com/u/1/issues/406332771.

@arthurscchan
Copy link
Author

The IeeeCdfReader.read(BufferedReader) method in the com.powsybl.ieeecdf.model package assumes that the first line of the file (the title line) will always be valid and parsable into an IeeeCdfTitle bean. However, if the input file is malformed (e.g., empty or with an invalid format), this assumption fails silently.

String line = reader.readLine();
IeeeCdfTitle title = parseLines(Collections.singletonList(line), IeeeCdfTitle.class).get(0);

The problem arises from the behavior of the Univocity FixedWidthParser, inherited via AbstractParser, which silently skips invalid or unparsable lines. As a result:

  • The call to parseLine(null) or parseLine(<malformed>) is skipped by the parser.
  • BeanListProcessor.getBeans() returns an empty list.
  • The .get(0) call throws an IndexOutOfBoundsException:

From the above understanding, this crash can be triggered easily by supplying an empty file or one with an invalid first line (malformed fixed-width format that doesn't match the expected bean schema for IeeeCdfTitle). This cause stability issue with unclear exception message.

@arthurscchan
Copy link
Author

This is a stability issue due to a lack of validation for failed parsing and blindly assumed that the imported data is structured correctly. Here is a simple proof of concept to trigger the problem.

import com.powsybl.ieeecdf.model.IeeeCdfReader;
import java.io.BufferedReader;
import java.io.StringReader;

public class ProofOfConcept {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new StringReader(""));
        new IeeeCdfReader().read(reader);
    }
}

To execute and test the PoC, follow the steps below. It is assumed that OpenJDK 17.0.2 and Maven 3.9.9 is used.

# Prepare OpenJDK 17.0.2
wget https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz && tar zxvf openjdk-17.0.2_linux-x64_bin.tar.gz && rm openjdk-17.0.2_linux-x64_bin.tar.gz
export JAVA_HOME=./jdk-17.0.2
export PATH=$JAVA_HOME/bin:$PATH

# Prepare Maven 3.9.9
wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz && tar zxvf apache-maven-3.9.9-bin.tar.gz && rm apache-maven-3.9.9-bin.tar.gz
export PATH_TO_MVN=./apache-maven-3.9.9/bin/mvn

# Build Powsybl-core
git clone https://github.com/powsybl/powsybl-core
cd powsybl-core
$PATH_TO_MVN clean package -DskipTests

# Group jar files
mkdir jar
for jar in $(find ./ -type f -name "*.jar"); do cp $jar jar/; done

# Build and run PoC
javac -cp "jar/*" ProofOfConcept.java
java -cp "jar/*:./" ProofOfConcept

You will get the following exception stack trace.

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0
        at java.base/java.util.Collections$EmptyList.get(Collections.java:4586)
        at com.powsybl.ieeecdf.model.IeeeCdfReader.read(IeeeCdfReader.java:36)
        at ProofOfConcept.main(ProofOfConcept.java:8)

@arthurscchan
Copy link
Author

arthurscchan commented Apr 1, 2025

The root cause is down at the IeeeCdfReader::read method. The fix is simply adding a empty checking before calling to the get method to avoid malformed input crash the execution with ArrayIndexOutOfBoundException.

Signed-off-by: Arthur Chan <[email protected]>
Signed-off-by: Arthur Chan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant