-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea to integrate cppcheck #1581
base: master
Are you sure you want to change the base?
Conversation
35b163e
to
b725a83
Compare
So, here my shot at #1574 .
is somewhat too unconventional for cppcheck (and it is weird, indeed, the .h ending should maybe change to .include?) But some limitations of cppcheck are easily outweighed by basically impossible to find gems like:
or
|
This sound like a good idea. Anything that can help us find bugs automatically is a good idea. |
I must admit that I do not really know how to integrate this properly in our build infrastructure. Run it on all the source tree and somewhere maintain a list of known issues that we do not want to fix? |
My initial plan IIRC was to incrementally add files that should be cppchecked and then fail if any of these shows any new errors. |
[Steffen Möller]
I must admit that I do not really know how to integrate this properly
in our build infrastructure. Run it on all the source tree and
somewhere maintain a list of known issues that we do not want to fix?
I suspect src/Makefile would need an update with TARGETS += cppcheck or
similar, and the CI scripts and d/control need a new build dependency.
…--
Happy hacking
Petter Reinholdtsen
|
Freecad seem to have a nice and sound code checking setup in its CI
|
Note to self by our friendly Jitsie-committee: Rebase! Introduce variables! |
Hi! I like the general idea, but may I suggest to put the body of the logic in a script (a .sh file somewhere) so that not only can actions run the script but so can a developer locally. Also, the branch has a conflict now. |
good idea, the Norwegian linuxcnc meeting agrees with @jepler |
i've sent a quick PR so lots of errors won't trigger CI for now so this could be merged without breaking stuff. |
@smoe can you rebase your branch/resolve conflicts? there were a lot of commits since back then ;) |
best way ist using compile_commands.json
|
8e945a9
to
5517792
Compare
Rebased. Also, I split the cppcheck invocations into multiple steps. I do not expect that build error to be triggered by the rebase. Had a look at what cppcheck brings, and, seems like some of them are real:
Yes, for the else branch beta is not set. Would not know how to set it, admittedly. 0.0?
Yes.
I am tempted to call this a false positive. Problem with designated initializers? Anyway, first it needs to build again :-) |
The bit about undefined beta/gamma: 5892 gamma = -M_PI_2l;
5893 } else
5894 ERS(NCE_BUG_SIDE_NOT_RIGHT_OR_LEFT);
5895 end_x = (px + (radius * cos(alpha + gamma))); ERS is a macro which always ends with "return;", so the // Set an error string using printf-style formats and return
#define ERS(fmt, ...) \
do { \
setError (fmt, ## __VA_ARGS__); \
_setup.stack_index = 0; \
(rtapi_strlcpy(_setup.stack[_setup.stack_index], __PRETTY_FUNCTION__, STACK_ENTRY_LEN)); \
_setup.stack[_setup.stack_index][STACK_ENTRY_LEN-1] = 0; \
_setup.stack_index++; \
_setup.stack[_setup.stack_index][0] = 0; \
return INTERP_ERROR; \
} while(0) So, EITHER it looks like this is a false positive from cppcheck OR there's something subtle going on with the ERS macro that means the code actually can continue from line 5894 to 5895. |
950 for(unsigned ii=0; ii<= nurbs_costant.size(); ii=ii+6) this loop check is probably just wrong, it's almost always an error to loop to |
308 #define MAX_CYCLE 18
...
319 int user_step_type[] = { [0 ... MAX_CYCLE-1] = -1 };
...
1206 for(i=0; i < MAX_CYCLE && user_step_type[i] != -1; i++) {
1207 master_lut[USER_STEP_TYPE][i] = user_step_type[i];
1208 used_phases |= user_step_type[i];
1209 } I checked GCC documentation for this gcc extension and I think this is a false positive. The range of this kind of initializer has an inclusive upper bound, so user_step_type[17] is initialized to -1 and is valid to access.
This might be a false positive that occurs because the GCC extension is not properly implemented in cppcheck. Adding the explicit bound might fix it? int user_step_type[MAX_CYCLE] = { [0 ... MAX_CYCLE-1] = -1 }; |
int foo(int x) {
int i;
if(x == 1) {
i = 3;
} else {
do {
return 2;
} while(0);
}
return i;
} reduced cppcheck test case for false positive legacyUninitvar -- someone interested could report it at their issue tracker. A long time ago they fixed a simpler case, but not when the return is in a do/while. |
Thanks a lot for the investigation! Does one get a github notification when highlighted in a random issue? Then @danmar might see this. |
I was not aware of the ERS macro. Fooled me, too. False positive, I tend to think. |
I have reported https://trac.cppcheck.net/ticket/13227 thanks! |
Thanks @danmar ! |
5517792
to
d46f876
Compare
imho it probably wouldn't hurt. (Aeons ago I learned "All variable definitions which aren't static need to be initialized" to avoid nasty bugs. But not sure if that still holds.) |
Should this have been a separate PR? Possibly, sorry for that. I have added shellcheck and applied it on *.sh in the archive. This seems to work just fine, have also added some commits that address some of the shellcheck's concerns. These were morely about missing quotes. Missing checks if an operation was successful I have addressed by setting bash's "-e" flag. Most problems were in the test scripts, so I am a bit curious if those still work now :) |
8124230
to
5deb6ed
Compare
5b8f3e8
to
1593e41
Compare
1593e41
to
3cac701
Compare
Done in 4c42080 . |
@smoe just want to say that i appreciate all your effort so much. This will add a good amount of solidness to the codebase. If we can at some point remove the "continue-on-error" to make CI fail on warnings, less efforts need to be taken when merging PRs aswell. A bright future lies ahead. All hail to static code analysis! ;) |
The next upload will have the separate scripts that @jepler and the Oslo folks suggested. Testing it, I was directed to ./src/hal/classicladder/edit_gtk.c
The "error" is a bit drastic, not? Shall I find out how to override this one? Have started extending the range of files to be checked. The directory src/hal/drivers is challenging: I tend to agree that a "return 0;" is missing:
The following may need some comments in the code - or a fix :-)
The following reads like intentional, a bit weird, though:
The corresponding source code is
so it looks a bit like an intentional effort to reach the bit indicating the sign on 32bit systems. @rene-dev , I have seen your comment in #1581 (comment) but have no exact idea how to incorporate this idea into this PR. Is this possibly for later once we have the cppcheck mandatory for all of our code? |
Checking src/hal/user_comps/mb2hal/mb2hal_init.c ... src/hal/user_comps/mb2hal/mb2hal_init.c:152:13: error: Common realloc mistake: 'name_ptrs' nulled but not freed upon failure [memleakOnRealloc] name_ptrs = realloc(name_ptrs, sizeof(char *) * name_buf_size);
There may be better ideas how to address this. The plan is to iteratively add all directories for which cppcheck works. At some point the coverage should then be good enough to simplify by calling it on larger subtrees.