zoukankan      html  css  js  c++  java
  • klockwork修改

    • kw异常描述 - 01

    Tainted data '(Double.valueOf(...) / 100)' that comes from 'bufferedReader.readLine()' is used in an arithmetic operation and can cause an integer overflow or unexpected result 

     

    代码行:

    Double samplePercentage = Double.valueOf(samples) * (Double.valueOf(percent) / 100);

    修改后的代码:

    BigDecimal b1 = new BigDecimal(Double.parseDouble(samples));
    BigDecimal b2 = new BigDecimal(Double.parseDouble(percent) / 100);
    BigDecimal samplePercentage = b1.multiply(b2);

    修改方法及原因:
    float和double是为了科学计算和工程计算而设计的,只是为了广泛的数值范围上提供较为精确的快速近似值计算,不适用于需要提供精确的计算。不需要记录的值为十进制小数点,所涉及的值不太大可用int或long,

    需要记录十进制小数点则使用BigDecimal。

    • kw异常描述 - 02

    Sql object 'statement' is not closed on exit. 

    代码行:

    PreparedStatement statement = connection.prepareStatement(sql_snap, PreparedStatement.RETURN_GENERATED_KEYS);

    ......

    if(statement!=null){
    try{
    statement.close();
    }catch (Exception e){
    LLSysLog.logError("statement Release ERROR",e);
    }
    }

    修改后的代码:

    PreparedStatement statement =null;
    try{
    statement = connection.prepareStatement(sql_snap, PreparedStatement.RETURN_GENERATED_KEYS);
    ......
    }finally{
    if(statement!=null){
    try{
    statement.close();
    }catch (Exception e){
    LLSysLog.logError("statement Release ERROR",e);
    }
    }
    }

    修改方法及原因:

    使用PrepareStatement、ResultSet、InputStream等均需要关闭,因为执行的过程中可能发生异常导致后面的代码没有执行,导致不能关闭,所以需要在finally中关闭,关闭的时候需要去捕获关闭的异常,因为有可能会关闭异常。

    • kw异常描述 - 03

    Null pointer dereference 

    代码行:

    if(map.ger(ip);){

    String userName=map.ger(ip);.getUserName();

    }

    修改后的代码:

    User user=map.ger(ip);

    if(user!=null){

    String userName=user.getUserName();

    }

    修改方法及原因:

    当从外部传来的一个对象,要使用对象做getset方法时,需先判断对象是否为空。

    获取Map里的数据后,且数据为为对象,应先将数据取出来再判断对象是否为空。

    因为直接判断,等使用的时候可能值已经改变了

  • 相关阅读:
    软件工程课程总结
    c#代码分析
    运用visual studio进行简单的单元测试
    安装visual studio过程
    忙着,快乐着
    软件工程心得
    session
    XML
    期末团队评价
    黄金点游戏
  • 原文地址:https://www.cnblogs.com/lihongling/p/9407228.html
Copyright © 2011-2022 走看看