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

Alignment warnings when casting char* pointers #437

Closed
jphickey opened this issue Dec 19, 2019 · 0 comments · Fixed by #479 or #450
Closed

Alignment warnings when casting char* pointers #437

jphickey opened this issue Dec 19, 2019 · 0 comments · Fixed by #479 or #450
Milestone

Comments

@jphickey
Copy link
Contributor

Describe the bug
When compiling on architectures with strict alignment requirements, casts between char* (as used in some pointer arithmetic to calculate addresses) to other types triggers a warning about alignment.

To Reproduce
Build for an architecture with strict alignment requirements (SPARC, MIPS, etc).
Example compiler warning:

/home/joe/code/cfecfs/github/cfe/fsw/cfe-core/src/sb/cfe_sb_buf.c:119:30: warning: cast increases required alignment of target type [-Wcast-align]
    CFE_SB_BufferD_t    *bd = (CFE_SB_BufferD_t *)(((uint8 *)Address) - sizeof(CFE_SB_BufferD_t));

Expected behavior
Should build cleanly with no warnings

System observed on:

  • Ubuntu 18.04 LTS build host
  • MIPS cross compiler (mips-poky-linux-gcc version 8.2.0)

Additional context
Many of these are actually safe because CFE ensures that the sizeof(CFE_SB_BufferD_t) as well as the base/pool addresses are aligned for the largest data types. However, because the pointer arithmetic is done as a unsigned char* the compiler sees the cast from an unsigned char* to a larger type as an issue.

Reporter Info
Joseph Hickey, Vantage Systems, Inc.

jphickey added a commit to jphickey/cFE that referenced this issue Dec 20, 2019
On CPUs with strict alignment requirements, some CFE code
that uses a char-type pointer (e.g. uint8*) to compute
memory addresses triggers an alignment warning when it gets
cast back to the actual data type.

This code should be alignment-safe already, because the address
computation already takes CPU alignment requirements into account
when calculating the addresses/offsets.

However, the compiler still flags the final conversion from a
pointer with no special alignment to something with alignment
requirements.

- For the CFE_SB pool buffers, using the `cpuaddr` type, which
  is integer in nature, avoids the warning.
- For the CFE_TBL internal table pointer, use a `void*` internally
  to store the buffer pointer, rather than a `uint8_t*`.  This
  changes the casting needs elsewhere.
skliper pushed a commit that referenced this issue Jan 10, 2020
On CPUs with strict alignment requirements, some CFE code
that uses a char-type pointer (e.g. uint8*) to compute
memory addresses triggers an alignment warning when it gets
cast back to the actual data type.

This code should be alignment-safe already, because the address
computation already takes CPU alignment requirements into account
when calculating the addresses/offsets.

However, the compiler still flags the final conversion from a
pointer with no special alignment to something with alignment
requirements.

- For the CFE_SB pool buffers, using the `cpuaddr` type, which
  is integer in nature, avoids the warning.
- For the CFE_TBL internal table pointer, use a `void*` internally
  to store the buffer pointer, rather than a `uint8_t*`.  This
  changes the casting needs elsewhere.
skliper pushed a commit that referenced this issue Jan 10, 2020
On CPUs with strict alignment requirements, some CFE code
that uses a char-type pointer (e.g. uint8*) to compute
memory addresses triggers an alignment warning when it gets
cast back to the actual data type.

This code should be alignment-safe already, because the address
computation already takes CPU alignment requirements into account
when calculating the addresses/offsets.

However, the compiler still flags the final conversion from a
pointer with no special alignment to something with alignment
requirements.

- For the CFE_SB pool buffers, using the `cpuaddr` type, which
  is integer in nature, avoids the warning.
- For the CFE_TBL internal table pointer, use a `void*` internally
  to store the buffer pointer, rather than a `uint8_t*`.  This
  changes the casting needs elsewhere.
skliper added a commit that referenced this issue Jan 10, 2020
Fix #259 #425 #427 #435 #437 #438 #443 #445
Reviewed and approved at 2020-01-08 CCB
skliper added a commit that referenced this issue Jan 14, 2020
Fix #437: Avoid alignment warnings on some CPUs
@skliper skliper linked a pull request Sep 24, 2021 that will close this issue
@skliper skliper added this to the 6.8.0 milestone Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants