Skip to content

Commit 4c44990

Browse files
Include common.h before system headers
In library source files, the order of things should be: 1. Define macros that affect the behavior of system headers, such as `_POSIX_C_SOURCE` and `_GNU_SOURCE`. 2. Include the library's common header: `common.h`. It takes care of many things, including defining the library configuration, granting access to private fields in structures, and activating platform-specific hacks. 3. Possibly a few header inclusions and macro definitions. 4. Guard everything else by `#if defined(MBEDTLS_XXX_C)` or some such. Enforce this order in files that previously did things they shouldn't have before including `common.h`. To locate the potentially problematic files: ``` grep -m1 '^#' library/*.c | grep -v -F common.h ``` Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
1 parent 14ada7f commit 4c44990

File tree

5 files changed

+20
-21
lines changed

5 files changed

+20
-21
lines changed

library/aesce.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
66
*/
77

8+
#include "common.h"
9+
10+
#if defined(MBEDTLS_AESCE_C)
11+
812
#if defined(__clang__) && (__clang_major__ >= 4)
913

1014
/* Ideally, we would simply use MBEDTLS_ARCH_IS_ARMV8_A in the following #if,
@@ -39,9 +43,6 @@
3943
#endif /* defined(__clang__) && (__clang_major__ >= 4) */
4044

4145
#include <string.h>
42-
#include "common.h"
43-
44-
#if defined(MBEDTLS_AESCE_C)
4546

4647
#include "aesce.h"
4748

library/base64.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
66
*/
77

8-
#include <limits.h>
9-
108
#include "common.h"
119

1210
#if defined(MBEDTLS_BASE64_C)
@@ -16,6 +14,7 @@
1614
#include "constant_time_internal.h"
1715
#include "mbedtls/error.h"
1816

17+
#include <limits.h>
1918
#include <stdint.h>
2019

2120
#if defined(MBEDTLS_SELF_TEST)

library/constant_time.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
* might be translated to branches by some compilers on some platforms.
1111
*/
1212

13-
#include <stdint.h>
14-
#include <limits.h>
15-
1613
#include "common.h"
1714
#include "constant_time_internal.h"
1815
#include "mbedtls/constant_time.h"
1916
#include "mbedtls/error.h"
2017
#include "mbedtls/platform_util.h"
2118

19+
#include <limits.h>
20+
#include <stdint.h>
2221
#include <string.h>
2322

2423
#if !defined(MBEDTLS_CT_ASM)

library/sha256.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
1111
*/
1212

13+
/* Ensure that SIG_SETMASK is defined when -std=c99 is used. */
14+
#if !defined(_GNU_SOURCE)
15+
#define _GNU_SOURCE
16+
#endif
17+
18+
#include "common.h"
19+
20+
#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C)
21+
1322
#if defined(__clang__) && (__clang_major__ >= 4)
1423

1524
/* Ideally, we would simply use MBEDTLS_ARCH_IS_ARMV8_A in the following #if,
@@ -43,15 +52,6 @@
4352

4453
#endif /* defined(__clang__) && (__clang_major__ >= 4) */
4554

46-
/* Ensure that SIG_SETMASK is defined when -std=c99 is used. */
47-
#if !defined(_GNU_SOURCE)
48-
#define _GNU_SOURCE
49-
#endif
50-
51-
#include "common.h"
52-
53-
#if defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA224_C)
54-
5555
#include "mbedtls/sha256.h"
5656
#include "mbedtls/platform_util.h"
5757
#include "mbedtls/error.h"

library/sha512.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
* http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
1111
*/
1212

13+
#include "common.h"
14+
15+
#if defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA384_C)
16+
1317
#if defined(__aarch64__) && !defined(__ARM_FEATURE_SHA512) && \
1418
defined(__clang__) && __clang_major__ >= 7
1519
/* TODO: Re-consider above after https://reviews.llvm.org/D131064 merged.
@@ -26,10 +30,6 @@
2630
#define MBEDTLS_ENABLE_ARM_SHA3_EXTENSIONS_COMPILER_FLAG
2731
#endif
2832

29-
#include "common.h"
30-
31-
#if defined(MBEDTLS_SHA512_C) || defined(MBEDTLS_SHA384_C)
32-
3333
#include "mbedtls/sha512.h"
3434
#include "mbedtls/platform_util.h"
3535
#include "mbedtls/error.h"

0 commit comments

Comments
 (0)