Skip to content

Alignment warnings when casting char* pointers #437

@jphickey

Description

@jphickey

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions