Skip to content

Instantly share code, notes, and snippets.

@greenlaw110
Last active August 20, 2017 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greenlaw110/ccc7feca57b9de61fa0e507074106d6f to your computer and use it in GitHub Desktop.
Save greenlaw110/ccc7feca57b9de61fa0e507074106d6f to your computer and use it in GitHub Desktop.
The source code that reproduce https://github.com/alibaba/fastjson/issues/1424
package testapp;
import com.alibaba.fastjson.JSON;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
public class FastJson37IssueWithOverflowFloatParsing {
public static class IntegerVal {
private int v;
public void setV(int v) {
this.v = v;
}
@Override
public String toString() {
return String.valueOf(v);
}
}
public static class FloatVal {
private float v;
public void setV(float v) {
this.v = v;
}
@Override
public String toString() {
return String.valueOf(v);
}
}
public static void main(String[] args) {
String fastJsonVersion = JSON.VERSION;
System.out.println("fastjson version: " + fastJsonVersion);
Map<String, Long> intOverflowMap = new HashMap<>();
long intOverflow = Integer.MAX_VALUE;
intOverflowMap.put("v", intOverflow + 1);
String sIntOverflow = JSON.toJSONString(intOverflowMap);
System.out.println("prepare to parse overflow int val: " + sIntOverflow);
try {
JSON.parseObject(sIntOverflow, IntegerVal.class);
} catch (Exception e) {
System.out.println("We captured the Exception: " + e.getMessage());
}
Map<String, Double> floatOverflowMap = new HashMap<>();
double floatOverflow = Float.MAX_VALUE;
floatOverflowMap.put("va", floatOverflow + 1);
String sFloatOverflow = JSON.toJSONString(floatOverflowMap);
System.out.println("prepare to parse overflow float val: " + sIntOverflow);
FloatVal floatVal = JSON.parseObject(sFloatOverflow, FloatVal.class);
System.out.println("We expect an exception raised, but found it parsed out an invalid value: " + floatVal);
}
}
@wenshao
Copy link

wenshao commented Aug 20, 2017

floatOverflowMap.put("va", floatOverflow + 1);
                     ----
floatOverflowMap.put("v", floatOverflow + 1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment