diff options
author | Thomas Haller <thaller@redhat.com> | 2020-10-13 09:34:04 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-10-21 10:38:25 +0200 |
commit | 143130066be214280a3c6d4abc4cfe97c10a5f6f (patch) | |
tree | 144d1ce99ee406bd73c0e58d2a0cd4723e143265 | |
parent | b6d6a16b2a1c5e4593ba5ff22a2e11f23dca93c9 (diff) | |
download | NetworkManager-143130066be214280a3c6d4abc4cfe97c10a5f6f.tar.gz |
c-rbtree: fix struct alignment of CRBTree on m68k architectures
On m68k, 32bit integer are aligned to only 2 bytes. This breaks
assumptions and a static assertion of c-rbtree.
Explicitly require that the first field is aligned to at least 4 bytes.
This fixes the build and ensures that all valid pointers to a CRBTree have
the lowest two bits unset (so they can be used for storing 2 additional flags).
Use a union instead of aligning __parent_and_flags itself. That is
because alignas() cannot lower the natural alignment, so if we would
want to align __parent_and_flags, we could only do
alignas(sizeof(unsigned long) > 4 ? sizeof(unsigned long) : 4)
That would not be correct if "long" is 8 bytes long but had a natural
alignment of only 4. The union allows us to specify an alignment
of at least 4, but otherwise follow natural alignment.
https://github.com/c-util/c-rbtree/commit/10d973a9e6718516a6839c8b87a3e630c5391d53
-rw-r--r-- | shared/c-rbtree/src/c-rbtree.h | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/shared/c-rbtree/src/c-rbtree.h b/shared/c-rbtree/src/c-rbtree.h index a9bbce52f2..d1941185fb 100644 --- a/shared/c-rbtree/src/c-rbtree.h +++ b/shared/c-rbtree/src/c-rbtree.h @@ -27,6 +27,7 @@ extern "C" { #endif #include <assert.h> +#include <stdalign.h> #include <stddef.h> typedef struct CRBNode CRBNode; @@ -58,7 +59,10 @@ typedef struct CRBTree CRBTree; * C_RBNODE_INIT. */ struct CRBNode { - unsigned long __parent_and_flags; + union { + unsigned long __parent_and_flags; + alignas(4) char __dmmy_for_struct_alignment; + }; CRBNode *left; CRBNode *right; }; |