如何解决整数溢出问题?

1、问题解析

当以整数数据类型(包括字节、短、长和其他类型)存储的值过大(大于变量可容纳的最大值)的值时,将发生整数溢出(或环
绕)。整数的最高有效位丢失,而其余值则相对于最小值(对于带符号类型,为 0 或负值)。简单来说就是实体类是个Integer类型,但是请求的时候传了值超过了Integer的最大取值范围报错了,但是不能返回异常响应(如500、400等),否则就认为没有对这种情况进行处理。

2、修复建议

  • 在问题api成批分配(跳转链接)的基础上修改 ,针对InputCoercionException异常单独处理。
@ExceptionHandler(value = HttpMessageNotReadableException.class)
    public ResponseEntity<ResponseDTO> methodHttpMessageNotReadableExceptionHandler(
            HttpServletRequest request, HttpMessageNotReadableException e) {
        // 判断是否是整数溢出问题
        if (e.getRootCause() instanceof InputCoercionException || e.getRootCause() instanceof InvalidFormatException) {
            logger.error("类型转换错误");
            logger.error("请求地址:{}", request.getServletPath());
            ResponseDTO responseDTO = WebUtils.createFailureResponse(ErrorCodeEnum.S_REQ_PARAM_ERROR, "非法的请求参数");
            return new ResponseEntity<>(responseDTO, HttpStatus.OK);
        }
        logger.error("请求body中存在实体类中不存在的参数");
        logger.error("请求地址:{}", request.getServletPath());
        ResponseDTO responseDTO = WebUtils.createFailureResponse(ErrorCodeEnum.S_REQ_PARAM_ERROR, "请求参数不正确");
        return new ResponseEntity<>(responseDTO, HttpStatus.INTERNAL_SERVER_ERROR);
    }

3、问题重新参数

请求参数的 ->“page” 本来应该传1 ,但是传了99999999999999999999,

测试传了page为99999999999999999999的请求和响应:

POST	/ngbsp-api/user-act/admin/discuss/black/list	HTTP/1.1
Host:	10.110.120.101:89
Accept:	application/json,	text/plain,	*/*
X-CSRF-TOKEN:	a6a5d13a-74e5-4507-bbc9-4fc0ccf94d7a
X-Requested-With:	XMLHttpRequest
User-Agent:	Mozilla/5.0	(Windows	NT	10.0;	Win64;	x64)	AppleWebKit/537.36	(KHTML,	like	Gecko)	Chrome/126.0.0.0	Safari/537.36Content-Type:	application/json;
Origin:	http://10.110.120.101:89
Referer:	http://10.110.120.101:89/ngbsp/
Accept-Language:	en-US
Connection:	keep-alive
Cookie:	LCUID=LzWATXrrydZKuIW4n18FFtf1mAoEUwqHy2AO0zGqMk9wz7l3tiOqZXpz/seb3o4IOyVQA4jS93S2k5OpJyRS8W62Y5Rxx+fTURpA+eQzD5g=;NGBSPSID=OGEwMjlmOTAtMDEzZi00NGY2LTk0OGQtOTM3YTg1NjYyYThj;	LCSSID=OGEwMjlmOTAtMDEzZi00NGY2LTk0OGQtOTM3YTg1NjYyYThjContent-Length:	58
{
	"page":	 99999999999999999999,
	"pageSize":	10,
	"userPhone":	""
}
HTTP/1.1	500	Internal	Server	Error
Server:	nginx/1.21.6
Date:	Wed,	24	Jul	2024	09:02:34	GMT
Content-Type:	application/json
Transfer-Encoding:	chunked
Connection:	keep-alive
Cache-Control:	no-cache,	no-store,	max-age=0,	must-revalidate
Expires:	0
Pragma:	no-cache
Referrer-Policy:	no-referrer
X-Content-Type-Options:	nosniff
X-Frame-Options:	DENY
X-Xss-Protection:	1	;	mode=block
{
	"timestamp":	"2024-07-24	17:02:34",
	"status":	500,
	"error":	"Internal	Server	Error",
	"path":	"/user-act/admin/discuss/black/list"
}

5、修改后测试

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农小C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值