You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+33-9Lines changed: 33 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,32 @@
1
1
# Static Analysis Example
2
2
3
-
Some quick code I wrote, mainly solution to different code exercises.
3
+
This is a small demo analyzing C code, in particular static analysis.
4
+
The code in question involve a dozen or so smaller C files which I wrote, test files or solution to different code exercises.
4
5
5
-
They all work but have different bugs, which hopefully are detected by the supported static analysis tools.
6
+
These small programs all "work" but according to static analysis tools they have many bugs (some I wilfully introduced).
7
+
It is interesting to see that even with many GCC warnings enabled (CMakeLists.txt) no warnings were reported,
8
+
and except for two memory errors I introduced (positives.c and ptrs.c), valgrind has no complaints.
6
9
7
-
Let's see.
10
+
The code was compiled with GCC 8.4.0 and relevant flags include: _"-Wall -Wextra -pedantic -Werror -Wformat=2 -Wconversion -Wdouble-promotion -O0 -g"_
8
11
9
-
NOTE: positives.c and ptrs.c contain some errors / memory leaks I wilfully introduced.
12
+
Results from analysis below, also summarized in c\_testing\_slides.pdf.
10
13
14
+
__Addendum:__ More GCC flags which we could've added (would have detected some additional bugs): _"-Wshadow -Wundef -Og"_
15
+
16
+
## Software Used
17
+
18
+
```
19
+
clang-7 / clang-tidy-7
20
+
GCC 8.4.0
21
+
Valgrind 3.13.0
22
+
Semgrep and CodeQL via CI pipeline (2022-03-22):
23
+
Semgrep 0.86.5 (/w all C specific rules, about 85)
24
+
CodeQL 2.8.1 (../CodeQL/0.0.0-20220214/..)
25
+
(Ubuntu 18.04)
26
+
```
27
+
28
+
Note that newer versions of the compiler and the static analysis tools will likely detect even more warnings
29
+
(clang-7 is fairly old).
11
30
12
31
## Run tests
13
32
@@ -101,9 +120,6 @@ As we use its sensitive setting, we get a few extra LOW and MEDIUM warnings (can
101
120
102
121
I was suprised to see that it did not complain of cert-err33-c, i.e. we do not check the return values of snprintf (et al.). Perhaps I have a too old version, as I've seen this warning in other situations. I think this is a warning which we could try to enable in the future...
(ran online via this Github CI pipeline, 2022-03-22)
226
+
_(ran online via this Github CI pipeline, 2022-03-22)_
207
227
208
228
## Extras
209
229
@@ -215,6 +235,10 @@ Ideally, we'd want to have unit tests, good code coverage, etc.
215
235
Perhaps also mock certain functions to see how the program
216
236
behaves for certain edge-cases.
217
237
238
+
Additionally, with black-box testing such as memory check with Valgrind we also want to have better code coverage.
239
+
Valgrind only checks the program under test, so it is possible that memory leaks are missed if that path is not taken
240
+
and additionally we do not currently test the library in _react_exercise_ for this reason.
241
+
218
242
For dynamic analysis we also have fuzzy testing.
219
243
220
244
Bonus: note also the different options to harden the binary itself (see https://github.com/Eliot-Roxbergh/examples/blob/master/c_programming/development_tips/gcc_flags.md)
0 commit comments