Skip to content

ArrayIndexOutOfBoundsException when deserializing certain double values #283

@eduard-vasinskyi

Description

@eduard-vasinskyi

Hi. I have encountered ArrayIndexOutOfBoundsException when deserializing double values.
Here is a code that reproduces the problem:

public static void main(String[] args) throws IOException {
	double number = 2E15;

	DslJson<?> dslJson = new DslJson<>();
	JsonWriter jsonWriter = dslJson.newWriter();
	NumberConverter.serialize(number, jsonWriter);
	String string = jsonWriter.toString();

	System.out.println(string);
	System.out.println(number);

	JsonReader<?> jsonReader = dslJson.newReader();

	ByteArrayInputStream bais = new ByteArrayInputStream(string.getBytes(StandardCharsets.UTF_8));
	jsonReader.process(bais);
	jsonReader.getNextToken();

	double deserialized = NumberConverter.deserializeDouble(jsonReader); 
	System.out.println(deserialized);
}

Here is the output:

2000000000000000.0
2.0E15
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
	at com.dslplatform.json.NumberConverter.approximateDouble(NumberConverter.java:519)
	at com.dslplatform.json.NumberConverter.parseDouble(NumberConverter.java:497)
	at com.dslplatform.json.NumberConverter.deserializeDouble(NumberConverter.java:398)
        ...

I got this error on dsl-json-java8 v1.10.0. But it is also reproducible in the current 'master' branch.
It also fails for numbers 3E15, 4E15, 5E15, 6E15, 7E15.
Originally, it failed on deserializing number 2670986322885633.0
Tested on Java 8 and 23

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions