Questions tagged [cppcheck]

Cppcheck is an open source tool for static C/C++ code analysis that tries to detect bugs that a C/C++ compiler doesn't see.

Cppcheck - A tool for static C/C++ code analysis

Cppcheck is an analysis tool for C/C++ code. Unlike C/C++ compilers and many other analysis tools, we don't detect syntax errors. Cppcheck only detects the types of bugs that the compilers normally fail to detect. The goal is no false positives.

389 questions
12
votes
1 answer

Automatically detect identical consecutive std::string::find() calls

During a code review, i found source code like this: void f_odd(std::string &className, std::string &testName) { if (className.find("::") != std::string::npos) { testName = className.substr(className.find("::") + 2); …
orbitcowboy
  • 1,438
  • 13
  • 25
11
votes
3 answers

How could reading numbers using sscanf crash?

Cppcheck has detected a potential problem in a code like this: float a, b, c; int count = sscanf(data, "%f,%f,%f", &a, &b, &c); It says that: "scanf without field width limits can crash with huge data". How is that possible? Is that a known bug in…
Juraj Blaho
  • 13,301
  • 7
  • 50
  • 96
11
votes
3 answers

CppCheck. The scope of the variable can be reduced (and loop)

CppCheck finds me some findings like: "The scope of the variable 'x' can be reduced". What if I have this situation: int x; for (int i = 0; i != 10; ++i) { x = someFunction(); // ... I use x variable here } I think my code is OK. What do…
peter55555
  • 1,413
  • 1
  • 19
  • 36
10
votes
0 answers

Run a plugin before another one

I want to make a plugin which runs cppcheck tool inside a Sensor class. In order to reuse an existing and supported plugin I would like to use sonar-cxx plugin to import my plugin's results. How does it work to order plugins use in sonar-scanner? I…
begarco
  • 751
  • 7
  • 20
10
votes
1 answer

How can I redirect the output of cppcheck into a file?

I would like to redirect the output of cppcheck to a text file. It prints a lot of information to stdout but if I run cppcheck --enable=all --verbose . > /srv/samba/share/tmp/cppcheck.out, I do not get all the information in the file. Why not? How…
stdcerr
  • 13,725
  • 25
  • 71
  • 128
10
votes
1 answer

Did I find a bug in CppCheck? Why do I get the "Null pointer dereference" error here?

I'm using Cppcheck to manage my code. I have the following function: bool my_function(std::string my_string) const { return 0 == my_string.compare("Some text"); // line 3 } To my surprise, I get the Null pointer dereference in the line 3. I am…
user2738748
  • 1,106
  • 2
  • 19
  • 36
10
votes
1 answer

Cppcheck inline suppression not working

Example code: class Foo { // cppcheck-suppress noExplicitConstructor Foo(int foo) { } } Cppcheck call: $ cppcheck.exe --enable=all foo.cpp Checking foo.cpp... [foo.cpp:3]: (style) Class 'Foo' has a constructor with 1 argument that is not…
chtenb
  • 14,924
  • 14
  • 78
  • 116
10
votes
4 answers

use cppcheck without defining all the macros

I use cppcheck on a project using the boost library. The headers in this library contain a huge amount of macro that I don't even use in my sources. Nevertheless, cppcheck explore paths depending on these macros that I think useless. Is there a way…
Brahim
  • 808
  • 1
  • 8
  • 17
10
votes
3 answers

Cppcheck GUI: Excluding a file or folder from checking

I am using Cppcheck GUI to scan my projects (new in Cppcheck, just starded to use it) and want to exclude some sub folders when I am scanning my project. How to exclude some sub folder when scanning project folder with cppcheck GUI? I have watched…
T M
  • 3,195
  • 2
  • 31
  • 52
9
votes
2 answers

clang-tidy cmake exclude file from check

I have a dependency as source in my project that I have no control over. I'm using cmake's clang-tidy integration to analyze my code, and this dependency is firing A LOT of warnings. Is there a way to tell cmake not to run clang-tidy on specific…
Niverton
  • 163
  • 1
  • 2
  • 6
9
votes
1 answer

How can I exclude or specify build configurations with a Visual Studio solution in cppcheck?

I have a semi-large Visual Studio solution, with a series of different build configurations, and various platforms. I really only want to check one or two configurations, on a single platform. However, I don't see a way in the CppCheck manual to…
Dirv
  • 737
  • 1
  • 6
  • 13
9
votes
2 answers

cppcheck How to suppress inline unmatched suppression?

I found that --suppress=unmatchedSuppression only suppresses unmatched suppression types in cppcheck options, but NOT unmatched inline suppressions. Is this the expected behavior? test.c Line 4 is wrong. It should be warned…
kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
9
votes
1 answer

Writing Custom rules for cppcheck

I am using cppcheck for static analysis. To accelerate review process I want to set up cppcheck to look for some custom rules, for example to check if geter functions defined as a const. If anyone has experience in writing custom rules for cppcheck…
T M
  • 3,195
  • 2
  • 31
  • 52
9
votes
1 answer

Should I really massively introduce the explicit keyword?

When I used the (recently released) Cppcheck 1.69 on my code1, it showed a whole lot of messages where I expected none. Disabling noExplicitConstructor proved that all of them were of exactly this kind. But I found that I'm not the only one with a…
Wolf
  • 9,679
  • 7
  • 62
  • 108
8
votes
3 answers

scanf Cppcheck warning

Cppcheck shows the following warning for scanf: Message: scanf without field width limits can crash with huge input data. To fix this error message add a field width specifier: %s => %20s %i => %3i Sample program that can crash: #include…
Alex F
  • 42,307
  • 41
  • 144
  • 212
1
2
3
25 26