Skip to content

Commit 874d738

Browse files
Add suggestion of more GCC flags & update README
1 parent a27c431 commit 874d738

2 files changed

Lines changed: 50 additions & 21 deletions

File tree

CMakeLists.txt

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@ set(MEMORYCHECK_COMMAND_OPTIONS "--error-exitcode=1") #fail on leak
99
include(CTest)
1010

1111
set(C_STD gnu11)
12-
#set(CMAKE_C_COMPILER /usr/bin/gcc)
13-
1412
#-DCMAKE_BUILD_TYPE=Debug
1513
IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
1614
message("Debug build")
17-
set(C_FLAGS_DEBUG "-g -fasynchronous-unwind-tables -fexceptions") #-g is probably implied by debug?
18-
set(C_FLAGS_WARNINGS "-Wall -Wextra -pedantic -Werror -Wformat=2 -Wconversion")
15+
set(C_FLAGS_DEBUG " -O0 -g -fasynchronous-unwind-tables -fexceptions ")
16+
set(C_FLAGS_WARNINGS " -Wall -Wextra -pedantic -Werror -Wformat=2 -Wconversion -Wdouble-promotion ")
17+
#More flags to try (these report some new warnings): "-Wshadow -Wundef -Og"
1918
#-DCMAKE_BUILD_TYPE=Release
2019
ELSE()
2120
message("Release build")
22-
set(C_FLAGS_SECURITY "-D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -fstack-protector-strong -Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro -Wl,-z,defs")
23-
#set(C_FLAGS_SECURITY_EXEC "-fpie -Wl,-pie")
24-
#set(C_FLAGS_SECURITY_LIB "-fpic")
21+
set(C_FLAGS_DEBUG " -O3 ")
22+
set(C_FLAGS_SECURITY " -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -fstack-protector-strong \
23+
-Wl,-z,noexecstack -Wl,-z,now -Wl,-z,relro -Wl,-z,defs \
24+
") #-fcf-protection=full -fstack-clash-protection
25+
#set(C_FLAGS_SECURITY_EXEC " -fpie -Wl,-pie ")
26+
#set(C_FLAGS_SECURITY_LIB " -fpic ")
2527
ENDIF()
2628

27-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=${C_STD} ${C_FLAGS_DEBUG} ${C_FLAGS_WARNINGS} ${C_FLAGS_SECURITY}")
29+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=${C_STD} ${C_FLAGS_DEBUG} ${C_FLAGS_WARNINGS} ${C_FLAGS_SECURITY} -fno-common")
30+
#set(CMAKE_C_COMPILER /usr/bin/gcc)
2831

2932
add_executable(array array.c)
3033
add_executable(bit_manip bit_manip.c)
@@ -81,7 +84,7 @@ if (TEST_CPPCHECK)
8184
--force;
8285
--inline-suppr;
8386
--std=${C_STD};
84-
#"--suppressions-list=${CMAKE_SOURCE_DIR}/CppCheckSuppressions.txt";
87+
#"--suppressions-list=${CMAKE_SOURCE_DIR}/CppCheckSuppressions.txt" ;
8588
)
8689
endif()
8790
if (TEST_IWYU)
@@ -134,18 +137,19 @@ add_custom_target(
134137
#TODO infer also looks like an interesting analyzer
135138
#TODO most these tools are supported in CodeChecker, easier to get an overview with that front-end
136139
# https://github.com/facebook/infer
137-
#CodeQL and Semgrep are run via CI so not added here.
138140

139141

142+
#CodeQL and Semgrep are run via CI so not added here.
143+
140144

141145
# Extra, option to use clang (et al) front-end, CodeChecker
142146
# Use clang-7, clang-tidy-7 or later
143147
# pip3 install --user codechecker
144148
# (If "codechecker" not found try "CodeChecker")
145149
add_custom_target(
146150
codechecker_build
147-
COMMAND codechecker log --build "make";
148-
--output "./compile_commands.json";
151+
COMMAND codechecker log --build "make" ;
152+
--output "./compile_commands.json" ;
149153
)
150154
add_custom_target(
151155
codechecker_analyze
@@ -166,3 +170,4 @@ add_custom_target(
166170
DEPENDS codechecker_analyze
167171
COMMAND codechecker parse ./reports -e html -o ./reports_html
168172
)
173+

README.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
# Static Analysis Example
22

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.
45

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.
69

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"_
811

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.
1013

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).
1130

1231
## Run tests
1332

@@ -101,9 +120,6 @@ As we use its sensitive setting, we get a few extra LOW and MEDIUM warnings (can
101120

102121
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...
103122

104-
CodeChecker 6.19.1
105-
clang(-tidy) 7.0.0
106-
107123
```
108124
-----------------------------------------------------------------------
109125
Checker name | Severity | Number of reports : True Positives (my approximation)
@@ -150,6 +166,9 @@ Detected by other tools (semgrep)
150166
[LOW] read_input.c:217 [cert-err34-c] TP, 'fscanf' (3/3)
151167
```
152168

169+
_CodeChecker 6.19.1, clang(-tidy) 7.0.0_
170+
171+
153172
Interesting that we get a narrowing conversion warning not caught by -Wconversion, maybe enums is a special case?
154173

155174
#### Semgrep
@@ -162,7 +181,6 @@ good input that other tools largely ignored.
162181

163182
For instance, a bit silly to always complain on the use of strlen or memcpy, but still might be a good idea to use the safer strlen_s and memcpy_s.
164183

165-
(ran online via this Github CI pipeline, 2022-03-22)
166184

167185
```
168186
Unique
@@ -186,6 +204,8 @@ Detected by other tools
186204
[HIGH] read_input.c:217 vswscanf-1 // ^
187205
```
188206

207+
_(ran online via this Github CI pipeline, 2022-03-22)_
208+
189209
(apparently you can't see the warnings for more than 1 month if you don't pay them 40$/month, oops. Can run it locally though..)
190210

191211
#### CodeQL
@@ -203,7 +223,7 @@ Detected by other tools
203223
[Critical] positives.c:22 // Bug! Likely overrunning write
204224
```
205225

206-
(ran online via this Github CI pipeline, 2022-03-22)
226+
_(ran online via this Github CI pipeline, 2022-03-22)_
207227

208228
## Extras
209229

@@ -215,6 +235,10 @@ Ideally, we'd want to have unit tests, good code coverage, etc.
215235
Perhaps also mock certain functions to see how the program
216236
behaves for certain edge-cases.
217237

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+
218242
For dynamic analysis we also have fuzzy testing.
219243

220244
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

Comments
 (0)