Skip to content

Commit 4f8db0d

Browse files
Merge pull request #122 from tom-cosgrove-arm/code-style-231013
Various tweaks to code style
2 parents 5f2d5aa + a7064d3 commit 4f8db0d

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

kb/development/mbedtls-coding-standards.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,24 @@ int do_calc_length(const unsigned char *str);
297297

298298
The code uses the C99 ISO standard.
299299

300+
However, don't use variable-length arrays (VLAs) as they are not supported by all compilers/systems, and can cause problems for static analysis.
301+
302+
In addition, avoid using `const` values to size arrays or as part of rvalues for other constants, as this is not supported by (at least) MSVC.
303+
300304
### Proper argument and variable typing
301305

302306
Type function arguments and variables properly. Specifically, the `int` and `size` fields hold their maximum length in a platform-independent way. For buffer length, this almost always means using `size_t`.
303307

304308
For values that can't be negative, use unsigned variables. Keep the type in mind when building loops with unsigned variables.
305309

310+
When it's unavoidable that a `size_t` must be passed as an `int` function parameter, it's necessary to add a cast to avoid warnings on some compilers.
311+
306312
### `Goto`
307313

308314
Use of `goto` is allowed in functions that have to do cleaning up before returning from the function even when an error has occurred. It can also be used to exit nested loops. In other cases the use of `goto` should be avoided.
309315

316+
Some compilers (e.g. IAR) issue warnings when a `goto` jumps over a variable declaration. In these cases, either hoist the variable to the top of the function, use a local block, or extract the code with this variable into a smaller `static` function. If hoisting a pointer, it must be initialized, to avoid potential code-paths where it may be used uninitialized.
317+
310318
### Exit early and prevent nesting
311319

312320
Structure functions to exit or `goto` the exit code as early as possible. This prevents nesting of code blocks and improves code readability.

0 commit comments

Comments
 (0)