Debian Codegraph @

STYLE

raw
1 Code style
2 ==========
3
4 In general, FreeBSD style(9) should be followed unless it is irrelevant
5 (e.g., $FreeBSD$ tags).
6
7 Functions with external linkage are declared like this:
8 /**
9 * module_func(arg1, arg2):
10 * Description of what the function does, referring to arguments as
11 * ${arg1} or suchlike.
12 */
13 int module_func(void *, int);
14
15 The identical comment appears in the C file where the function is defined.
16
17 Static functions may have the above form of comment, or simply a
18 /* Brief description of what the function does. */
19 line before the function.
20
21 "Unrewrappable" comments starting in the first column should be
22 /**
23 * Written like this.
24 *
25 * Because (some of) the line-breaks are important.
26 */
27 whereas when such comments are indented, they should be
28 /*-
29 * Written like this.
30 *
31 * Because (some of) the line-breaks are important.
32 */
33
34 Line lengths should generally be 78 characters, and not more than 80
35 characters.
36
37 In general, functions should return (int)(-1) or NULL to indicate error.
38
39 Errors should be printed via warnp (if errno is relevant) or warn0 (if errno
40 is not relevant) when they are first detected and also at higher levels where
41 useful. As an exception to this, malloc failures (i.e., errno = ENOMEM) can
42 result in failure being passed back up the call chain without being printed
43 immediately. (Naturally, other errors can be passed back where a function
44 definition so specifies; e.g., ENOENT in cases where a file not existing is
45 not erroneous.)
46
47 The first statement in main(), after variable declarations, should be
48 "WARNP_INIT;" in order to set the program name used for printing warnings.
49
50 We use %d rather than %i in printf and warn0/warnp strings.
51
52 In general, functions should be structured with one return statement per
53 status, e.g., one return() for success and one return() for failure. Errors
54 should be handled by using goto to enter the error return path, e.g.,
55 int
56 foo(int bar)
57 {
58
59 if (something fails)
60 goto err0;
61 /* ... */
62 if (something else fails)
63 goto err1;
64 /* ... */
65 if (yet another operation fails)
66 goto err2;
67
68 /* Success! */
69 return (0);
70
71 err2:
72 /* Clean up something. */
73 err1:
74 /* Clean up something else. */
75 err0:
76 /* Failure! */
77 return (-1);
78 }
79
80 As an exception to the above, if there is only one way for the function to
81 fail, the idioms
82 return (baz(bar));
83 and
84 int rc;
85
86 rc = baz(bar);
87 /* ... cleanup code here ... */
88 return (rc);
89 are allowed; furthermore, in cases such as foo_free(), the idiom
90 if (we shouldn't do anything)
91 return;
92 is preferred over
93 if (we shouldn't do anything)
94 goto done;
95 at the start of a function.
96
97 Headers should be included in the following groups, with a blank line after
98 each (non-empty) group:
99 1. <sys/*.h>, with <sys/types.h> first followed by others alphabetically.
100 2. <net/*.h>, in alphabetical order.
101 3. <*.h>, in alphabetical order.
102 4. header files from /lib/, in alphabetical order.
103 5. header files from the program being built, in alphabetical order.
104 6. header files (usually just one) defining the interface for this C file.
105
106 If ssize_t is needed, <unistd.h> should be included to provide it.
107
108 If size_t is needed, <stddef.h> should be included to provide it unless
109 <stdio.h>, <stdlib.h>, <string.h>, or <unistd.h> is already required.
110
111 If the C99 integer types (uint8_t, int64_t, etc.) are required, <stdint.h>
112 should be included to provide them unless <inttypes.h> is already required.
113
114 The type 'char' should only be used to represent human-readable characters
115 (input from users, output to users, pathnames, et cetera). The type
116 'char *' should normally be a NUL-terminated string. The types 'signed
117 char' and 'unsigned char' should never be used; C99 integer types should
118 be used instead.
119
120 When a variable is declared to have a pointer type, there should be a space
121 between the '*' and the variable name, e.g.,
122 int
123 main(int argc, char * argv[])
124 {
125 char * opt_p = NULL;
126 Note that this is inconsistent with FreeBSD style(9). When used as a unary
127 operator, '*' is not separated from its argument, e.g.,
128 while (*p != '\0')
129 p++;
130
131 When a struct is referenced, the idiom
132 /* Opaque types. */
133 struct foo;
134
135 struct bar * bar_from_foo(struct foo *);
136 is preferable to
137 #include "foo.h" /* needed for struct foo */
138
139 struct bar * bar_from_foo(struct foo *);
140 unless there is some reason why the internal layout of struct foo is needed
141 (e.g., if struct bar contains a struct foo rather than a struct foo *). Such
142 struct declarations should be sorted alphabetically.
143
144 The file foo.c should only export symbols of the following forms:
145 foo_* -- most symbols should be of this form.
146 FOO_* / BAR_FOO_*
147 -- allowed in cases where FOO or BAR_FOO is idiomatic (e.g.,
148 MD5, HMAC_SHA256).
149 foo() / defoo() / unfoo()
150 -- where "foo" is a verb and this improves code clarity.
151
152 Functions named foo_free should return void, and foo_free(NULL) should have
153 no effect. The right way to spell a comment about this is
154 /* Behave consistently with free(NULL). */
155
156 If static variables need to be initialized to 0 (or NULL) then they should be
157 explicitly declared that way; implicit initialization should not be used.
158
159 In non-trivial code, comments should be included which describe in English
160 what is being done by the surrounding code with sufficient detail that if the
161 code were removed, it could be replaced based on reading the comments without
162 requiring any significant creativity.
163
164 Comments and documentation should be written in en-GB-oed; i.e., with
165 the 'u' included in words such as "honour", "colour", and "neighbour",
166 and the ending '-ize' in words such as "organize" and "realize". The
167 Oxford (aka. serial) comma should be used in lists. Quotation marks
168 should be placed logically, i.e., not including punctuation marks which
169 do not form a logical part of the quoted text. Two spaces should be used
170 after a period which ends a sentence.
171
172 The first local variable declaration in cookie-using functions should be
173 struct foo * bar = cookie;
174
175 When versions of functions are written to exploit special CPU features
176 (using the cpusupport framework), that code should be placed into a
177 separate file (e.g., crypto_aes_aesni.c) so that it can be compiled with
178 different compiler flags. Such a file should start with
179 #include "cpusupport.h"
180 #ifdef CPUSUPPORT_FOO_BAR
181 /**
182 * CPUSUPPORT CFLAGS: FOO_BAR FOO_BAZ
183 */
184 and end with
185 #endif /* CPUSUPPORT_FOO_BAR */
186
187 For example, we could have
188 #if defined(CPUSUPPORT_X86_SHANI) && defined(CPUSUPPORT_X86_SSSE3)
189 /**
190 * CPUSUPPORT CFLAGS: X86_SHANI X86_SSSE3
191 */
192
193 Functions for which special CPU-feature-exploiting variants exist should
194 take the form
195 {
196 /* Variable declarations here. */
197
198 /* Asserts here, if any. */
199
200 #ifdef CPUSUPPORT_FOO_BAR
201 if (/* We've decided we can use the variant code */) {
202 /* Call variant code and return. */
203 }
204 #endif
205
206 /* Normal implementation of the function. */
207 }
208
209 If there are multiple CPU-feature-exploiting variants, the `if` could instead
210 be a `switch` which invokes the appropriate variant function.