xref: /curl/docs/CODE_REVIEW.md (revision 816ac2a8)
1<!--
2Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3
4SPDX-License-Identifier: curl
5-->
6
7# How to do code reviews for curl
8
9Anyone and everyone is encouraged and welcome to review code submissions in
10curl. This is a guide on what to check for and how to perform a successful
11code review.
12
13## All submissions should get reviewed
14
15All pull requests and patches submitted to the project should be reviewed by
16at least one experienced curl maintainer before that code is accepted and
17merged.
18
19## Let the tools and tests take the first rounds
20
21On initial pull requests, let the tools and tests do their job first and then
22start out by helping the submitter understand the test failures and tool
23alerts.
24
25## How to provide feedback to author
26
27Be nice. Ask questions. Provide examples or suggestions of improvements.
28Assume the best intentions. Remember language barriers.
29
30All first-time contributors can become regulars. Let's help them go there.
31
32## Is this a change we want?
33
34If this is not a change that seems to be aligned with the project's path
35forward and as such cannot be accepted, inform the author about this sooner
36rather than later. Do it gently and explain why and possibly what could be
37done to make it more acceptable.
38
39## API/ABI stability or changed behavior
40
41Changing the API and the ABI may be fine in a change but it needs to be done
42deliberately and carefully. If not, a reviewer must help the author to realize
43the mistake.
44
45curl and libcurl are similarly strict on not modifying existing behavior. API
46and ABI stability is not enough, the behavior should also remain intact as far
47as possible.
48
49## Code style
50
51Most code style nits are detected by checksrc but not all. Only leave remarks
52on style deviation once checksrc does not find anymore.
53
54Minor nits from fresh submitters can also be handled by the maintainer when
55merging, in case it seems like the submitter is not clear on what to do. We
56want to make the process fun and exciting for new contributors.
57
58## Encourage consistency
59
60Make sure new code is written in a similar style as existing code. Naming,
61logic, conditions, etc.
62
63## Are pointers always non-NULL?
64
65If a function or code rely on pointers being non-NULL, take an extra look if
66that seems to be a fair assessment.
67
68## Asserts
69
70Conditions that should never be false can be verified with `DEBUGASSERT()`
71calls to get caught in tests and debugging easier, while not having an impact
72on final or release builds.
73
74## Memory allocation
75
76Can the mallocs be avoided? Do not introduce mallocs in any hot paths. If
77there are (new) mallocs, can they be combined into fewer calls?
78
79Are all allocations handled in error paths to avoid leaks and crashes?
80
81## Thread-safety
82
83We do not like static variables as they break thread-safety and prevent
84functions from being reentrant.
85
86## Should features be `#ifdef`ed?
87
88Features and functionality may not be present everywhere and should therefore
89be `#ifdef`ed. Additionally, some features should be possible to switch on/off
90in the build.
91
92Write `#ifdef`s to be as little of a "maze" as possible.
93
94## Does it look portable enough?
95
96curl runs "everywhere". Does the code take a reasonable stance and enough
97precautions to be possible to build and run on most platforms?
98
99Remember that we live by C89 restrictions.
100
101## Tests and testability
102
103New features should be added in conjunction with one or more test cases.
104Ideally, functions should also be written so that unit tests can be done to
105test individual functions.
106
107## Documentation
108
109New features or changes to existing functionality **must** be accompanied by
110updated documentation. Submitting that in a separate follow-up pull request is
111not OK. A code review must also verify that the submitted documentation update
112matches the code submission.
113
114English is not everyone's first language, be mindful of this and help the
115submitter improve the text if it needs a rewrite to read better.
116
117## Code should not be hard to understand
118
119Source code should be written to maximize readability and be easy to
120understand.
121
122## Functions should not be large
123
124A single function should never be large as that makes it hard to follow and
125understand all the exit points and state changes. Some existing functions in
126curl certainly violate this ground rule but when reviewing new code we should
127propose splitting into smaller functions.
128
129## Duplication is evil
130
131Anything that looks like duplicated code is a red flag. Anything that seems to
132introduce code that we *should* already have or provide needs a closer check.
133
134## Sensitive data
135
136When credentials are involved, take an extra look at what happens with this
137data. Where it comes from and where it goes.
138
139## Variable types differ
140
141`size_t` is not a fixed size. `time_t` can be signed or unsigned and have
142different sizes. Relying on variable sizes is a red flag.
143
144Also remember that endianness and >= 32-bit accesses to unaligned addresses
145are problematic areas.
146
147## Integer overflows
148
149Be careful about integer overflows. Some variable types can be either 32-bit
150or 64-bit. Integer overflows must be detected and acted on *before* they
151happen.
152
153## Dangerous use of functions
154
155Maybe use of `realloc()` should rather use the dynbuf functions?
156
157Do not allow new code that grows buffers without using dynbuf.
158
159Use of C functions that rely on a terminating zero must only be used on data
160that really do have a null-terminating zero.
161
162## Dangerous "data styles"
163
164Make extra precautions and verify that memory buffers that need a terminating
165zero always have exactly that. Buffers *without* a null-terminator must not be
166used as input to string functions.
167
168# Commit messages
169
170Tightly coupled with a code review is making sure that the commit message is
171good. It is the responsibility of the person who merges the code to make sure
172that the commit message follows our standard (detailed in the
173[CONTRIBUTE](CONTRIBUTE.md) document). This includes making sure the PR
174identifies related issues and giving credit to reporters and helpers.
175