Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible array members are not standard C++ #9020

Closed
gilles-peskine-arm opened this issue Apr 9, 2024 · 0 comments · Fixed by #9446
Closed

Flexible array members are not standard C++ #9020

gilles-peskine-arm opened this issue Apr 9, 2024 · 0 comments · Fixed by #9446
Labels
bug component-crypto Crypto primitives and low-level interfaces size-s Estimated task size: small (~2d)

Comments

@gilles-peskine-arm
Copy link
Contributor

Since Mbed TLS 3.6.0, one of the types declared in an Mbed TLS header is a struct with a flexible array member: typedef struct psa_key_production_parameters_s psa_key_production_parameters_t.

struct psa_key_production_parameters_s {
    /* Future versions may add other fields in this structure. */
    uint32_t flags;
    uint8_t data[];
};

This is standard C99, but not standard C++ (any version), although many compilers accept it.

3.6.0 % echo '#include <psa/crypto.h>' | g++ -c -I include -std=c++2a -x c++ -
3.6.0 % echo '#include <psa/crypto.h>' | g++ -c -I include -std=c++2a -pedantic -x c++ -
In file included from include/psa/crypto.h:4828,
                 from <stdin>:1:
include/psa/crypto_struct.h:229:13: warning: ISO C++ forbids flexible array member ‘data’ [-Wpedantic]
  229 |     uint8_t data[];
      |             ^~~~
3.6.0 % echo '#include <psa/crypto.h>' | clang++ -c -I include -std=c++2a -x c++ -
3.6.0 % echo '#include <psa/crypto.h>' | clang++ -c -I include -std=c++2a -pedantic -x c++ -
In file included from <stdin>:1:
In file included from include/psa/crypto.h:4828:
include/psa/crypto_struct.h:229:13: warning: flexible array members are a C99 feature [-Wc99-extensions]
    uint8_t data[];
            ^
1 warning generated.

First reported here.

Importance:

  • Although we only advertise Mbed TLS as a C library, it is generally accepted that the headers of a C library can be consumed by a C++ compiler, and we do have a test that checks that C++ builds work (currently using GCC in non-pedantic mode, but that's originally not by design, just because we didn't think of adding -pedantic; currently partly by design because of other non-strict-C++-compliance that we'd also like to fix).
  • This evidently impacts many users, given that it was one of the first things reported after the 3.6 release.
  • Many projects like to invoke their compiler in pedantic mode. Disabling pedantic for the sake of the Mbed TLS headers would also potentially hide other desired errors.
  • Most C++ compilers do include a C99 compiler and are capable of mixing C99 and C++ features. However this is not the case for all of them. On Godbolt, I could only find one compiler that doesn't accept a flexible array member with its default command line: EDG C++, which claims C++17 and C90 but not C99 support.

Goal of this issue: find a solution so that at least users who don't call psa_generate_key_ext, and so don't actually need struct psa_key_production_parameters_s, can compile their C++ application with a strict C++ compiler that doesn't support flexible array members. This must be done without an API change for users who are building with a C99 compiler.

Validation: g++ -std=c++17 -pedantic and clang++ -std=c++17 -pedantic, ideally on the CI, but at least manually if we haven't fixed #7087.

@gilles-peskine-arm gilles-peskine-arm added bug component-crypto Crypto primitives and low-level interfaces size-s Estimated task size: small (~2d) labels Apr 9, 2024
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jun 6, 2024
The reason these functions are deprecated is that they cause strict C++
compilers to error out, since they use a struct type with a flexible array
member and that is not standard C++.

Resolves Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jun 6, 2024
Document psa_generate_key_ext() and psa_key_derivation_output_key_ext() as
deprecated in favor of psa_generate_key_custom() and
psa_key_derivation_output_key_custom(), and no longer declared in C++ builds.

Resolves Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jun 6, 2024
In public headers, we want to avoid things that are not standard C++,
including features that GCC and Clang support as extensions, such as
flexible array members. So compile with `-pedantic`.

Non-regression for Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jun 7, 2024
Document psa_generate_key_ext() and psa_key_derivation_output_key_ext() as
deprecated in favor of psa_generate_key_custom() and
psa_key_derivation_output_key_custom(), and no longer declared in C++ builds.

Resolves Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jun 7, 2024
In public headers, we want to avoid things that are not standard C++,
including features that GCC and Clang support as extensions, such as
flexible array members. So compile with `-pedantic`.

Non-regression for Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jul 31, 2024
Document psa_generate_key_ext() and psa_key_derivation_output_key_ext() as
deprecated in favor of psa_generate_key_custom() and
psa_key_derivation_output_key_custom(), and no longer declared in C++ builds.

Resolves Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Jul 31, 2024
In public headers, we want to avoid things that are not standard C++,
including features that GCC and Clang support as extensions, such as
flexible array members. So compile with `-pedantic`.

Non-regression for Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
@minosgalanakis minosgalanakis moved this to 3.6.1 patch release in Mbed TLS Epics Aug 2, 2024
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Aug 2, 2024
Document psa_generate_key_ext() and psa_key_derivation_output_key_ext() as
deprecated in favor of psa_generate_key_custom() and
psa_key_derivation_output_key_custom(), and no longer declared in C++ builds.

Resolves Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Aug 2, 2024
In public headers, we want to avoid things that are not standard C++,
including features that GCC and Clang support as extensions, such as
flexible array members. So compile with `-pedantic`.

Non-regression for Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Aug 5, 2024
Document psa_generate_key_ext() and psa_key_derivation_output_key_ext() as
deprecated in favor of psa_generate_key_custom() and
psa_key_derivation_output_key_custom(), and no longer declared in C++ builds.

Resolves Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Aug 5, 2024
In public headers, we want to avoid things that are not standard C++,
including features that GCC and Clang support as extensions, such as
flexible array members. So compile with `-pedantic`.

Non-regression for Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Aug 6, 2024
Document psa_generate_key_ext() and psa_key_derivation_output_key_ext() as
deprecated in favor of psa_generate_key_custom() and
psa_key_derivation_output_key_custom(), and no longer declared in C++ builds.

Resolves Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
gilles-peskine-arm added a commit to gilles-peskine-arm/mbedtls that referenced this issue Aug 6, 2024
In public headers, we want to avoid things that are not standard C++,
including features that GCC and Clang support as extensions, such as
flexible array members. So compile with `-pedantic`.

Non-regression for Mbed-TLS#9020.

Signed-off-by: Gilles Peskine <[email protected]>
@github-project-automation github-project-automation bot moved this to Done in Barriers Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug component-crypto Crypto primitives and low-level interfaces size-s Estimated task size: small (~2d)
Projects
Archived in project
Status: 3.6.1 patch release
Development

Successfully merging a pull request may close this issue.

1 participant