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