alibaba / alibaba/fastjson2

[BUG]值超出范围时应当抛出异常

Open
#3,448 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
*简要描述您碰到的问题。*
JSONArray/JSONObject使用getShort()/getFloat()等方法时,当值超出范围时应当抛出异常。

### 环境信息
*请填写以下信息:*

- OS信息: [MacOS Sonoma 14.7 M3 Pro 36 GB]
- JDK信息: [Openjdk 11.0.26]
- 版本信息:[Fastjson2 53258fbfcf8019c197cbd485f8da2ffd200dfaca]

### 重现步骤
*如何操作可以重现该问题:*

1. 使用 getShort()或getFloat()等 方法
2. 输入 超出范围的 数据
3. 没有异常提示
```java
//可在此输入示例代码
@Test
public void testShort() {
JSONArray array = new JSONArray();
array.add(23456789);
System.out.println(array.getShort(0));//-5099,和array.add(-5099)的效果一样,不太合理
System.out.println(array.getShortValue(0));//-5099
}

@Test
public void testGetFloat() {
JSONObject object = new JSONObject();
object.put("val", "1.7976931348623157E308");//Double.MAX_VALUE
System.out.println(object.getFloat("val"));//Infinity
System.out.println(object.getFloatValue("val"));//Infinity
}
```

### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
期待抛出异常,参考https://github.com/alibaba/fastjson/issues/868

### 相关日志输出
*请复制并粘贴任何相关的日志输出。*

#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*

Contributor guide

Open the contributing guide

Research direction

Start with the JSONArray and JSONObject getShort(), getShortValue(), getFloat(), and getFloatValue() calls shown in the reproduction tests. Run those tests with the out-of-range values and compare the returned values with the expected exception behavior, including the linked fastjson issue for context. Done means all affected conversions reject values outside their target type range consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.