PrePAN

Sign in to PrePAN

C::Check or C::Static or C::Mistakes or C::Critic static check of C programs

Author
benkasminbullock@github
Date
URL
Status
In Review
Good

Synopsis

use C::Check 'check_functions';
# File::Slurper etc.
my $c = read_text ('file.c');
check_functions ($c);

e.g. calloc arguments in wrong order, uses gets, insists on comments at switch fallthroughs, optionally complains about using assert, etc.

Description

C compilers typically don't warn about daft mistakes like putting function arguments in the wrong order, or using the wrong size of a variable, or things like not checking the return value of malloc, etc. This module would check for typical errors in C programs like switch fallthroughs, use of equals instead of == in an if statement, insist on using braces with if statements, bad if statement indentation, like

if (x) 
      printf ("reached");
      printf ("reached even if x is not true, despite this indentation");

etc.

At the moment I have a script which does something like the above, wondering whether it would be worth working up into a module.

Comments

However, modern C compilers (e.g. gcc 6) can warn on some (all?) of these possible problems. For example the bad indentation problem:

bla.c: In function ‘main’:
bla.c:14:5: warning: this ‘if’ clause does not guard... [-Wmisleading-indentation]
if (0)
^~
bla.c:16:2: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the ‘if’
printf("2\n");
^~~~~~
C compilers can definitely catch some of the problems. I'm not aware of a C compiler which checks that you check the value returned from malloc or fopen, or checks that you have used a sizeof argument in calloc, bsearch, qsort, or checks there is an equivalent "free" statement for each "malloc", or insists on not using magic numbers anywhere, or forces one to add a comment when using a fallthrough in a switch.

These are the kinds of jobs I'm already doing with my hacky script. I wonder if it is worth converting the hacky script into a proper module and releasing it.

Please sign up to post a review.