Menu

possible bug

2023-08-18
2023-08-19
  • Jason Reich

    Jason Reich - 2023-08-18

    I'm using the example code from the manual (the section that covers how to make custom output usign --template and --template-location. Never mind the ~~::~~ characters or the key/value pair pattern I put in the output... both have nothing to do with this.

    The thing that surprised me was when I ran cppcheck without --cppcheck-build-dir.... followed by running it twice using --cppcheck-build-dir (the second time with the cache in place).

    Below I will first show without --cppcheck-build-dir. Then the two runs with --cppcheck-build-dir.

    Does anybody else think that comparing, in turn, each of the two "with --cppcheck-build-dir" outputs to the "wihtout with --cppcheck-build-dir" output reveals some chaotic behavior?

    For example, why would the third to last line in Output A (the one without cppcheck-build-dir) have no code associated with it? And then, in Output C (the second run with cppcheck-build-dir), that line doesn't appear at all.

    Another example is: Why would Output B (the first run with cppcehck-build-dir) have so many extra lines that output A (the one without cppcheck-build-dir) doesn't have?

    Here are the details:

    Prerequisites:
    
    1. mkdir cppcheck-build 
    2. mkdir cpp-app
    3. Create the main.cpp file shown below within the cpp-app directory, then run the two cppcheck calls
    
    For this code:
    
    # cpp-app/main.cpp
    void f(int *p) { *p = 3; }
    
    int main() {
      int *p = 0;
      f(p);
      return 0;
    }
    
    The following cppcheck runs give the unexpected results shown below:
    
    cppcheck:
        cppcheck --enable=all --std=c++11 --language=c++ --template="file: {file}~~::~~line: {line}~~::~~severity: {severity}~~::~~message: {message}~~::~~code: {code}" --template-location="file: {file}~~::~~line: {line}~~::~~note: {info}~~::~~code: {code}" cpp-app/main.cpp
    
    output A:
    file: cpp-app/main.cpp~~::~~line: 1~~::~~severity: warning~~::~~message: Possible null pointer dereference: p~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 4~~::~~note: Assignment 'p=0', assigned value is 0~~::~~code:   int *p = 0;
               ^
    file: cpp-app/main.cpp~~::~~line: 5~~::~~note: Calling function 'f', 1st argument 'p' value is 0~~::~~code:   f(p);
        ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~note: Null pointer dereference~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~severity: error~~::~~message: Null pointer dereference: p~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 4~~::~~note: Assignment 'p=0', assigned value is 0~~::~~code: 
               ^
    file: cpp-app/main.cpp~~::~~line: 5~~::~~note: Calling function f, 1st argument is null~~::~~code:   f(p);
       ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~note: Dereferencing argument p that is null~~::~~code: void f(int *p) { *p = 3; }
    
    
    
    Cppcheck with -cppcheck-build-dir:
        cppcheck --cppcheck-build-dir=cppcheck-build --enable=all --std=c++11 --language=c++ --template="file: {file}~~::~~line: {line}~~::~~severity: {severity}~~::~~message: {message}~~::~~code: {code}" --template-location="file: {file}~~::~~line: {line}~~::~~note: {info}~~::~~code: {code}" cpp-app/main.cpp
    
    output B:
    file: cpp-app/main.cpp~~::~~line: 1~~::~~severity: warning~~::~~message: Possible null pointer dereference: p~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 4~~::~~note: Assignment 'p=0', assigned value is 0~~::~~code:   int *p = 0;
               ^
    file: cpp-app/main.cpp~~::~~line: 5~~::~~note: Calling function 'f', 1st argument 'p' value is 0~~::~~code:   f(p);
        ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~note: Null pointer dereference~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~severity: error~~::~~message: Null pointer dereference: p~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 4~~::~~note: Assignment 'p=0', assigned value is 0~~::~~code: 
               ^
    file: cpp-app/main.cpp~~::~~line: 5~~::~~note: Calling function f, 1st argument is null~~::~~code:   f(p);
       ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~note: Dereferencing argument p that is null~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~severity: error~~::~~message: Null pointer dereference: p~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 5~~::~~note: Calling function f, 1st argument is null~~::~~code:   f(p);
       ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~note: Dereferencing argument p that is null~~::~~code: void f(int *p) { *p = 3; }
    
    
    Cppcheck with -cppcheck-build-dir (run it a second time now that results from first run are cached):
        cppcheck --cppcheck-build-dir=cppcheck-build --enable=all --std=c++11 --language=c++ --template="file: {file}~~::~~line: {line}~~::~~severity: {severity}~~::~~message: {message}~~::~~code: {code}" --template-location="file: {file}~~::~~line: {line}~~::~~note: {info}~~::~~code: {code}" cpp-app/main.cpp
    
    output C:
    file: cpp-app/main.cpp~~::~~line: 1~~::~~severity: warning~~::~~message: Possible null pointer dereference: p~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 4~~::~~note: Assignment 'p=0', assigned value is 0~~::~~code:   int *p = 0;
               ^
    file: cpp-app/main.cpp~~::~~line: 5~~::~~note: Calling function 'f', 1st argument 'p' value is 0~~::~~code:   f(p);
        ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~note: Null pointer dereference~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~severity: error~~::~~message: Null pointer dereference: p~~::~~code: void f(int *p) { *p = 3; }
                      ^
    file: cpp-app/main.cpp~~::~~line: 5~~::~~note: Calling function f, 1st argument is null~~::~~code:   f(p);
       ^
    file: cpp-app/main.cpp~~::~~line: 1~~::~~note: Dereferencing argument p that is null~~::~~code: void f(int *p) { *p = 3; }
    
     
  • CHR

    CHR - 2023-08-19

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/11883

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.