[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/devzero2000/cppcheck/main/test/testbufferoverrun.cpp [Back]  [Original]

/*
 * Cppcheck - A tool for static C/C++ code analysis
 * Copyright (C) 2007-2022 Cppcheck team.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */


#include "check.h"
#include "checkbufferoverrun.h"
#include "ctu.h"
#include "errortypes.h"
#include "standards.h"
#include "library.h"
#include "preprocessor.h"
#include "settings.h"
#include "testsuite.h"
#include "tokenize.h"

#include 
#include 
#include 
#include 
#include 
#include 

#include 

#include 

class TestBufferOverrun : public TestFixture {
public:
    TestBufferOverrun() : TestFixture("TestBufferOverrun") {}

private:
    Settings settings0;

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
    void check_(const char* file, int line, const char code[], const char filename[] = "test.cpp") {
        // Clear the error buffer..
        errout.str("");

        settings0.certainty.enable(Certainty::inconclusive);

        // Tokenize..
        Tokenizer tokenizer(&settings0, this);
        std::istringstream istr(code);
        ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);

        // Check for buffer overruns..
        CheckBufferOverrun checkBufferOverrun;
        checkBufferOverrun.runChecks(&tokenizer, &settings0, this);
    }

    void check_(const char* file, int line, const char code[], const Settings &settings, const char filename[] = "test.cpp") {
        Tokenizer tokenizer(&settings, this);
        std::istringstream istr(code);
        ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line);

        // Clear the error buffer..
        errout.str("");

        // Check for buffer overruns..
        CheckBufferOverrun checkBufferOverrun(&tokenizer, &settings, this);
        checkBufferOverrun.runChecks(&tokenizer, &settings, this);
    }

    void checkP(const char code[], const char* filename = "test.cpp")
    {
        // Clear the error buffer..
        errout.str("");

        Settings* settings = &settings0;
        settings->severity.enable(Severity::style);
        settings->severity.enable(Severity::warning);
        settings->severity.enable(Severity::portability);
        settings->severity.enable(Severity::performance);
        settings->standards.c = Standards::CLatest;
        settings->standards.cpp = Standards::CPPLatest;
        settings->certainty.enable(Certainty::inconclusive);
        settings->certainty.disable(Certainty::experimental);

        // Raw tokens..
        std::vector files(1, filename);
        std::istringstream istr(code);
        const simplecpp::TokenList tokens1(istr, files, files[0]);

        // Preprocess..
        simplecpp::TokenList tokens2(files);
        std::map filedata;
        simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());

        Preprocessor preprocessor(*settings, nullptr);
        preprocessor.setDirectives(tokens1);

        // Tokenizer..
        Tokenizer tokenizer(settings, this);
        tokenizer.createTokens(std::move(tokens2));
        tokenizer.simplifyTokens1("");
        tokenizer.setPreprocessor(&preprocessor);

        // Check for buffer overruns..
        CheckBufferOverrun checkBufferOverrun(&tokenizer, settings, this);
        checkBufferOverrun.runChecks(&tokenizer, settings, this);
    }

    void run() override {
        LOAD_LIB_2(settings0.library, "std.cfg");

        settings0.severity.enable(Severity::warning);
        settings0.severity.enable(Severity::style);
        settings0.severity.enable(Severity::portability);

        TEST_CASE(noerr1);
        TEST_CASE(noerr2);
        TEST_CASE(noerr3);
        TEST_CASE(noerr4);

        TEST_CASE(sizeof3);

        TEST_CASE(array_index_1);
        TEST_CASE(array_index_2);
        TEST_CASE(array_index_3);
        TEST_CASE(array_index_4);
        TEST_CASE(array_index_6);
        TEST_CASE(array_index_7);
        TEST_CASE(array_index_11);
        TEST_CASE(array_index_12);
        TEST_CASE(array_index_13);
        TEST_CASE(array_index_14);
        TEST_CASE(array_index_15);
        TEST_CASE(array_index_16);
        TEST_CASE(array_index_17);
        TEST_CASE(array_index_18);
        TEST_CASE(array_index_19);
        TEST_CASE(array_index_20);
        TEST_CASE(array_index_21);
        TEST_CASE(array_index_22);
        TEST_CASE(array_index_23);
        TEST_CASE(array_index_24); // ticket #1492 and #1539
        TEST_CASE(array_index_25); // ticket #1536
        TEST_CASE(array_index_26);
        TEST_CASE(array_index_27);
        TEST_CASE(array_index_28); // ticket #1418
        TEST_CASE(array_index_29); // ticket #1734
        TEST_CASE(array_index_30); // ticket #2086 - out of bounds when type is unknown
        TEST_CASE(array_index_31); // ticket #2120 - out of bounds in subfunction when type is unknown
        TEST_CASE(array_index_32);
        TEST_CASE(array_index_33); // ticket #3044
        TEST_CASE(array_index_34); // ticket #3063
        TEST_CASE(array_index_35); // ticket #2889
        TEST_CASE(array_index_36); // ticket #2960
        TEST_CASE(array_index_37);
        TEST_CASE(array_index_38); // ticket #3273
        TEST_CASE(array_index_39);
        TEST_CASE(array_index_40); // loop variable calculation, taking address
        TEST_CASE(array_index_41); // structs with the same name
        TEST_CASE(array_index_42);
        TEST_CASE(array_index_43); // struct with array
        TEST_CASE(array_index_44); // #3979
        TEST_CASE(array_index_45); // #4207 - calling function with variable number of parameters (...)
        TEST_CASE(array_index_46); // #4840 - two-statement for loop
        TEST_CASE(array_index_47); // #5849
        TEST_CASE(array_index_48); // #9478
        TEST_CASE(array_index_49); // #8653
        TEST_CASE(array_index_50);
        TEST_CASE(array_index_51); // #3763
        TEST_CASE(array_index_52); // #7682
        TEST_CASE(array_index_53); // #4750
        TEST_CASE(array_index_54); // #10268
        TEST_CASE(array_index_55); // #10254
        TEST_CASE(array_index_56); // #10284
        TEST_CASE(array_index_57); // #10023
        TEST_CASE(array_index_58); // #7524
        TEST_CASE(array_index_59); // #10413
        TEST_CASE(array_index_60); // #10617, #9824
        TEST_CASE(array_index_61); // #10621
        TEST_CASE(array_index_multidim);
        TEST_CASE(array_index_switch_in_for);
        TEST_CASE(array_index_for_in_for);   // FP: #2634
        TEST_CASE(array_index_bounds);
        TEST_CASE(array_index_calculation);
        TEST_CASE(array_index_negative1);
        TEST_CASE(array_index_negative2);    // ticket #3063
        TEST_CASE(array_index_negative3);
        TEST_CASE(array_index_negative4);
        TEST_CASE(array_index_for_decr);
        TEST_CASE(array_index_varnames);     // FP: struct member #1576, FN: #1586
        TEST_CASE(array_index_for_continue); // for,continue
        TEST_CASE(array_index_for);          // FN: for,if
        TEST_CASE(array_index_for_neq);      // #2211: Using != in condition
        TEST_CASE(array_index_for_question); // #2561: for, ?:
        TEST_CASE(array_index_for_andand_oror);  // FN: using && or || in the for loop condition
        TEST_CASE(array_index_for_varid0);   // #4228: No varid for counter variable
        TEST_CASE(array_index_vla_for);      // #3221: access VLA inside for
        TEST_CASE(array_index_extern);       // FP when using 'extern'. #1684
        TEST_CASE(array_index_cast);         // FP after cast. #2841
        TEST_CASE(array_index_string_literal);
        TEST_CASE(array_index_same_struct_and_var_name); // #4751 - not handled well when struct name and var name is same
        TEST_CASE(array_index_valueflow);
        TEST_CASE(array_index_valueflow_pointer);
        TEST_CASE(array_index_function_parameter);
        TEST_CASE(array_index_enum_array); // #8439
        TEST_CASE(array_index_container); // #9386
        TEST_CASE(array_index_two_for_loops);
        TEST_CASE(array_index_new); // #7690

        TEST_CASE(buffer_overrun_2_struct);
        TEST_CASE(buffer_overrun_3);
        TEST_CASE(buffer_overrun_4);
        TEST_CASE(buffer_overrun_5);
        TEST_CASE(buffer_overrun_6);
        TEST_CASE(buffer_overrun_7);
        TEST_CASE(buffer_overrun_8);
        TEST_CASE(buffer_overrun_9);
        TEST_CASE(buffer_overrun_10);
        TEST_CASE(buffer_overrun_11);
        TEST_CASE(buffer_overrun_15); // ticket #1787
        TEST_CASE(buffer_overrun_16);
        TEST_CASE(buffer_overrun_18); // ticket #2576 - for, calculation with loop variable
        TEST_CASE(buffer_overrun_19); // #2597 - class member with unknown type
        TEST_CASE(buffer_overrun_21);
        TEST_CASE(buffer_overrun_24); // index variable is changed in for-loop
        TEST_CASE(buffer_overrun_26); // #4432 (segmentation fault)
        TEST_CASE(buffer_overrun_27); // #4444 (segmentation fault)
        TEST_CASE(buffer_overrun_29); // #7083: false positive: typedef and initialization with strings
        TEST_CASE(buffer_overrun_30); // #6367
        TEST_CASE(buffer_overrun_31);
        TEST_CASE(buffer_overrun_32); //#10244
        TEST_CASE(buffer_overrun_33); //#2019
        TEST_CASE(buffer_overrun_errorpath);
        TEST_CASE(buffer_overrun_bailoutIfSwitch);  // ticket #2378 : bailoutIfSwitch
        TEST_CASE(buffer_overrun_function_array_argument);
        TEST_CASE(possible_buffer_overrun_1); // #3035
        TEST_CASE(buffer_overrun_readSizeFromCfg);

        TEST_CASE(valueflow_string); // using ValueFlow string values in checking

        // It is undefined behaviour to point out of bounds of an array
        // the address beyond the last element is in bounds
        // char a[10];
        // char *p1 = a + 10;  // OK
        // char *p2 = a + 11   // UB
        TEST_CASE(pointer_out_of_bounds_1);
        TEST_CASE(pointer_out_of_bounds_2);
        TEST_CASE(pointer_out_of_bounds_3);
        TEST_CASE(pointer_out_of_bounds_4);
        TEST_CASE(pointer_out_of_bounds_sub);

        TEST_CASE(strcat1);

        TEST_CASE(varid1);
        TEST_CASE(varid2);  // ticket #4764

        TEST_CASE(assign1);

        TEST_CASE(alloc_new);      // Buffer allocated with new
        TEST_CASE(alloc_malloc);   // Buffer allocated with malloc
        TEST_CASE(alloc_string);   // statically allocated buffer
        TEST_CASE(alloc_alloca);   // Buffer allocated with alloca

        // TODO TEST_CASE(countSprintfLength);
        TEST_CASE(minsize_argvalue);
        TEST_CASE(minsize_sizeof);
        TEST_CASE(minsize_strlen);
        TEST_CASE(minsize_mul);
        TEST_CASE(unknownType);

        TEST_CASE(terminateStrncpy1);
        TEST_CASE(terminateStrncpy2);
        TEST_CASE(terminateStrncpy3);
        TEST_CASE(terminateStrncpy4);
        TEST_CASE(recursive_long_time);

        TEST_CASE(crash1);  // Ticket #1587 - crash
        TEST_CASE(crash2);  // Ticket #3034 - crash
        TEST_CASE(crash3);  // Ticket #5426 - crash
        TEST_CASE(crash4);  // Ticket #8679 - crash
        TEST_CASE(crash5);  // Ticket #8644 - crash
        TEST_CASE(crash6);  // Ticket #9024 - crash
        TEST_CASE(crash7);  // Ticket #9073 - crash

        TEST_CASE(insecureCmdLineArgs);
        TEST_CASE(checkBufferAllocatedWithStrlen);

        TEST_CASE(scope);   // handling different scopes

        TEST_CASE(getErrorMessages);

        // Access array and then check if the used index is within bounds
        TEST_CASE(arrayIndexThenCheck);
        TEST_CASE(arrayIndexEarlyReturn); // #6884

        TEST_CASE(bufferNotZeroTerminated);

        TEST_CASE(negativeMemoryAllocationSizeError); // #389
        TEST_CASE(negativeArraySize);

        TEST_CASE(pointerAddition1);

        TEST_CASE(ctu_malloc);
        TEST_CASE(ctu_array);
        TEST_CASE(ctu_variable);
        TEST_CASE(ctu_arithmetic);

        TEST_CASE(objectIndex);
    }


    void noerr1() {
        check("extern int ab;\n"
              "void f()\n"
              "{\n"
              "    if (ab)\n"
              "    {\n"
              "        char str[50];\n"
              "    }\n"
              "    if (ab)\n"
              "    {\n"
              "        char str[50];\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }


    void noerr2() {
        check("static char buf[2];\n"
              "void f1(char *str)\n"
              "{\n"
              "    strcpy(buf,str);\n"
              "}\n"
              "void f2(char *str)\n"
              "{\n"
              "    strcat(buf,str);\n"
              "}\n"
              "void f3(char *str)\n"
              "{\n"
              "    sprintf(buf,\"%s\",str);\n"
              "}\n"
              "void f4(const char str[])\n"
              "{\n"
              "    strcpy(buf, str);\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }


    void noerr3() {
        check("struct { char data[10]; } abc;\n"
              "static char f()\n"
              "{\n"
              "    char data[1];\n"
              "    return abc.data[1];\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }


    void noerr4() {
        // The memory isn't read or written and therefore there is no error.
        check("static void f() {\n"
              "    char data[100];\n"
              "    const char *p = data + 100;\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void sizeof3() {
        check("struct group { int gr_gid; };\n"
              "void f()\n"
              "{\n"
              "    char group[32];\n"
              "    snprintf(group, 32, \"%u\", 0);\n"
              "    struct group *gr;\n"
              "    snprintf(group, 32, \"%u\", gr->gr_gid);\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_1() {
        check("void f()\n"
              "{\n"
              "    char str[0x10] = {0};\n"
              "    str[15] = 0;\n"
              "    str[16] = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Array 'str[16]' accessed at index 16, which is out of bounds.\n", errout.str());

        check("char f()\n"
              "{\n"
              "    char str[16] = {0};\n"
              "    return str[16];\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4]: (error) Array 'str[16]' accessed at index 16, which is out of bounds.\n", errout.str());

        // test stack array
        check("int f()\n"
              "{\n"
              "   int x[ 3 ] = { 0, 1, 2 };\n"
              "   int y;\n"
              "   y = x[ 4 ];\n"
              "   return y;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Array 'x[3]' accessed at index 4, which is out of bounds.\n", errout.str());

        check("int f()\n"
              "{\n"
              "   int x[ 3 ] = { 0, 1, 2 };\n"
              "   int y;\n"
              "   y = x[ 2 ];\n"
              "   return y;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int x[5] = {0};\n"
              "int a = x[10];");
        ASSERT_EQUALS("[test.cpp:2]: (error) Array 'x[5]' accessed at index 10, which is out of bounds.\n", errout.str());

        check("int x[5] = {0};\n"
              "int a = (x)[10];");
        ASSERT_EQUALS("[test.cpp:2]: (error) Array 'x[5]' accessed at index 10, which is out of bounds.\n", errout.str());
    }


    void array_index_2() {
        check("void a(int i)\n" // valueflow
              "{\n"
              "    char *str = new char[0x10];\n"
              "    str[i] = 0;\n"
              "}\n"
              "void b() { a(16); }");
        ASSERT_EQUALS("[test.cpp:4]: (error) Array 'str[16]' accessed at index 16, which is out of bounds.\n", errout.str());
    }

    void array_index_4() {
        check("char c = \"abc\"[4];");
        ASSERT_EQUALS("[test.cpp:1]: (error) Array '\"abc\"[4]' accessed at index 4, which is out of bounds.\n", errout.str());

        check("p = &\"abc\"[4];");
        ASSERT_EQUALS("", errout.str());

        check("char c = \"\\0abc\"[2];");
        ASSERT_EQUALS("", errout.str());

        check("char c = L\"abc\"[4];");
        ASSERT_EQUALS("[test.cpp:1]: (error) Array 'L\"abc\"[4]' accessed at index 4, which is out of bounds.\n", errout.str());
    }

    void array_index_3() {
        check("void f()\n"
              "{\n"
              "    int val[50];\n"
              "    int i, sum=0;\n"
              "    for (i = 0; i < 100; i++)\n"
              "        sum += val[i];\n"
              "}");
        ASSERT_EQUALS("[test.cpp:6]: (error) Array 'val[50]' accessed at index 99, which is out of bounds.\n", errout.str());

        check("void f()\n"
              "{\n"
              "    int val[50];\n"
              "    int i, sum=0;\n"
              "    for (i = 1; i < 100; i++)\n"
              "        sum += val[i];\n"
              "}");
        ASSERT_EQUALS("[test.cpp:6]: (error) Array 'val[50]' accessed at index 99, which is out of bounds.\n", errout.str());

        check("void f(int a)\n"
              "{\n"
              "    int val[50];\n"
              "    int i, sum=0;\n"
              "    for (i = a; i < 100; i++)\n"
              "        sum += val[i];\n"
              "}");
        ASSERT_EQUALS("[test.cpp:6]: (error) Array 'val[50]' accessed at index 99, which is out of bounds.\n", errout.str());

        check("typedef struct g g2[3];\n"
              "void foo(char *a)\n"
              "{\n"
              "  for (int i = 0; i < 4; i++)\n"
              "  {\n"
              "    a[i]=0;\n"
              "  }\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(int argc)\n"
              "{\n"
              "  char a[2];\n"
              "  for (int i = 4; i < argc; i++){}\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void foo(int a[10]) {\n"
              "    for (int i=0;istr[1] = 0;"
              "}");
        ASSERT_EQUALS("", errout.str());

        // This is not out of bounds because it is not a variable length array
        check("struct ABC\n"
              "{\n"
              "    char str[1];\n"
              "    int x;\n"
              "};\n"
              "\n"
              "static void f()\n"
              "{\n"
              "    struct ABC* x = malloc(sizeof(struct ABC) + 10);\n"
              "    x->str[1] = 0;"
              "}");
        TODO_ASSERT_EQUALS("error", "", errout.str());

        // This is not out of bounds because it is a variable length array
        // and the index is within the memory allocated.
        /** @todo this works by accident because we ignore any access to this array */
        check("struct ABC\n"
              "{\n"
              "    char str[1];\n"
              "};\n"
              "\n"
              "static void f()\n"
              "{\n"
              "    struct ABC* x = malloc(sizeof(struct ABC) + 10);\n"
              "    x->str[10] = 0;"
              "}");
        ASSERT_EQUALS("", errout.str());

        // This is out of bounds because it is outside the memory allocated.
        /** @todo this doesn't work because of a bug in sizeof(struct)  */
        check("struct ABC\n"
              "{\n"
              "    char str[1];\n"
              "};\n"
              "\n"
              "static void f()\n"
              "{\n"
              "    struct ABC* x = malloc(sizeof(struct ABC) + 10);\n"
              "    x->str[11] = 0;"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:9]: (error) Array 'str[1]' accessed at index 11, which is out of bounds.\n", "", errout.str());

        // This is out of bounds if 'sizeof(ABC)' is 1 (No padding)
        check("struct ABC\n"
              "{\n"
              "    char str[1];\n"
              "};\n"
              "\n"
              "static void f()\n"
              "{\n"
              "    struct ABC* x = malloc(sizeof(ABC) + 10);\n"
              "    x->str[11] = 0;"
              "}");
        TODO_ASSERT_EQUALS("error", "", errout.str());

        // This is out of bounds because it is outside the memory allocated
        /** @todo this doesn't work because of a bug in sizeof(struct) */
        check("struct ABC\n"
              "{\n"
              "    char str[1];\n"
              "};\n"
              "\n"
              "static void f()\n"
              "{\n"
              "    struct ABC* x = malloc(sizeof(struct ABC));\n"
              "    x->str[1] = 0;"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:9]: (error) Array 'str[1]' accessed at index 1, which is out of bounds.\n", "", errout.str());

        // This is out of bounds because it is outside the memory allocated
        // But only if 'sizeof(ABC)' is 1 (No padding)
        check("struct ABC\n"
              "{\n"
              "    char str[1];\n"
              "};\n"
              "\n"
              "static void f()\n"
              "{\n"
              "    struct ABC* x = malloc(sizeof(ABC));\n"
              "    x->str[1] = 0;"
              "}");
        TODO_ASSERT_EQUALS("error", "", errout.str());

        // This is out of bounds because it is not a variable array
        check("struct ABC\n"
              "{\n"
              "    char str[1];\n"
              "};\n"
              "\n"
              "static void f()\n"
              "{\n"
              "    struct ABC x;\n"
              "    x.str[1] = 0;"
              "}");
        ASSERT_EQUALS("[test.cpp:9]: (error) Array 'x.str[1]' accessed at index 1, which is out of bounds.\n", errout.str());

        check("struct foo\n"
              "{\n"
              "    char str[10];\n"
              "};\n"
              "\n"
              "void x()\n"
              "{\n"
              "    foo f;\n"
              "    for ( unsigned int i = 0; i < 64; ++i )\n"
              "        f.str[i] = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:10]: (error) Array 'f.str[10]' accessed at index 63, which is out of bounds.\n", errout.str());

        check("struct AB { char a[NUM]; char b[NUM]; }\n"
              "void f(struct AB *ab) {\n"
              "    ab->a[0] = 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("union { char a[1]; int b; } ab;\n"
              "void f() {\n"
              "    ab.a[2] = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (error) Array 'ab.a[1]' accessed at index 2, which is out of bounds.\n", errout.str());
    }


    void array_index_7() {
        check("struct ABC\n"
              "{\n"
              "    char str[10];\n"
              "};\n"
              "\n"
              "static void f(struct ABC *abc)\n"
              "{\n"
              "    abc->str[10] = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:8]: (error) Array 'abc->str[10]' accessed at index 10, which is out of bounds.\n", errout.str());
    }

    void array_index_11() {
        check("class ABC\n"
              "{\n"
              "public:\n"
              "    ABC();\n"
              "    char *str[10];\n"
              "    struct ABC *next();\n"
              "};\n"
              "\n"
              "static void f(ABC *abc1)\n"
              "{\n"
              "    for ( ABC *abc = abc1; abc; abc = abc->next() )\n"
              "    {\n"
              "        abc->str[10] = 0;\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("[test.cpp:13]: (error) Array 'abc->str[10]' accessed at index 10, which is out of bounds.\n", errout.str());
    }

    void array_index_12() {
        check("class Fred\n"
              "{\n"
              "private:\n"
              "    char str[10];\n"
              "public:\n"
              "    Fred();\n"
              "};\n"
              "Fred::Fred()\n"
              "{\n"
              "    str[10] = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:10]: (error) Array 'str[10]' accessed at index 10, which is out of bounds.\n", errout.str());

        check("class Fred\n"
              "{\n"
              "private:\n"
              "    char str[10];\n"
              "public:\n"
              "    char c();\n"
              "};\n"
              "char Fred::c()\n"
              "{\n"
              "    return str[10];\n"
              "}");
        ASSERT_EQUALS("[test.cpp:10]: (error) Array 'str[10]' accessed at index 10, which is out of bounds.\n", errout.str());
    }

    void array_index_13() {
        check("void f()\n"
              "{\n"
              "    char buf[10];\n"
              "    for (int i = 0; i < 100; i++)\n"
              "    {\n"
              "        if (i < 10)\n"
              "            int x = buf[i];\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_14() {
        check("void f()\n"
              "{\n"
              "    int a[10];\n"
              "    for (int i = 0; i < 10; i++)\n"
              "        a[i+10] = i;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Array 'a[10]' accessed at index 19, which is out of bounds.\n", errout.str());
    }

    void array_index_15() {
        check("void f()\n"
              "{\n"
              "    int a[10];\n"
              "    for (int i = 0; i < 10; i++)\n"
              "        a[10+i] = i;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Array 'a[10]' accessed at index 19, which is out of bounds.\n", errout.str());
    }

    void array_index_16() {
        check("void f()\n"
              "{\n"
              "    int a[10];\n"
              "    for (int i = 0; i < 10; i++)\n"
              "        a[i+1] = i;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Array 'a[10]' accessed at index 10, which is out of bounds.\n", errout.str());
    }

    void array_index_17() {
        check("void f()\n"
              "{\n"
              "    int a[10];\n"
              "    for (int i = 0; i < 10; i++)\n"
              "        a[i*2] = i;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Array 'a[10]' accessed at index 18, which is out of bounds.\n", errout.str());

        check("void f()\n"
              "{\n"
              "    int a[12];\n"
              "    for (int i = 0; i < 12; i+=6)\n"
              "        a[i+5] = i;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f()\n"
              "{\n"
              "    int a[12];\n"
              "    for (int i = 0; i < 12; i+=6)\n"
              "        a[i+6] = i;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5]: (error) Array 'a[12]' accessed at index 12, which is out of bounds.\n", errout.str());

        check("void f() {\n"  // #4398
              "    int a[2];\n"
              "    for (int i = 0; i < 4; i+=2)\n"
              "        a[i] = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4]: (error) Array 'a[2]' accessed at index 2, which is out of bounds.\n", errout.str());

        check("void f() {\n"  // #4398
              "    int a[2];\n"
              "    for (int i = 0; i < 4; i+=2)\n"
              "        do_stuff(a+i);\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_18() {
        check("void f()\n"
              "{\n"
              "    int a[5];\n"
              "    for (int i = 0; i < 6; i++)\n"
              "    {\n"
              "        a[i] = i;\n"
              "        i+=1;\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f()\n"
              "{\n"
              "    int a[5];\n"
              "    for (int i = 0; i < 6; i++)\n"
              "    {\n"
              "        a[i] = i;\n"
              "        i++;\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f()\n"
              "{\n"
              "    int a[5];\n"
              "    for (int i = 0; i < 6; i++)\n"
              "    {\n"
              "        a[i] = i;\n"
              "        ++i;\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f()\n"
              "{\n"
              "    int a[5];\n"
              "    for (int i = 0; i < 6; i++)\n"
              "    {\n"
              "        a[i] = i;\n"
              "        i=4;\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f()\n"
              "{\n"
              "    int a[6];\n"
              "    for (int i = 0; i < 7; i++)\n"
              "    {\n"
              "        a[i] = i;\n"
              "        i+=1;\n"
              "    }\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Buffer overrun\n", "", errout.str());
    }

    void array_index_19() {
        // "One Past the End" is legal, as long as pointer is not dereferenced.
        check("void f()\n"
              "{\n"
              "  char a[2];\n"
              "  char *end = &(a[2]);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // Getting more than one past the end is not legal
        check("void f()\n"
              "{\n"
              "  char a[2];\n"
              "  char *end = &(a[3]);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4]: (error) Array 'a[2]' accessed at index 3, which is out of bounds.\n", errout.str());
    }

    void array_index_20() {
        check("void f()\n"
              "{\n"
              " char a[8];\n"
              " int b[10];\n"
              " for ( int i = 0; i < 9; i++ )\n"
              "  b[i] = 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_21() {
        check("class A {\n"
              " int indices[2];\n"
              " void foo(int indices[3]);\n"
              "};\n"
              "\n"
              "void A::foo(int indices[3]) {\n"
              " for(int j=0; jb[10][2] = 4;\n"
              "    ptest->b[0][19] = 4;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:9]: (error) Array 'test.a[10]' accessed at index 10, which is out of bounds.\n"
                      "[test.cpp:10]: (error) Array 'test.b[10][5]' accessed at index test.b[10][2], which is out of bounds.\n"
                      "[test.cpp:11]: (error) Array 'test.b[10][5]' accessed at index test.b[0][19], which is out of bounds.\n"
                      "[test.cpp:14]: (error) Array 'ptest->a[10]' accessed at index 10, which is out of bounds.\n"
                      "[test.cpp:15]: (error) Array 'ptest->b[10][5]' accessed at index ptest->b[10][2], which is out of bounds.\n"
                      "[test.cpp:16]: (error) Array 'ptest->b[10][5]' accessed at index ptest->b[0][19], which is out of bounds.\n", errout.str());

        check("struct TEST\n"
              "{\n"
              "    char a[10][5];\n"
              "};\n"
              "void foo()\n"
              "{\n"
              "    TEST test;\n"
              "    test.a[9][5] = 4;\n"
              "    test.a[0][50] = 4;\n"
              "    TEST *ptest;\n"
              "    ptest = &test;\n"
              "    ptest->a[9][5] = 4;\n"
              "    ptest->a[0][50] = 4;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:8]: (error) Array 'test.a[10][5]' accessed at index test.a[9][5], which is out of bounds.\n"
                      "[test.cpp:9]: (error) Array 'test.a[10][5]' accessed at index test.a[0][50], which is out of bounds.\n"
                      "[test.cpp:12]: (error) Array 'ptest->a[10][5]' accessed at index ptest->a[9][5], which is out of bounds.\n"
                      "[test.cpp:13]: (error) Array 'ptest->a[10][5]' accessed at index ptest->a[0][50], which is out of bounds.\n", errout.str());
    }

    void array_index_35() { // ticket #2889
        check("void f() {\n"
              "    struct Struct { unsigned m_Var[1]; } s;\n"
              "    s.m_Var[1] = 1;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (error) Array 's.m_Var[1]' accessed at index 1, which is out of bounds.\n", errout.str());

        check("struct Struct { unsigned m_Var[1]; };\n"
              "void f() {\n"
              "    struct Struct s;\n"
              "    s.m_Var[1] = 1;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4]: (error) Array 's.m_Var[1]' accessed at index 1, which is out of bounds.\n", errout.str());

        check("struct Struct { unsigned m_Var[1]; };\n"
              "void f() {\n"
              "    struct Struct * s = calloc(40);\n"
              "    s->m_Var[1] = 1;\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_36() { // ticket #2960
        check("class Fred {\n"
              "    Fred(const Fred &);\n"
              "private:\n"
              "    bool m_b[2];\n"
              "};\n"
              "Fred::Fred(const Fred & rhs) {\n"
              "    m_b[2] = rhs.m_b[2];\n"
              "}");
        ASSERT_EQUALS("[test.cpp:7]: (error) Array 'm_b[2]' accessed at index 2, which is out of bounds.\n"
                      "[test.cpp:7]: (error) Array 'rhs.m_b[2]' accessed at index 2, which is out of bounds.\n", errout.str());
    }

    void array_index_37() {
        check("class Fred {\n"
              "    char x[X];\n"
              "    Fred() {\n"
              "        for (unsigned int i = 0; i < 15; i++)\n"
              "            i;\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_38() { //ticket #3273
        check("void aFunction() {\n"
              "    double aDoubleArray[ 10 ];\n"
              "    unsigned int i; i = 0;\n"
              "    for( i = 0; i < 6; i++ )\n"
              "    {\n"
              "        unsigned int j; j = 0;\n"
              "        for( j = 0; j < 5; j++ )\n"
              "        {\n"
              "            unsigned int x; x = 0;\n"
              "            for( x = 0; x < 4; x++ )\n"
              "            {\n"
              "            }\n"
              "        }\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_39() { // ticket 3387
        check("void aFunction()\n"
              "{\n"
              "    char a[10];\n"
              "    a[10] = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4]: (error) Array 'a[10]' accessed at index 10, which is out of bounds.\n", errout.str());
    }

    void array_index_40() {
        check("void f() {\n"
              "    char a[10];\n"
              "    for (int i = 0; i < 10; ++i)\n"
              "        f2(&a[i + 1]);\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_41() {
        // Don't generate false positives when structs have the same name
        check("void a() {\n"
              "    struct Fred { char data[6]; } fred;\n"
              "    fred.data[4] = 0;\n"  //  no error
        check("void f1(char *buf) {\n"
              "    buf[4] = 0;\n"
              "}\n"
              "void f2() {\n"
              "    int x[2];\n"
              "    f1(x);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // Same type => error
        check("void f1(const char buf[]) {\n"
              "    char c = buf[4];\n"
              "}\n"
              "void f2() {\n"
              "    char x[2];\n"
              "    f1(x);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (error) Array 'x[2]' accessed at index 4, which is out of bounds.\n",
                           "",
                           errout.str());
    }

    void array_index_string_literal() {
        check("void f() {\n"
              "    const char *str = \"abc\";\n"
              "    bar(str[10]);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:3]: (error) Array 'str[4]' accessed at index 10, which is out of bounds.\n", errout.str());

        check("void f()\n"
              "{\n"
              "    const char *str = \"abc\";\n"
              "    bar(str[4]);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4]: (error) Array 'str[4]' accessed at index 4, which is out of bounds.\n", errout.str());

        check("void f()\n"
              "{\n"
              "    const char *str = \"abc\";\n"
              "    bar(str[3]);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f()\n"
              "{\n"
              "    const char *str = \"a\tc\";\n"
              "    bar(str[4]);\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4]: (error) Array 'str[4]' accessed at index 4, which is out of bounds.\n", errout.str());

        check("void f() {\n" // #6973
              "    const char *name = \"\";\n"
              "    if ( name[0] == 'U' ? name[1] : 0) {}\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int main(int argc, char **argv) {\n"
              "    char str[6] = \"\\0\";\n"
              "    unsigned short port = 65535;\n"
              "    snprintf(str, sizeof(str), \"%hu\", port);\n"
              "}", settings0, "test.c");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_same_struct_and_var_name() {
        // don't throw internal error
        check("struct tt {\n"
              "    char name[21];\n"
              "} ;\n"
              "void doswitch(struct tt *x)\n"
              "{\n"
              "    struct tt *tt=x;\n"
              "    tt->name;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // detect error
        check("struct tt {\n"
              "    char name[21];\n"
              "} ;\n"
              "void doswitch(struct tt *x)\n"
              "{\n"
              "    struct tt *tt=x;\n"
              "    tt->name[22] = 123;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:7]: (error) Array 'tt->name[21]' accessed at index 22, which is out of bounds.\n", errout.str());
    }

    void array_index_valueflow() {
        check("void f(int i) {\n"
              "    char str[3];\n"
              "    str[i] = 0;\n"
              "    if (i==10) {}\n"
              "}");
        ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:3]: (warning) Either the condition 'i==10' is redundant or the array 'str[3]' is accessed at index 10, which is out of bounds.\n", errout.str());

        check("void f(int i) {\n"
              "    char str[3];\n"
              "    str[i] = 0;\n"
              "    switch (i) {\n"
              "    case 10: break;\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:3]: (warning) Either the switch case 'case 10' is redundant or the array 'str[3]' is accessed at index 10, which is out of bounds.\n", errout.str());

        check("void f() {\n"
              "    char str[3];\n"
              "    str[((unsigned char)3) - 1] = 0;\n"
              "}", "test.cpp");
        ASSERT_EQUALS("", errout.str());

        check("void f() {\n"  // #5416 FP
              "    char *str[3];\n"
              "    do_something(&str[0][5]);\n"
              "}", "test.cpp");
        ASSERT_EQUALS("", errout.str());

        check("class X { static const int x[100]; };\n" // #6070
              "const int X::x[100] = {0};");
        ASSERT_EQUALS("", errout.str());

        check("namespace { class X { static const int x[100]; };\n" // #6232
              "const int X::x[100] = {0}; }");
        ASSERT_EQUALS("", errout.str());

        check("class ActorSprite { static ImageSet * targetCursorImages[2][10]; };\n"
              "ImageSet *ActorSprite::targetCursorImages[2][10];");
        ASSERT_EQUALS("", errout.str());

    }

    void array_index_valueflow_pointer() {
        check("void f() {\n"
              "  int a[10];\n"
              "  int *p = a;\n"
              "  p[20] = 0;\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Array 'a[10]' accessed at index 20, which is out of bounds.\n", "", errout.str());

        {
            // address of
            check("void f() {\n"
                  "  int a[10];\n"
                  "  int *p = a;\n"
                  "  p[10] = 0;\n"
                  "}");
            TODO_ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (error) Array 'a[10]' accessed at index 10, which is out of bounds.\n", "", errout.str());

            check("void f() {\n"
                  "  int a[10];\n"
                  "  int *p = a;\n"
                  "  dostuff(&p[10]);\n"
                  "}");
            ASSERT_EQUALS("", errout.str());
        }

        check("void f() {\n"
              "  int a[X];\n" // unknown size
              "  int *p = a;\n"
              "  p[20] = 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f() {\n"
              "  int a[2];\n"
              "  char *p = (char *)a;\n" // cast
              "  p[4] = 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void array_index_function_parameter() {
        check("void f(char a[10]) {\n"
              "  a[20] = 0;\n" // m[9]' accessed at index 36, which is out of bounds.\n", errout.str());
    }

    void buffer_overrun_31() {
        check("void f(WhereInfo *pWInfo, int *aiCur) {\n"
              "  memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void buffer_overrun_32() {
        // destination size is too small
        check("void f(void) {\n"
              "    const char src[3] = \"abc\";\n"
              "    char dest[1] = \"a\";\n"
              "    (void)strxfrm(dest,src,1);\n"
              "    (void)strxfrm(dest,src,2);\n"// 1)\n"
              "    {\n"
              "        char buf[2];\n"
              "        char *p = strdup(argv[1]);\n"
              "        strcpy(buf,p);\n"
              "        free(p);\n"
              "    }\n"
              "    return 0;\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, char *argv[])\n"
              "{\n"
              "    if(argc>1)\n"
              "    {\n"
              "        char buf[2] = {'\\0','\\0'};\n"
              "        char *p = strdup(argv[1]);\n"
              "        strcat(buf,p);\n"
              "        free(p);\n"
              "    }\n"
              "    return 0;\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:7]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(const int argc, char* argv[])\n"
              "{\n"
              "    char prog[10];\n"
              "    strcpy(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, const char* argv[])\n"
              "{\n"
              "    char prog[10];\n"
              "    strcpy(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(const int argc, const char* argv[])\n"
              "{\n"
              "    char prog[10];\n"
              "    strcpy(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, char* argv[])\n"
              "{\n"
              "    char prog[10] = {'\\0'};\n"
              "    strcat(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, char **argv, char **envp)\n"
              "{\n"
              "    char prog[10];\n"
              "    strcpy(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, const char *const *const argv, char **envp)\n"
              "{\n"
              "    char prog[10];\n"
              "    strcpy(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(const int argc, const char *const *const argv, const char *const *const envp)\n"
              "{\n"
              "    char prog[10];\n"
              "    strcpy(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, char **argv, char **envp)\n"
              "{\n"
              "    char prog[10] = {'\\0'};\n"
              "    strcat(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(const int argc, const char **argv, char **envp)\n"
              "{\n"
              "    char prog[10] = {'\\0'};\n"
              "    strcat(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, const char **argv, char **envp)\n"
              "{\n"
              "    char prog[10] = {'\\0'};\n"
              "    strcat(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(const int argc, char **argv, char **envp)\n"
              "{\n"
              "    char prog[10] = {'\\0'};\n"
              "    strcat(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, char **options)\n"
              "{\n"
              "    char prog[10];\n"
              "    strcpy(prog, options[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, char **options)\n"
              "{\n"
              "    char prog[10] = {'\\0'};\n"
              "    strcat(prog, options[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, char **options)\n"
              "{\n"
              "    char prog[10];\n"
              "    strcpy(prog, *options);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, char **options)\n"
              "{\n"
              "    char prog[10];\n"
              "    strcpy(prog+3, *options);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        check("int main(int argc, char **argv, char **envp)\n"
              "{\n"
              "    char prog[10];\n"
              "    if (strlen(argv[0]) < 10)\n"
              "        strcpy(prog, argv[0]);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int main(int argc, char **argv, char **envp)\n"
              "{\n"
              "    char prog[10] = {'\\0'};\n"
              "    if (10 > strlen(argv[0]))\n"
              "        strcat(prog, argv[0]);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int main(int argc, char **argv, char **envp)\n"
              "{\n"
              "    char prog[10];\n"
              "    argv[0][0] = '\\0';\n"
              "    strcpy(prog, argv[0]);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // #5835
        check("int main(int argc, char* argv[]) {\n"
              "    char prog[10];\n"
              "    strcpy(prog, argv[0]);\n"
              "    strcpy(prog, argv[0]);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Buffer overrun possible for long command line arguments.\n"
                           "[test.cpp:4]: (error) Buffer overrun possible for long command line arguments.\n", "", errout.str());

        // #7964
        check("int main(int argc, char *argv[]) {\n"
              "  char *strcpy();\n"
              "}");
        ASSERT_EQUALS("", errout.str());
        check("int main(int argc, char *argv[]) {\n"
              "  char *strcat();\n"
              "}");
        ASSERT_EQUALS("", errout.str());
    }

    void checkBufferAllocatedWithStrlen() {
        check("void f(char *a) {\n"
              "  char *b = new char[strlen(a)];\n"
              "  strcpy(b, a);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds.\n", "", errout.str());

        check("void f(char *a) {\n"
              "  char *b = new char[strlen(a) + 1];\n"
              "  strcpy(b, a);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f(char *a) {\n"
              "  char *b = new char[strlen(a)];\n"
              "  a[0] = '\\0';\n"
              "  strcpy(b, a);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f(char *a) {\n"
              "  char *b = (char *)malloc(strlen(a));\n"
              "  b = realloc(b, 10000);\n"
              "  strcpy(b, a);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f(char *a) {\n"
              "  char *b = (char *)malloc(strlen(a));\n"
              "  strcpy(b, a);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds.\n", "", errout.str());

        check("void f(char *a) {\n"
              "  char *b = (char *)malloc(strlen(a));\n"
              "  {\n"
              "    strcpy(b, a);\n"
              "  }\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Buffer is accessed out of bounds.\n", "", errout.str());

        check("void f(char *a) {\n"
              "  char *b = (char *)malloc(strlen(a) + 1);\n"
              "  strcpy(b, a);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f(char *a, char *c) {\n"
              "  char *b = (char *)realloc(c, strlen(a));\n"
              "  strcpy(b, a);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds.\n", "", errout.str());

        check("void f(char *a, char *c) {\n"
              "  char *b = (char *)realloc(c, strlen(a) + 1);\n"
              "  strcpy(b, a);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f(char *a) {\n"
              "  char *b = (char *)malloc(strlen(a));\n"
              "  strcpy(b, a);\n"
              "}");
        TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Buffer is accessed out of bounds.\n", "", errout.str());
    }

    void scope() {
        check("class A {\n"
              "private:\n"
              "    struct X { char buf[10]; };\n"
              "};\n"
              "\n"
              "void f()\n"
              "{\n"
              "    X x;\n"
              "    x.buf[10] = 0;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("class A {\n"
              "public:\n"
              "    struct X { char buf[10]; };\n"
              "};\n"
              "\n"
              "void f()\n"
              "{\n"
              "    A::X x;\n"
              "    x.buf[10] = 0;\n"
              "}");
        ASSERT_EQUALS("[test.cpp:9]: (error) Array 'x.buf[10]' accessed at index 10, which is out of bounds.\n", errout.str());
    }

    void getErrorMessages() {
        // Ticket #2292: segmentation fault when using --errorlist
        CheckBufferOverrun c;
        c.getErrorMessages(this, nullptr);
    }

    void arrayIndexThenCheck() {
        // extracttests.start: volatile int y;

        check("void f(const char s[]) {\n"
              "    if (s[i] == 'x' && i < y) {\n"
              "    }"
              "}");
        ASSERT_EQUALS("", errout.str()); // No message because i is unknown and thus gets no varid. Avoid an internalError here.

        check("void f(const char s[], int i) {\n"
              "    if (s[i] == 'x' && i < y) {\n"
              "    }"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (style) Array index 'i' is used before limits check.\n", errout.str());

        check("void f(const char s[]) {\n"
              "    for (int i = 0; s[i] == 'x' && i < y; ++i) {\n"
              "    }"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (style) Array index 'i' is used before limits check.\n", errout.str());

        check("void f(const int a[], unsigned i) {\n"
              "    if((a[i] < 2) && (i = i)) {\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("[test.cpp:2]: (style) Array index 'i' is used before limits check.\n", errout.str());

        // extracttests.start: int elen;
        check("void f(char* e, int y) {\n"
              "    if (e[y] == '/' && elen > y + 1 && e[y + 1] == '?') {\n"
              "    }\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        // extracttests.start: int foo(int); int func(int);
        check("void f(const int a[], unsigned i) {\n"
              "    if(a[i] < func(i) && i  [test.cpp:7] -> [test.cpp:2]: (error) Array index out of bounds; buffer 'p' is accessed at offset -3.\n", errout.str());

        ctu("void dostuff(char *p) {\n"
            "  p[4] = 0;\n"
            "}\n"
            "\n"
            "int main() {\n"
            "  char *s = malloc(4);\n"
            "  dostuff(s);\n"
            "}");
        ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:7] -> [test.cpp:2]: (error) Array index out of bounds; 'p' buffer size is 4 and it is accessed at offset 4.\n", errout.str());
    }

    void ctu_array() {
        ctu("void dostuff(char *p) {\n"
            "    p[10] = 0;\n"
            "}\n"
            "int main() {\n"
            "  char str[4];\n"
            "  dostuff(str);\n"
            "}");
        ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (error) Array index out of bounds; 'p' buffer size is 4 and it is accessed at offset 10.\n", errout.str());

        ctu("static void memclr( char *data )\n"
            "{\n"
            "    data[10] = 0;\n"
            "}\n"
            "\n"
            "static void f()\n"
            "{\n"
            "    char str[5];\n"
            "    memclr( str );\n"
            "}");
        ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (error) Array index out of bounds; 'data' buffer size is 5 and it is accessed at offset 10.\n", errout.str());

        ctu("static void memclr( int i, char *data )\n"
            "{\n"
            "    data[10] = 0;\n"
            "}\n"
            "\n"
            "static void f()\n"
            "{\n"
            "    char str[5];\n"
            "    memclr( 0, str );\n"
            "}");
        ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (error) Array index out of bounds; 'data' buffer size is 5 and it is accessed at offset 10.\n", errout.str());

        ctu("static void memclr( int i, char *data )\n"
            "{\n"
            "    data[i] = 0;\n"
            "}\n"
            "\n"
            "static void f()\n"
            "{\n"
            "    char str[5];\n"
            "    memclr( 10, str );\n"
            "}");
        TODO_ASSERT_EQUALS("[test.cpp:9] -> [test.cpp:3]: (possible error) Array index out of bounds.\n",
                           "", errout.str());

        // This is not an error
        ctu("static void memclr( char *data, int size )\n"
            "{\n"
            "    if( size > 10 )"
            "      data[10] = 0;\n"
            "}\n"
            "\n"
            "static void f()\n"
            "{\n"
            "    char str[5];\n"
            "    memclr( str, 5 );\n"
            "}");
        ASSERT_EQUALS("", errout.str());

        // #2097
        ctu("void foo(int *p)\n"
            "{\n"
            "    --p;\n"
            "    p[2] = 0;\n"
            "}\n"
            "\n"
            "void bar()\n"
            "{\n"
            "    int p[3];\n"
            "    foo(p+1);\n"
            "}");
        ASSERT_EQUALS("", errout.str());

        // #9112
        ctu("static void get_mac_address(const u8 *strbuf)\n"
            "{\n"
            "    (strbuf[2]);\n"
            "}\n"
            "\n"
            "static void program_mac_address(u32 mem_base)\n"
            "{\n"
            "    u8 macstrbuf[17] = { 0 };\n"
            "    get_mac_address(macstrbuf);\n"
            "}");
        ASSERT_EQUALS("", errout.str());
    }

    void ctu_variable() {
        ctu("void dostuff(int *p) {\n"
            "    p[10] = 0;\n"
            "}\n"
            "int main() {\n"
            "  int x = 4;\n"
            "  dostuff(&x);\n"
            "}");
        ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:2]: (error) Array index out of bounds; 'p' buffer size is 4 and it is accessed at offset 40.\n", errout.str());
    }

    void ctu_arithmetic() {
        ctu("void dostuff(int *p) { x = p + 10; }\n"
            "int main() {\n"
            "  int x[3];\n"
            "  dostuff(x);\n"
            "}");
        ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (error) Pointer arithmetic overflow; 'p' buffer size is 12\n", errout.str());
    }

    void objectIndex() {
        check("int f() {\n"
              "    int i;\n"
              "    return (&i)[1];\n"
              "}");
        ASSERT_EQUALS(
            "[test.cpp:3] -> [test.cpp:3]: (error) The address of local variable 'i' is accessed at non-zero index.\n",
            errout.str());

        check("int f(int j) {\n"
              "    int i;\n"
              "    return (&i)[j];\n"
              "}");
        ASSERT_EQUALS(
            "[test.cpp:3] -> [test.cpp:3]: (warning) The address of local variable 'i' might be accessed at non-zero index.\n",
            errout.str());

        check("int f() {\n"
              "    int i;\n"
              "    return (&i)[0];\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int f(int * i) {\n"
              "    return i[1];\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int f(std::vector i) {\n"
              "    return i[1];\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int f(std::vector i) {\n"
              "    return i.data()[1];\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int* f(std::vector& i) {\n"
              "    return &(i[1]);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("struct A { int i; int j; };\n"
              "int f() {\n"
              "    A x;\n"
              "    return (&x.i)[0];\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("struct A { int i; int j; };\n"
              "int f() {\n"
              "    A x;\n"
              "    int * i = &x.i;\n"
              "    return i[0];\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void f() {\n"
              "  int x = 0;\n"
              "  std::map m;\n"
              "  m[0] = &x;\n"
              "  m[1] = &x;\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("int f() {\n"
              "  int x = 0;\n"
              "  std::map m;\n"
              "  m[0] = &x;\n"
              "  return m[0][1];\n"
              "}");
        ASSERT_EQUALS(
            "[test.cpp:4] -> [test.cpp:5]: (error) The address of local variable 'x' is accessed at non-zero index.\n",
            errout.str());

        check("int f(int * y) {\n"
              "  int x = 0;\n"
              "  std::map m;\n"
              "  m[0] = &x;\n"
              "  m[1] = y;\n"
              "  return m[1][1];\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("void print(char** test);\n"
              "int main(){\n"
              "    char* test = \"abcdef\";\n"
              "    print(&test);\n"
              "    return 0;\n"
              "}\n"
              "void print(char** test){\n"
              "    for(int i=0;i [test.cpp:4] -> [test.cpp:9]: (warning) The address of local variable 'test' might be accessed at non-zero index.\n",
            "",
            errout.str());

        check("void Bar(uint8_t data);\n"
              "void Foo(const uint8_t * const data, const uint8_t length) {\n"
              "        for(uint8_t index = 0U; index < length ; ++index)\n"
              "            Bar(data[index]);\n"
              "}\n"
              "void test() {\n"
              "    const uint8_t data = 0U;\n"
              "    Foo(&data,1U);\n"
              "}\n");
        ASSERT_EQUALS("", errout.str());

        check("int foo(int n, int* p) {\n"
              "    int res = 0;\n"
              "    for(int i = 0; i < n; i++ )\n"
              "        res += p[i];\n"
              "    return res;\n"
              "}\n"
              "int bar() {\n"
              "    int single_value = 0;\n"
              "    return foo(1, &single_value);\n"
              "}\n");
        ASSERT_EQUALS("", errout.str());

        check("void f(const char* app, size_t applen) {\n" // #10137
              "    char* tmp_de = NULL;\n"
              "    char** str = &tmp_de;\n"
              "    char* tmp = (char*)realloc(*str, applen + 1);\n"
              "    if (tmp) {\n"
              "        *str = tmp;\n"
              "        memcpy(*str, app, applen);\n"
              "        (*str)[applen] = '\\0';\n"
              "    }\n"
              "    free(*str);\n"
              "}\n", "test.c");
        ASSERT_EQUALS("", errout.str());

        check("template \n"
              "using vector = Eigen::Matrix;\n"
              "template \n"
              "void scharr(image2d& out) {\n"
              "    vector* out_row = &out(r, 0);\n"
              "    out_row[c] = vector(1,2);\n"
              "}\n");
        ASSERT_EQUALS("", errout.str());

        check("void f(const uint8_t* d, const uint8_t L) {\n" // #10092
              "    for (uint8_t i = 0U; i < L; ++i)\n"
              "        g(d[i]);\n"
              "}\n"
              "void h() {\n"
              "    const uint8_t u = 4;\n"
              "    f(&u, N);\n"
              "}");
        ASSERT_EQUALS("", errout.str());

        check("uint32_t f(uint32_t u) {\n" // #10154
              "    return ((uint8_t*)&u)[3];\n"
              "}\n");
        ASSERT_EQUALS("", errout.str());

        check("uint32_t f(uint32_t u) {\n"
              "    return ((uint8_t*)&u)[4];\n"
              "}\n");
        ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (error) The address of local variable 'u' is accessed at non-zero index.\n", errout.str());

        check("uint32_t f(uint32_t u) {\n"
              "    return reinterpret_cast(&u)[3];\n"
              "}\n");
        ASSERT_EQUALS("", errout.str());

        check("uint32_t f(uint32_t u) {\n"
              "    return reinterpret_cast(&u)[4];\n"
              "}\n");
        ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (error) The address of local variable 'u' is accessed at non-zero index.\n", errout.str());

        check("uint32_t f(uint32_t u) {\n"
              "    uint8_t* p = (uint8_t*)&u;\n"
              "    return p[3];\n"
              "}\n");
        ASSERT_EQUALS("", errout.str());

        check("uint32_t f(uint32_t u) {\n"
              "    uint8_t* p = (uint8_t*)&u;\n"
              "    return p[4];\n"
              "}\n");
        ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) The address of local variable 'u' is accessed at non-zero index.\n", errout.str());

        check("uint32_t f(uint32_t* pu) {\n"
              "    uint8_t* p = (uint8_t*)pu;\n"
              "    return p[4];\n"
              "}\n");
        ASSERT_EQUALS("", errout.str());

        check("struct S { uint8_t padding[500]; };\n" // #10133
              "S s = { 0 };\n"
              "uint8_t f() {\n"
              "    uint8_t* p = (uint8_t*)&s;\n"
              "    return p[10];\n"
              "}\n");
        ASSERT_EQUALS("", errout.str());
    }
};

REGISTER_TEST(TestBufferOverrun)

Web Proxy Viewer  |  New URL  |  Original Page