静态代码扫描错误类型

版权声明: https://blog.csdn.net/lihaidong1991/article/details/85007327

       最近工作中对代码库中的代码借助Tscancode工具进行了一次全面的扫描,并进行清理。从而优化代码、消除隐患以及为后续的代码监控做好准备。在此特地总结一下几种主要的错误类型,以供大家借鉴,避免犯同样的错误。以下代码仅为demo,非正式代码,仅供参考。

类成员未初始化

// 修正前
class CFace
{
public:
    CFace(): m_pBody(nullptr){};  //  m_index 未初始化
    ~CFace(){ delete m_pBody};

private:
    CBody* m_pBody;
    int m_index;
}

// 修正后
class CFace
{
public:
    CFace(): m_pBody(nullptr), m_index(-1){};
    ~CFace(){ delete m_pBody};

private:
    CBody* m_pBody;
    int m_index;
}

可派生类析构函数为非虚函数

// 修正前
class CFace
{
public:
    CFace(): m_pBody(nullptr){};
    ~CFace(){ delete m_pBody};  // 析构函数为非虚函数

    virtual double Area() = 0;
private:
    CBody* m_pBody;
    int m_index;
}

class CPlaneFace : public CFace
{
    ...
}

// 修正后
class CFace
{
public:
    CFace(): m_pBody(nullptr){};
    virtual ~CFace(){ delete m_pBody};

    virtual double Area() = 0;
private:
    CBody* m_pBody;
    int m_index;
}

...

循环嵌套

// 修正前
vector<vector<int> > vec;
...  // vec赋值操作

double val(0.0);
for (size_t = 0; i < vec.size(); ++i)
{
    vector<int> vec2;
    for (size_t i = 0; i < vec2.size(); ++i)
    {
        val += vec2[i];
    }
}

// 修正后
vector<vector<int> > vec;
...  // vec赋值操作

double val(0.0);
for (size_t i = 0; i < vec.size(); ++i)
{
    vector<int> vec2;
    for (size_t j = 0; j < vec2.size(); ++j)
    {
        val += vec2[j];
    }
}

指针未判空

CFace* CreateFace(bool bPlane)
{
    if (bPlane)
        return new CPlaneFace;
    else
        return nullptr;
}

// 修正前
CFace* pFace = CreateFace(bPlane);  // bPlane为上文环境传入值
double dArea = pFace->Area();

// 修正后
CFace* pFace = CreateFace(bPlane);
double dArea(0.0);
if (pFace)
    dArea = pFace->Area();

|| 、 && 过于复杂

int getIndex();
int a = getIndex();

// 修正前
if (a < 2 || a  >= 3 && a < 5 || a >= 7 && a < 9)
{
    a = -1;
}
else
{
    a = 0;
}

// 修正后
if (a < 2 
    || (a  >= 3 && a < 5) 
    || (a >= 7 && a < 9))
{
    a = -1;
}
else
{
    a = 0;
}

循环逻辑错误

std::string getName(std::string str); // 获取非空名
vec<std::string> vecStr;  
... // vecStr 赋值操作

// 修正前
for (auto iter = vecStr.cbegin(); iter != vecStr.cend(); ++iter)
{
    return getName(*iter);
}

// 修正后
for (auto iter = vecStr.cbegin(); iter != vecStr.cend(); ++iter)
{
    std::string sName = getName(*iter);
    if (!sName.empty)
        return sName;
}

逻辑运算符两侧一致

struct CPoint2i   // 屏幕坐标点
{
    int x;
    int y;
    ...
};

// 修正前
CPoint2i oPoint = GetScreenPoint();
if (oPoint.x == 0 || oPoint.x == 0)  // 大多数复制、粘贴造成的笔误
    return false;

// 修正后
CPoint2i oPoint = GetScreenPoint();
if (oPoint.x == 0 || oPoint.y == 0)
    return false;

if 、 else分支一致

int getIndex();

// 修正前
int oIndex = getIndex();
if (oIndex < 0)
    return false;
else if (oIndex >= 0 && oIndex < 5)
    return true;
else
    return true;

// 修正后
int oIndex = getIndex();
if (oIndex < 0)
    return false;
else
    return true;

有符号、无符号混用

vector<std::string> vecStr;
... // vecStr 赋值操作

// 修正前
int index = vecStr.size();  //int && size_t 混用
for (size_t i = 0; i < vecStr.size(); ++i)
{
    if (vec[i] == "hello")
    {
        index = i;  //int && size_t 混用
        break;
    }
}

// 修正后
size_t index = vecStr.size();
for (size_t i = 0; i < vecStr.size(); ++i)
{
    if (vec[i] == "hello")
    {
        index = i;
        break;
    }
}

冗余判断

CFace* GetFace(int index)
{
    if (index >= 0 && index < FaceCount())
        return m_faceVec[inndex];

    return nullptr;
}

// 修正前
CFace* pFace = GetFace(2);
if (!pFace || (pFace && pFace->Area() < 1)) //pFace冗余判断
    return false;

// 修正后
CFace* pFace = GetFace(2);
if (!pFace || pFace->Area() < 1)
    return false;

浮点数直接比较

const double g_DoubleEpsilon = 0.0000001;   // 全局容差
bool isZero(double dVal, double dEpsilon); // 判零函数

// 修正前
double dArea = pFace->Area();
if (dArea == 0)
    return false;
else
    return true;

// 修正后
double dArea = pFace->Area();
if (isZero(dArea, g_DoubleEpsilon)
    return false;
else
    return true;

自我赋值

// 修正前
void CFace::init(CFace* pFace)
{
    if (m_index == pFace->GetIndex())
        this->m_Area = this->m_Area;  // 大多数是笔误
}

// 修正后
void CFace::init(CFace* pFace)
{
    if (m_index == pFace->GetIndex())
        this->m_Area = pFace->Area();
}

变量使用前未初始化

// 修正前
bool SetIndex(int index)
{
    bool bChange;
    if (index < 0)
    {
        m_index = 0;
        bchange = false;
    }
    else if (index > 3 && index < 5)
    {
        m_index = index;
        bchange = true;
    }
    
    return bchange;  // 有可能bchange没有被赋值
}

// 修正后
bool SetIndex(int index)
{
    bool bChange(false);
    if (index < 0)
    {
        m_index = 0;
        bchange = false;
    }
    else if (index > 3 && index < 5)
    {
        m_index = index;
        bchange = true;
    }
    
    return bchange;  // 有可能bchange没有被赋值
}

猜你喜欢

转载自blog.csdn.net/lihaidong1991/article/details/85007327