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

C99: simplify size_t printing with printf #9307

Open
gilles-peskine-arm opened this issue Jun 24, 2024 · 2 comments
Open

C99: simplify size_t printing with printf #9307

gilles-peskine-arm opened this issue Jun 24, 2024 · 2 comments
Labels
api-break This issue/PR breaks the API and must wait for a new major version component-tls enhancement size-s Estimated task size: small (~2d)

Comments

@gilles-peskine-arm
Copy link
Contributor

gilles-peskine-arm commented Jun 24, 2024

Since Mbed TLS 2.23, we require a C99 platform, with some pragmatic restrictions to support platforms that are mostly but not fully compliant to C99. In particular, we do not require printf and friends (snprintf, vsnprintf, fprintf) to support C99 format specifiers.

In Mbed TLS 4.0, consider requiring printf and friends to support the z modifier to print size_t values with %zu. This only affects the TLS debugging code (gated by MBEDTLS_DEBUG_C). That code could be simplified and it would be easier to ensure that we use the right modifiers.

We would lose support for older C libraries that don't provide the z modifier. Currently, integrators can define MBEDTLS_PRINTF_SIZET to e.g. u or lu if their platform does not support %zu. If we remove this possibility, integrators on a platform that does not support %zu would need to provide a whole printf alternative by defining mbedtls_printf and friends.

Mailing list discussion: https://lists.trustedfirmware.org/archives/list/mbed-tls@lists.trustedfirmware.org/thread/RAV67RL6GF5CX6CFDH5LZAR7RPAY2AEK/

@gilles-peskine-arm gilles-peskine-arm added enhancement needs-design-approval component-tls api-break This issue/PR breaks the API and must wait for a new major version size-s Estimated task size: small (~2d) labels Jun 24, 2024
@mschulz-at-hilscher
Copy link
Contributor

Could we just rely on format modifier definitions in inttypes.h?
If a compiler does not provide that header, an integrator could just provide one that works for the used compiler?

@gilles-peskine-arm
Copy link
Contributor Author

@mschulz-at-hilscher Unfortunately, there's no definition in inttypes.h for a size_t format. But other projects e.g. Chromium have defined a macro using the same naming pattern as inttypes.h:

#define PRIuS "zu"

I would suggest that we rename MBEDTLS_PRINTF_SIZET to PRIuS, but we'd continue to not use %zu directly.

@gilles-peskine-arm gilles-peskine-arm changed the title C99: consider requiring printf to support %zu (size_t printing) C99: simplify size_t printing with printf Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-break This issue/PR breaks the API and must wait for a new major version component-tls enhancement size-s Estimated task size: small (~2d)
Projects
Status: Implementation needed
Development

No branches or pull requests

3 participants