113498266Sopenharmony_ci# How to do code reviews for curl
213498266Sopenharmony_ci
313498266Sopenharmony_ciAnyone and everyone is encouraged and welcome to review code submissions in
413498266Sopenharmony_cicurl. This is a guide on what to check for and how to perform a successful
513498266Sopenharmony_cicode review.
613498266Sopenharmony_ci
713498266Sopenharmony_ci## All submissions should get reviewed
813498266Sopenharmony_ci
913498266Sopenharmony_ciAll pull requests and patches submitted to the project should be reviewed by
1013498266Sopenharmony_ciat least one experienced curl maintainer before that code is accepted and
1113498266Sopenharmony_cimerged.
1213498266Sopenharmony_ci
1313498266Sopenharmony_ci## Let the tools and tests take the first rounds
1413498266Sopenharmony_ci
1513498266Sopenharmony_ciOn initial pull requests, let the tools and tests do their job first and then
1613498266Sopenharmony_cistart out by helping the submitter understand the test failures and tool
1713498266Sopenharmony_cialerts.
1813498266Sopenharmony_ci
1913498266Sopenharmony_ci## How to provide feedback to author
2013498266Sopenharmony_ci
2113498266Sopenharmony_ciBe nice. Ask questions. Provide examples or suggestions of improvements.
2213498266Sopenharmony_ciAssume the best intentions. Remember language barriers.
2313498266Sopenharmony_ci
2413498266Sopenharmony_ciAll first-time contributors can become regulars. Let's help them go there.
2513498266Sopenharmony_ci
2613498266Sopenharmony_ci## Is this a change we want?
2713498266Sopenharmony_ci
2813498266Sopenharmony_ciIf this is not a change that seems to be aligned with the project's path
2913498266Sopenharmony_ciforward and as such cannot be accepted, inform the author about this sooner
3013498266Sopenharmony_cirather than later. Do it gently and explain why and possibly what could be
3113498266Sopenharmony_cidone to make it more acceptable.
3213498266Sopenharmony_ci
3313498266Sopenharmony_ci## API/ABI stability or changed behavior
3413498266Sopenharmony_ci
3513498266Sopenharmony_ciChanging the API and the ABI may be fine in a change but it needs to be done
3613498266Sopenharmony_cideliberately and carefully. If not, a reviewer must help the author to realize
3713498266Sopenharmony_cithe mistake.
3813498266Sopenharmony_ci
3913498266Sopenharmony_cicurl and libcurl are similarly strict on not modifying existing behavior. API
4013498266Sopenharmony_ciand ABI stability is not enough, the behavior should also remain intact as far
4113498266Sopenharmony_cias possible.
4213498266Sopenharmony_ci
4313498266Sopenharmony_ci## Code style
4413498266Sopenharmony_ci
4513498266Sopenharmony_ciMost code style nits are detected by checksrc but not all. Only leave remarks
4613498266Sopenharmony_cion style deviation once checksrc does not find anymore.
4713498266Sopenharmony_ci
4813498266Sopenharmony_ciMinor nits from fresh submitters can also be handled by the maintainer when
4913498266Sopenharmony_cimerging, in case it seems like the submitter is not clear on what to do. We
5013498266Sopenharmony_ciwant to make the process fun and exciting for new contributors.
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci## Encourage consistency
5313498266Sopenharmony_ci
5413498266Sopenharmony_ciMake sure new code is written in a similar style as existing code. Naming,
5513498266Sopenharmony_cilogic, conditions, etc.
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci## Are pointers always non-NULL?
5813498266Sopenharmony_ci
5913498266Sopenharmony_ciIf a function or code rely on pointers being non-NULL, take an extra look if
6013498266Sopenharmony_cithat seems to be a fair assessment.
6113498266Sopenharmony_ci
6213498266Sopenharmony_ci## Asserts
6313498266Sopenharmony_ci
6413498266Sopenharmony_ciConditions that should never be false can be verified with `DEBUGASSERT()`
6513498266Sopenharmony_cicalls to get caught in tests and debugging easier, while not having an impact
6613498266Sopenharmony_cion final or release builds.
6713498266Sopenharmony_ci
6813498266Sopenharmony_ci## Memory allocation
6913498266Sopenharmony_ci
7013498266Sopenharmony_ciCan the mallocs be avoided? Do not introduce mallocs in any hot paths. If
7113498266Sopenharmony_cithere are (new) mallocs, can they be combined into fewer calls?
7213498266Sopenharmony_ci
7313498266Sopenharmony_ciAre all allocations handled in error paths to avoid leaks and crashes?
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci## Thread-safety
7613498266Sopenharmony_ci
7713498266Sopenharmony_ciWe do not like static variables as they break thread-safety and prevent
7813498266Sopenharmony_cifunctions from being reentrant.
7913498266Sopenharmony_ci
8013498266Sopenharmony_ci## Should features be `#ifdef`ed?
8113498266Sopenharmony_ci
8213498266Sopenharmony_ciFeatures and functionality may not be present everywhere and should therefore
8313498266Sopenharmony_cibe `#ifdef`ed. Additionally, some features should be possible to switch on/off
8413498266Sopenharmony_ciin the build.
8513498266Sopenharmony_ci
8613498266Sopenharmony_ciWrite `#ifdef`s to be as little of a "maze" as possible.
8713498266Sopenharmony_ci
8813498266Sopenharmony_ci## Does it look portable enough?
8913498266Sopenharmony_ci
9013498266Sopenharmony_cicurl runs "everywhere". Does the code take a reasonable stance and enough
9113498266Sopenharmony_ciprecautions to be possible to build and run on most platforms?
9213498266Sopenharmony_ci
9313498266Sopenharmony_ciRemember that we live by C89 restrictions.
9413498266Sopenharmony_ci
9513498266Sopenharmony_ci## Tests and testability
9613498266Sopenharmony_ci
9713498266Sopenharmony_ciNew features should be added in conjunction with one or more test cases.
9813498266Sopenharmony_ciIdeally, functions should also be written so that unit tests can be done to
9913498266Sopenharmony_citest individual functions.
10013498266Sopenharmony_ci
10113498266Sopenharmony_ci## Documentation
10213498266Sopenharmony_ci
10313498266Sopenharmony_ciNew features or changes to existing functionality **must** be accompanied by
10413498266Sopenharmony_ciupdated documentation. Submitting that in a separate follow-up pull request is
10513498266Sopenharmony_cinot OK. A code review must also verify that the submitted documentation update
10613498266Sopenharmony_cimatches the code submission.
10713498266Sopenharmony_ci
10813498266Sopenharmony_ciEnglish is not everyone's first language, be mindful of this and help the
10913498266Sopenharmony_cisubmitter improve the text if it needs a rewrite to read better.
11013498266Sopenharmony_ci
11113498266Sopenharmony_ci## Code should not be hard to understand
11213498266Sopenharmony_ci
11313498266Sopenharmony_ciSource code should be written to maximize readability and be easy to
11413498266Sopenharmony_ciunderstand.
11513498266Sopenharmony_ci
11613498266Sopenharmony_ci## Functions should not be large
11713498266Sopenharmony_ci
11813498266Sopenharmony_ciA single function should never be large as that makes it hard to follow and
11913498266Sopenharmony_ciunderstand all the exit points and state changes. Some existing functions in
12013498266Sopenharmony_cicurl certainly violate this ground rule but when reviewing new code we should
12113498266Sopenharmony_cipropose splitting into smaller functions.
12213498266Sopenharmony_ci
12313498266Sopenharmony_ci## Duplication is evil
12413498266Sopenharmony_ci
12513498266Sopenharmony_ciAnything that looks like duplicated code is a red flag. Anything that seems to
12613498266Sopenharmony_ciintroduce code that we *should* already have or provide needs a closer check.
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci## Sensitive data
12913498266Sopenharmony_ci
13013498266Sopenharmony_ciWhen credentials are involved, take an extra look at what happens with this
13113498266Sopenharmony_cidata. Where it comes from and where it goes.
13213498266Sopenharmony_ci
13313498266Sopenharmony_ci## Variable types differ
13413498266Sopenharmony_ci
13513498266Sopenharmony_ci`size_t` is not a fixed size. `time_t` can be signed or unsigned and have
13613498266Sopenharmony_cidifferent sizes. Relying on variable sizes is a red flag.
13713498266Sopenharmony_ci
13813498266Sopenharmony_ciAlso remember that endianness and >= 32 bit accesses to unaligned addresses
13913498266Sopenharmony_ciare problematic areas.
14013498266Sopenharmony_ci
14113498266Sopenharmony_ci## Integer overflows
14213498266Sopenharmony_ci
14313498266Sopenharmony_ciBe careful about integer overflows. Some variable types can be either 32 bit
14413498266Sopenharmony_cior 64 bit. Integer overflows must be detected and acted on *before* they
14513498266Sopenharmony_cihappen.
14613498266Sopenharmony_ci
14713498266Sopenharmony_ci## Dangerous use of functions
14813498266Sopenharmony_ci
14913498266Sopenharmony_ciMaybe use of `realloc()` should rather use the dynbuf functions?
15013498266Sopenharmony_ci
15113498266Sopenharmony_ciDo not allow new code that grows buffers without using dynbuf.
15213498266Sopenharmony_ci
15313498266Sopenharmony_ciUse of C functions that rely on a terminating zero must only be used on data
15413498266Sopenharmony_cithat really do have a null-terminating zero.
15513498266Sopenharmony_ci
15613498266Sopenharmony_ci## Dangerous "data styles"
15713498266Sopenharmony_ci
15813498266Sopenharmony_ciMake extra precautions and verify that memory buffers that need a terminating
15913498266Sopenharmony_cizero always have exactly that. Buffers *without* a null-terminator must not be
16013498266Sopenharmony_ciused as input to string functions.
16113498266Sopenharmony_ci
16213498266Sopenharmony_ci# Commit messages
16313498266Sopenharmony_ci
16413498266Sopenharmony_ciTightly coupled with a code review is making sure that the commit message is
16513498266Sopenharmony_cigood. It is the responsibility of the person who merges the code to make sure
16613498266Sopenharmony_cithat the commit message follows our standard (detailed in the
16713498266Sopenharmony_ci[CONTRIBUTE](CONTRIBUTE.md) document). This includes making sure the PR
16813498266Sopenharmony_ciidentifies related issues and giving credit to reporters and helpers.
169