⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testmemleak.cpp

📁 cppcheck is a static C/C++ code analyzer that checks for memory leaks, mismatching allocation-deallo
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        ASSERT_EQUALS(std::string(""), errout.str());    }    void ifelse9()    {        check("static char *f()\n"              "{\n"              "    char *s = new char[10];\n"              "    if ( ghfgf )\n"              "    {\n"              "        delete [] s;\n"              "    }\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void ifelse10()    {        check("static char *f()\n"              "{\n"              "    char *s = new char[10];\n"              "    if ( ghfgf )\n"              "    {\n"              "        str[0] = s;\n"              "    }\n"              "    else\n"              "    {\n"              "        str[0] = s;\n"              "    }\n"              "}\n", true);        ASSERT_EQUALS(std::string(""), errout.str());    }    void if1()    {        check("void f()\n"              "{\n"              "    struct abc *p = new abc;\n"              "    p->a = new char[100];\n"              "    if ( ! p->a )\n"              "        return;\n"              "    foo(p);\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:6]: (error) Memory leak: p\n"), errout.str());    }    void if2()    {        check("void f()\n"              "{\n"              "    struct smp_alt_module *smp;\n"              "    smp = kzalloc(sizeof(*smp), GFP_KERNEL);\n"              "    if (NULL == smp)\n"              "        return;\n"              "    kfree( smp );\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void if3()    {        check("void f()\n"              "{\n"              "    char *s = new char[100];\n"              "    if (0 != s)\n"              "        foo(s);\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void if4()    {        check("void f()\n"              "{\n"              "    char *s;\n"              "    bool b = true;\n"              "    if (b && (s = malloc(256)))\n"              "        ;\n"              "    if (b)\n"              "        free(s);\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void if5()    {        check("void f()\n"              "{\n"              "    char *p = malloc(256);\n"              "    if (somecondition && !p)\n"              "        return;\n"              "    free(p);\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void if6()    {        check("void f()\n"              "{\n"              "    FILE *a = 0;\n"              "    a = fopen(\"test.txt\", \"rw\");\n"              "    if( a == 0 )\n"              "    {\n"              "        a = fopen(\"test.txt\", \"r\");\n"              "    }\n"              "\n"              "    fclose( a );\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void if7()    {        check("void f( bool b )\n"              "{\n"              "    int *a=0;\n"              "    if( b )\n"              "    {\n"              "        a = new int[10];\n"              "    }\n"              "\n"              "    if( b )\n"              "        delete [] a;\n"              "    else {}\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void if8()    {        check("static void f(int i)\n"              "{\n"              "    char *c = malloc(50);\n"              "    if (i == 1)\n"              "    {\n"              "        free(c);\n"              "        return;\n"              "    }\n"              "    if (i == 2)\n"              "    {\n"              "        return;\n"              "    }\n"              "    free(c);\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: c\n"), errout.str());    }    void if9()    {        check("static void f()\n"              "{\n"              "	char *buf = NULL, *tmp;\n"              "	if (!(tmp = realloc(buf, 50)))\n"              "	{\n"              "		free(buf);\n"              "		return NULL;\n"              "	}\n"              "	buf = tmp;\n"              "	return buf;\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void if10()    {        check("static void f()\n"              "{\n"              "	char *buf = malloc(10);\n"              "	if (aa)\n"              "	    ;\n"              "    else if (buf = realloc(buf, 100))\n"              "		;\n"              "    free(buf);\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void if11()    {        check("void foo()\n"              "{\n"              "    int *x = new int[10];\n"              "    if (x == 0 || aa)\n"              "    {\n"              "        return 1;\n"              "    }\n"              "    delete [] x;\n"              "}\n", true);        ASSERT_EQUALS(std::string("[test.cpp:6]: (error) Memory leak: x\n"), errout.str());    }    void forwhile1()    {        check("void f()\n"              "{\n"              "    char *str = strdup(\"hello\");\n"              "    while (condition)\n"              "    {\n"              "        if (condition)\n"              "        {\n"              "            break;\n"              "        }\n"              "    }\n"              "    free(str);\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void forwhile2()    {        check("void f()\n"              "{\n"              "    for (int i = 0; i < j; i++)\n"              "    {\n"              "        char *str = strdup(\"hello\");\n"              "        if (condition)\n"              "            continue;\n"              "        free(str);\n"              "    }\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:7]: (error) Memory leak: str\n"), errout.str());    }    void forwhile3()    {        check("void f()\n"              "{\n"              "    char *str = 0;\n"              "    for (int i = 0; i < 10; i++)\n"              "    {\n"              "        str = strdup(\"hello\");\n"              "    }\n"              "    free(str);\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:4]: (error) Memory leak: str\n"), errout.str());    }    void forwhile4()    {        check("void f()\n"              "{\n"              "    char *str = 0;\n"              "    for (int i = 0; i < 10; i++)\n"              "    {\n"              "        str = strdup(\"hello\");\n"              "        if (str) { }\n"              "    }\n"              "    free(str);\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:4]: (error) Memory leak: str\n"), errout.str());    }    void forwhile5()    {        check("void f(const char **a)\n"              "{\n"              "    char *str = 0;\n"              "    for (int i = 0; i < 10 && !str; ++i)\n"              "    {\n"              "        str = strdup(a[i]);\n"              "    }\n"              "    return str;\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void forwhile6()    {        check("void f(const char **a)\n"              "{\n"              "    char *str = 0;\n"              "    for (int i = 0; i < 10 && !str; ++i)\n"              "    {\n"              "        str = strdup(a[i]);\n"              "    }\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: str\n"), errout.str());    }    void forwhile7()    {        check("void f()\n"              "{\n"              "    for (int i = 0; i < j; i++)\n"              "    {\n"              "        char *str = strdup(\"hello\");\n"              "        if (condition)\n"              "            break;\n"              "        free(str);\n"              "    }\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:7]: (error) Memory leak: str\n"), errout.str());    }    void forwhile8()    {        check("char *f()\n"              "{\n"              "    char *a = 0;\n"              "    int i = 0;\n"              "    for( ;; )\n"              "    {\n"              "    ++i;\n"              "    a = realloc( a, i );\n"              "    if( !a )\n"              "        return 0;\n"              "\n"              "    if( i > 10 )\n"              "        break;\n"              "    }\n"              "\n"              "    return a;\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void forwhile9()    {        check("char *f()\n"              "{\n"              "    char *a = 0;\n"              "    int i = 0;\n"              "    for( ;; )\n"              "    {\n"              "    if(i>=0)\n"              "        continue;\n"              "    a = realloc( a, i );\n"              "    if(i>=0)\n"              "        continue;\n"              "    }\n"              "\n"              "    return a;\n"              "}\n", true);        ASSERT_EQUALS(std::string(""), errout.str());    }    void forwhile10()    {        check("char *f()\n"              "{\n"              "    char *a = 0;\n"              "    int i = 0;\n"              "    for( ;; )\n"              "    {\n"              "    if(i>=0)\n"              "        continue;\n"              "    a = realloc( a, i );\n"              "    if(i>=0)\n"              "        return;\n"              "    }\n"              "\n"              "    return a;\n"              "}\n", true);        ASSERT_EQUALS(std::string("[test.cpp:11]: (error) Memory leak: a\n"), errout.str());    }    void dowhile1()    {        check("void f()\n"              "{\n"              "    char *str = strdup(\"abc\");\n"              "    do\n"              "    {\n"              "        str = strdup(\"def\");\n"              "    }\n"              "    while (!str);\n"              "    return str;\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:6]: (error) Memory leak: str\n"), errout.str());    }    void switch1()    {        check("void f()\n"              "{\n"              "    char *str = new char[10];\n"              "    switch (abc)\n"              "    {\n"              "        case 1:\n"              "            break;\n"              "    };\n"              "    delete [] str;\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void switch2()    {        const std::string code("void f()\n"                               "{\n"                               "    char *str = new char[10];\n"                               "    switch (abc)\n"                               "    {\n"                               "        case 1:\n"                               "            delete [] str;\n"                               "            break;\n"                               "        default:\n"                               "            break;\n"                               "    };\n"                               "}\n");        check(code.c_str(), false);        ASSERT_EQUALS("", errout.str());        check(code.c_str(), true);        ASSERT_EQUALS("[test.cpp:12]: (all) Memory leak: str\n", errout.str());    }    void switch3()    {        check("void f()\n"              "{\n"              "    char *str = new char[10];\n"              "    while (abc)\n"              "    {\n"              "        switch (def)\n"              "        {\n"              "            default:\n"              "                return;\n"              "        }\n"              "    }\n"              "    delete [] str;\n"              "}\n");        ASSERT_EQUALS("[test.cpp:9]: (error) Memory leak: str\n", errout.str());    }    void ret1()    {        check("char *f( char **str )\n"              "{\n"              "    char *ret = malloc( 10 );\n"              "    return *str = ret;\n"              "}\n");        ASSERT_EQUALS(std::string(""), errout.str());    }    void ret2()    {        check("void foo()\n"              "{\n"              "    struct ABC *abc = new ABC;\n"              "    abc->a = new char[10];\n"              "    if ( ! abc->a )\n"              "        return;\n"              "    delete [] abc->a;\n"              "    delete abc;\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:6]: (error) Memory leak: abc\n"), errout.str());    }    void ret3()    {        check("void foo()\n"              "{\n"              "    FILE *filep = fopen(\"myfile.txt\",\"w\");\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:4]: (error) Resource leak: filep\n"), errout.str());    }    void ret4()    {        check("void foo()\n"              "{\n"              "    FILE *p = popen( \"ls -l\", \"r\");\n"              "}\n");        ASSERT_EQUALS(std::string("[test.cpp:4]: (error) Resource leak: p\n"), errout.str());    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -