垃圾sonar,心里一万只XXX

简介: 因为业务场景我必然需要绝对随机或者不能重复的啊!!!因此采用他的改造建议

欢迎点击主页查看更多内容....

代码申诉我写过好多次了,按理说正常的代码扫描给出的建议确实很专业,但也要看是哪款扫描软件,但就此次的sonar来说,垃圾!!!

业务负责返回前端轨迹RGB值,为了区分每次颜色区间,用随机数生成。因此用到了Random函数,本地测试环境都通过后测试后续业务,就在上午偶然想起代码检测的一些bug和漏洞,就想跟随解决,但发现一些问题。

首先扫描出的空指针问题最多,但扫描时不结合上下文判断,上文判空后,后续用到对象仍然认为没有判空,然后是漏洞扫描,除了对象内属性没有显示的声明外,报出了此次的问题

image.png

本着能发现就处理的原则,就看了下这个问题,

每次需要一个随机值时创建一个新的随机对象是低效的,并且可能会根据JDK生成非随机的数字。为了获得更好的效率和随机性,请创建一个随机文件,然后存储并重用它。

Random()构造函数每次都尝试使用不同的值设置种子。但是,不能保证种子是随机的,甚至是均匀分布的。一些JDK将使用当前时间作为种子,这使得生成的数字完全不是随机的。

此规则查找每次调用方法并将其分配给局部随机变量时创建新随机变量的情况

因为业务场景我必然需要绝对随机或者不能重复的啊!!!因此采用他的改造建议

图片

就是用函数SecureRandom

private Random rand = SecureRandom.getInstanceStrong();

噩梦开始

本地测试请求后完美通过开始提测,又提升了代码质量,又提升了随机值效率,何乐而不为?

oh mud fuck

构建完提测,前端请求此接口超时1分钟+未返回数据???

我一开始看到前台页面是报的前端跨域,请求超时,那么找到前端看此问题,前端表示此请求是后端请求到但后端未返回数据超时

找运维,是否是网关配了超时策略,未通过响应阈值而被杀掉,并没有,也没有触发LB,可是本地好好的啊???

难道是我数据量大表查询,导致太慢???

但我一是筛选当天数据并不可能出现大数量啊?而且是测试环境啊?

没办法,开始每行打日志计算响应时间

终于,没有终于此时我构建后端超过15次,时间已经过了2个小时

终于,到我的计算RGB色码这里,他hang住了???后端死都不返回数据,他能不超时吗?

然后看我这段方法,我下午14.46对此段代码进行了优化

 try {
            random = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException e) {
            return "#000000";
        }

 public static SecureRandom getInstanceStrong()
            throws NoSuchAlgorithmException {

        String property = AccessController.doPrivileged(
            new PrivilegedAction<String>() {
                @Override
                public String run() {
                    return Security.getProperty(
                        "securerandom.strongAlgorithms");
                }
            });

        if ((property == null) || (property.length() == 0)) {
            throw new NoSuchAlgorithmException(
                "Null/empty securerandom.strongAlgorithms Security Property");
        }

        String remainder = property;
        while (remainder != null) {
            Matcher m;
            if ((m = StrongPatternHolder.pattern.matcher(
                    remainder)).matches()) {

                String alg = m.group(1);
                String prov = m.group(3);

                try {
                    if (prov == null) {
                        return SecureRandom.getInstance(alg);
                    } else {
                        return SecureRandom.getInstance(alg, prov);
                    }
                } catch (NoSuchAlgorithmException |
                        NoSuchProviderException e) {
                }
                remainder = m.group(5);
            } else {
                remainder = null;
            }
        }

        throw new NoSuchAlgorithmException(
            "No strong SecureRandom impls available: " + property);
    }

注意此方法SecureRandom generateSeed()。他使用了一个叫做阻塞数字生成器,如果没有足够的随机值来生成,那么就会一直阻塞在那里!!!

具体原因查看一下资料,但是此次的bug坑了我一下午时间!!!表示很干

目录
相关文章
五分钟带你玩转sonar(五)教你使用SonarLint插件
五分钟带你玩转sonar(五)教你使用SonarLint插件
2840 0
五分钟带你玩转sonar(五)教你使用SonarLint插件
|
23天前
|
存储 Linux
linux上SVN出现 "Unable to connect to a repository at URL 'svn://xx.xx.xx.xx/xxx' 和 No repository ...
centos上安装了svn, 有时候会不知道什么原因出现客户端小乌龟无法连接或无法提交等情况
26 5
|
7月前
|
Linux
svn踩坑-xxx is not valid as filename in directory
svn踩坑-xxx is not valid as filename in directory
33 0
|
Java Maven
Maven中Could not transfer artifact xxx from/to xxx问题(附镜像云配置)
Maven中Could not transfer artifact xxx from/to xxx问题(附镜像云配置)
354 0
|
关系型数据库 MySQL
1130 - Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server
1130 - Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server
91 0
1130 - Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server
|
SQL Java 数据库连接
Could not find resource xxx/xxxx/xxx.xml报错解决
Could not find resource xxx/xxxx/xxx.xml报错解决
Could not find resource xxx/xxxx/xxx.xml报错解决
|
关系型数据库 MySQL 网络安全
Navicat - 2003 - Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10038)
Navicat - 2003 - Can‘t connect to MySQL server on ‘xxx.xxx.xxx.xxx‘ (10038)
106 0
|
JavaScript jenkins Shell
在Jenkins中使用sonar进行静态代码检查
要解决的问题 jenkins自动构建完成后,希望能通过sonar静态代码检查生成一份报告,给与开发人员对当前代码的做一个质量评估和修改意见 1.安装并配置sonar服务器 懒得说,跟着官方文档走就行,这边主要的开发语言是.
3057 0
|
jenkins 持续交付