Skip to content

Rename BEFORE_COLON/BC to avoid conflicts #68

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

Merged

Conversation

stgloorious
Copy link
Contributor

Description

Rename the BEFORE_COLON and BC defines to include a MBEDTLS_ prefix and expand the BC to BEFORE_COLON.
The existing names are rather short and are not properly namespaced, increasing the likelihood for conflicts.

The defines are not even used in x509_crl.c, so remove them entirely there.

One example is the Renesas RA HAL headers that have variables named BC. Those headers are included into the mbedtls module via the sys/socket.h and arpa/inet.h headers in library/x509.crt.c which will eventually include CPU-specific headers containing the Renesas HAL. The conflict can be triggered with the https sample: west build -p auto -b ek_ra6m4 zephyr/samples/net/sockets/http_server, which will lead to

/home/stefan/Downloads/zephyr_test/modules/crypto/mbedtls/library/x509_crt.c:1750:25: error: expected identifier or '(' before string constant
 1750 | #define BC              "18"
      |                         ^~~~
/home/stefan/Downloads/zephyr_test/modules/hal/renesas/drivers/ra/fsp/src/bsp/cmsis/Device/RENESAS/Include/R7FA6M4AF.h:8100:27: note: in expansion of macro 'BC'
 8100 |             __IOM uint8_t BC   : 3;    /*!< [2..0] Bit Counter                                                        */
      |                           ^~

The include chain can be investigated by copying the GCC command, removing the -M* options, and adding -H.

Arguably, this conflict can also be solved by not including the socket.h and inet.h headers, e.g., by adding a special #ifdef __ZEPHYR__ case that defaults to the software version of inet_pton() just as it is done for Win32.
It could also be solved in the Renesas HAL header, but it makes more sense to me to fix the #define.

Consider this an RFC. Let me know if you have any better ideas to fix this. If this is accepted here I will submit it upstream as well.

PR checklist

  • changelog not required
  • 3.6 backport not required
  • 2.28 backport not required
  • tests not required

Copy link
Collaborator

@tomi-font tomi-font left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, those changes should be submitted to upstream Mbed TLS: https://github.com/Mbed-TLS/mbedtls/pulls
They can then be cherry-picked into Zephyr.

@stgloorious
Copy link
Contributor Author

The patches were merged upstream 243e13f. Please consider cherry-picking the commits or merging this PR now.

Copy link
Collaborator

@tomi-font tomi-font left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Can you now just cherry pick the commits directly from upstream with the -x flag so we keep track of the commit hashes (useful for when updating this repo)?

@tomi-font
Copy link
Collaborator

You'll also need to make a PR in Zephyr if you want that change to land into main there.

@stgloorious stgloorious force-pushed the stgloor/fix/before_colon branch from d5062f6 to c9bebc9 Compare February 27, 2025 14:44
Copy link
Collaborator

@tomi-font tomi-font left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not quite right yet. The title of the commit should be identical to that of the upstream one. You shouldn't be using git merge, really just git cherry-pick.
Feel free to contact me on the Zephyr Discord if you need help doing that.

@stgloorious
Copy link
Contributor Author

This is the merge commit from upstream (243e13f). I did

git cherry-pick -x upstream/development 243e13fb2a527d0df342c9a488209849120da558 -m 1

to pull it in. I think you want to see only my two commits, and not the merge commit? I tried doing that but it would always pull in the merge commit instead.

Thanks for the offer. I will try again as soon as I get back to work and then write you in case I don't manage to do it.

@tomi-font
Copy link
Collaborator

Ah yeah, kinda had forgotten that Mbed TLS merges PRs with merge commits.
Basically instead of cherry picking the merge commit you want to cherry pick your own commits, Mbed-TLS/mbedtls@6a9cf11 and Mbed-TLS/mbedtls@b5c079b.
So the full command you should use is: git cherry-pick -sx 6a9cf113611de1d8ac18f49563883a639ae7c7d6 b5c079b13c4977bdba8593d174d7851e41b5788e

BEFORE_COLON and BC defines with the accompanying comment are only
required in x509_crt and x509_csr, but not used in x509_crl.c.

Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
(cherry picked from commit 6a9cf113611de1d8ac18f49563883a639ae7c7d6)
Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
Namespace BEFORE_COLON and BC defines by prepending MBEDTLS_
and expanding BC to BEFORE_COLON_STR. This is to avoid naming
conflicts with third-party code. No functional change.

Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
(cherry picked from commit b5c079b13c4977bdba8593d174d7851e41b5788e)
Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
@stgloorious stgloorious force-pushed the stgloor/fix/before_colon branch from c9bebc9 to 4952e13 Compare February 28, 2025 09:40
@stgloorious stgloorious reopened this Feb 28, 2025
@stgloorious
Copy link
Contributor Author

Thanks, I think it worked now.

Copy link
Collaborator

@tomi-font tomi-font left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect!

@facchinm
Copy link
Contributor

facchinm commented Mar 5, 2025

We (Arduino) are impacted too by this bug while porting Portenta C33 (RA6M5 based) with networking, thanks @stgloorious for providing the fix!

@tomi-font
Copy link
Collaborator

tomi-font commented Mar 6, 2025

Bear in mind (any of) you will need to make a PR in the main Zephyr repo that changes the reference to this repo (in the west.yml file) to this PR (pull/68/head) in order to integrate the changes into Zephyr.
see https://docs.zephyrproject.org/latest/develop/modules.html#process-for-submitting-changes-to-existing-modules

stgloorious added a commit to stgloorious/zephyr that referenced this pull request Mar 6, 2025
See: zephyrproject-rtos/mbedtls#68

Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
@stgloorious
Copy link
Contributor Author

Done. Please consider merging this PR now, so I can update the hash in zephyrproject-rtos/zephyr#86704.

@tomi-font
Copy link
Collaborator

We first need to see that CI agrees. 🙂

@tomi-font tomi-font merged commit 3bc59ad into zephyrproject-rtos:zephyr Mar 7, 2025
facchinm pushed a commit to facchinm/zephyr that referenced this pull request Mar 7, 2025
See: zephyrproject-rtos/mbedtls#68

Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
stgloorious added a commit to stgloorious/zephyr that referenced this pull request Mar 7, 2025
See: zephyrproject-rtos/mbedtls#68

Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
facchinm pushed a commit to facchinm/zephyr that referenced this pull request Mar 7, 2025
See: zephyrproject-rtos/mbedtls#68

Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
kartben pushed a commit to zephyrproject-rtos/zephyr that referenced this pull request Mar 10, 2025
See: zephyrproject-rtos/mbedtls#68

Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
rbudai98 pushed a commit to rbudai98/zephyr_robi that referenced this pull request Mar 17, 2025
See: zephyrproject-rtos/mbedtls#68

Signed-off-by: Stefan Gloor <stefan.gloor@siemens.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants