Skip to content

platform: fix -Wcast-align warnings in memory_buffer_alloc.c#743

Merged
valeriosetti merged 1 commit into
Mbed-TLS:developmentfrom
M-Moawad:moawad-fix-wcast-align-memory-buffer-alloc
Apr 22, 2026
Merged

platform: fix -Wcast-align warnings in memory_buffer_alloc.c#743
valeriosetti merged 1 commit into
Mbed-TLS:developmentfrom
M-Moawad:moawad-fix-wcast-align-memory-buffer-alloc

Conversation

@M-Moawad
Copy link
Copy Markdown
Contributor

@M-Moawad M-Moawad commented Apr 8, 2026

Cast from 'unsigned char *' to 'memory_header *' through an intermediate 'void *' to suppress -Wcast-align warnings.

Some Clang-based toolchains (e.g. MetaWare/ARC) enable -Wcast-align as part of -Wall, unlike standard Clang on x86/ARM. Combined with -Werror this turns the casts into fatal build errors.

The casts are already alignment-safe at runtime:

  • In mbedtls_memory_buffer_alloc_init(), buf is explicitly aligned to MBEDTLS_MEMORY_ALIGN_MULTIPLE before the cast.
  • In buffer_alloc_calloc(), p is computed from an aligned base plus aligned offsets (sizeof(memory_header) and len are both multiples of MBEDTLS_MEMORY_ALIGN_MULTIPLE).
  • In buffer_alloc_free(), p is derived from a previously aligned allocation pointer minus the aligned header size.

Original issue reported on zephyr: zephyrproject-rtos/zephyr#106712

PR checklist

@valeriosetti
Copy link
Copy Markdown
Contributor

@M-Moawad I suspect that using "Fixes" in the description of this PR will close the pointed issue once this PR is merged which is not correct. That issue will only be closed when this PR is cherry-picked to Zephyr fork of this repo

@M-Moawad
Copy link
Copy Markdown
Contributor Author

M-Moawad commented Apr 8, 2026

Modified

@valeriosetti valeriosetti self-requested a review April 8, 2026 15:12
@valeriosetti valeriosetti added needs-review Every commit must be reviewed by at least two team members needs-reviewer This PR needs someone to pick it up for review priority-high High priority - will be reviewed soon size-xs Estimated task size: extra small (a few hours at most) labels Apr 8, 2026
Comment thread platform/memory_buffer_alloc.c
Cast from 'unsigned char *' to 'memory_header *' through an
intermediate 'void *' to suppress -Wcast-align warnings.

Some Clang-based toolchains (e.g. MetaWare/ARC) enable -Wcast-align
as part of -Wall, unlike standard Clang on x86/ARM. Combined with
-Werror this turns the casts into fatal build errors.

The casts are already alignment-safe at runtime:
- In mbedtls_memory_buffer_alloc_init(), buf is explicitly aligned
  to MBEDTLS_MEMORY_ALIGN_MULTIPLE before the cast.
- In buffer_alloc_calloc(), p is computed from an aligned base plus
  aligned offsets (sizeof(memory_header) and len are both multiples
  of MBEDTLS_MEMORY_ALIGN_MULTIPLE).
- In buffer_alloc_free(), p is derived from a previously aligned
  allocation pointer minus the aligned header size.

Signed-off-by: Mohamed Moawad <moawad@synopsys.com>
@M-Moawad M-Moawad force-pushed the moawad-fix-wcast-align-memory-buffer-alloc branch from 868fb8c to b16feb7 Compare April 8, 2026 16:15
Copy link
Copy Markdown
Contributor

@gilles-peskine-arm gilles-peskine-arm left a comment

Choose a reason for hiding this comment

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

LGTM

At runtime, we know that the start of the heap is aligned to MBEDTLS_MEMORY_ALIGN_MULTIPLE, which is supposed to be good enough for the platform's alignment requirements. So the warning is spurious, but the compiler can't know. This patch silences the warning in a sensible way.

OK to not have a changelog entry since silencing compiler warnings is on a best effort basis and this one doesn't have an issue on our bug tracker.

@gilles-peskine-arm gilles-peskine-arm added approved Design and code approved - may be waiting for CI or backports needs-backports Backports are missing or are pending review and approval. and removed needs-review Every commit must be reviewed by at least two team members needs-reviewer This PR needs someone to pick it up for review labels Apr 8, 2026
@gilles-peskine-arm
Copy link
Copy Markdown
Contributor

Thanks for the patch! Can you please backport it to the tf-psa-crypto-1.1 long-time support branch, and to the mbedtls-3.6 LTS branch on https://github.com/Mbed-TLS/mbedtls ?

@valeriosetti
Copy link
Copy Markdown
Contributor

Thanks for the patch! Can you please backport it to the tf-psa-crypto-1.1 long-time support branch, and to the mbedtls-3.6 LTS branch on https://github.com/Mbed-TLS/mbedtls ?

I did those:

@valeriosetti valeriosetti added this pull request to the merge queue Apr 22, 2026
Merged via the queue into Mbed-TLS:development with commit 5ed7d72 Apr 22, 2026
3 checks passed
@github-project-automation github-project-automation Bot moved this from Triage in to Done in Community Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Design and code approved - may be waiting for CI or backports needs-backports Backports are missing or are pending review and approval. priority-high High priority - will be reviewed soon size-xs Estimated task size: extra small (a few hours at most)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants