aboutsummaryrefslogtreecommitdiff
path: root/C/README.md
blob: 3a38e073b146f01dace9a4ccf4c6bc97c881486e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# C ArKa Coding style
---

The coding style used for C project by ArKa, is the same used in the Linux Kernel.

## File layout

Files MUST follow this layout:
- Comment with LICENSE and short explaination of the file / tool  
- Headers  
- Macros  
- Types  
- Function declarations:  
    - Include variable names.  
    - For short files these can be left out.  
    - Group/order in logical manner.  
- Global variables.  
- Function definitions in same order as declarations.  
- main (if needed)  

## Compilation & Makefile

To compile C code, a magical tool was created, Makefiles.  
  
Makefiles MUST look likes Makefile from [Makefile tutor](https://clemedon.github.io/Makefile_tutor/)  
  
Every project should be compiled with those flags:  
`-O2 -Wall -Wformat -Wformat=2 -Wconversion -Wimplicit-fallthrough \
-Werror=format-security \
-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 \
-D_GLIBCXX_ASSERTIONS \
-fstrict-flex-arrays=3 \
-fstack-clash-protection -fstack-protector-strong \
-Wl,-z,nodlopen -Wl,-z,noexecstack \
-Wl,-z,relro -Wl,-z,now \
-Wl,--as-needed -Wl,--no-copy-dt-needed-entries`

For test/debug, use those flags:  
`-fsanitize=address -fsanitize=pointer-compare -fsanitize=pointer-subtract -fsanitize=leak -fno-omit-frame-pointer -fsanitize=undefined -fsanitize=bounds-strict -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow
export ASAN_OPTIONS=strict_string_checks=1:detect_stack_use_after_return=1:check_initialization_order=1:strict_init_order=1:detect_invalid_pointer_pairs=2`

List of flags every project should use [by FSF](https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html)  

List of flags every project should use [by Airbus security](https://airbus-seclab.github.io/c-compiler-security/)  

Explaination of every flags with [this stackexchange post](https://security.stackexchange.com/questions/24444/what-is-the-most-hardened-set-of-options-for-gcc-compiling-c-c)  


## Code style

You can use the configured clang-format file to automatically format your code.

## Project setup

The project tree sould basically look like this:

```
my_project/
|--- .git/
|--- src/
|      |----- main.c
|--- include/
|      |---- main.h
|--- Makefile
|--- README.md
|--- .clang-format
```

Because we are not using some autocompilations tools, clang doesn't know the
structure of the project. To help him understand it, we need to make the
Makefile in first, and use bear for helping clang to understand the project.
  
So once the Makefile is made, just run `make all; bear -- make`

## Folder and filename
ArKa projects. All rights to me, and your next child right arm.