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

Wrong macros in mps_common.h #6254

Closed
Laserdance100 opened this issue Sep 4, 2022 · 5 comments · Fixed by #6257
Closed

Wrong macros in mps_common.h #6254

Laserdance100 opened this issue Sep 4, 2022 · 5 comments · Fixed by #6257
Assignees
Labels
bug component-crypto Crypto primitives and low-level interfaces size-s Estimated task size: small (~2d)

Comments

@Laserdance100
Copy link
Contributor

In mbedtls/library/mps_common.h I have GCC warnings for those:

typedef size_t mbedtls_mps_stored_size_t;
#define MBEDTLS_MPS_STORED_SIZE_MAX ( (mbedtls_mps_stored_size_t) -1 )
typedef size_t mbedtls_mps_size_t;
#define MBEDTLS_MPS_SIZE_MAX ( (mbedtls_mps_size_t) -1 )

It looks like it should be either sizeof(type)-1 or 2^sizeof(type)-1.
Current behavior of GCC is defaulting types values with 0 in the macros.

@paul-elliott-arm
Copy link
Member

Hi!

Many thanks for reporting this - could you possibly supply the warnings you are getting, the GCC version and the platform you are targetting?

Many thanks,

Paul.

@paul-elliott-arm paul-elliott-arm self-assigned this Sep 5, 2022
@paul-elliott-arm paul-elliott-arm added bug component-crypto Crypto primitives and low-level interfaces size-s Estimated task size: small (~2d) labels Sep 5, 2022
@gilles-peskine-arm
Copy link
Contributor

The definitions have correct values. But they can't be used in preprocessor conditionals, since the preprocessor doesn't know about types (whether it's through casts, sizeof or whatever). Either we need to change the preprocessor conditionals or to change the definitions to use SIZE_MAX.

@Laserdance100
Copy link
Contributor Author

Laserdance100 commented Sep 5, 2022

Hullo.

I'm using arm-none-eabi-gcc-10.2.1 and my target is STM32F7.
The warning message is as follow: warning: <type_name> is not defined, evaluates to 0, in expansion of macro <*_SIZE_MAX>

As we can read in the doxygen comment "For now, we use the default type of size_t throughout, and the use of smaller types or different types for ::mbedtls_mps_size_t and ::mbedtls_mps_stored_size_t is not yet supported.", I suggest to use as hinted by Gilles:
#define MBEDTLS_MPS_STORED_SIZE_MAX (SIZE_MAX)
#define MBEDTLS_MPS_SIZE_MAX(SIZE_MAX)

This would have to be corrected when the support for different size is added, which is probably far in the future.

One way to go would be as follow:
#if "condition"
typedef uint32_t mbedtls_mps_stored_size_t;
#define MBEDTLS_MPS_STORED_SIZE_MAX "see limit.h"
#else
typedef uint16_t mbedtls_mps_stored_size_t;
#define MBEDTLS_MPS_STORED_SIZE_MAX "see limit.h"
#endif
Which keep the type definition and the type size constant coherent.

Best regards,

Simon

@paul-elliott-arm
Copy link
Member

Hi!

I would invite you to make a PR for this here - that would likely be the quickest resolution here, given its a very small PR.

Regards,

Paul.

@tom-cosgrove-arm
Copy link
Contributor

Actually, this would be a good use-case for a putative MBEDTLS_STATIC_ASSERT() - then we could keep the #defines the way they are (which is reasonable) (the issue is using them in #if, as the preprocessor does not know about the new types - switching to a static-type assert would have the compiler handle this)

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
None yet
Development

Successfully merging a pull request may close this issue.

4 participants