From 9ad2f879463a33f00793d2f8c0f27ca227aca468 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Tue, 14 Jul 2015 19:36:15 -0400 Subject: [PATCH] Fix: require power of two for type alignment MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- formats/ctf/ir/event-types.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/formats/ctf/ir/event-types.c b/formats/ctf/ir/event-types.c index df117474..da0ecd6d 100644 --- a/formats/ctf/ir/event-types.c +++ b/formats/ctf/ir/event-types.c @@ -1780,14 +1780,20 @@ end: return ret; } +static inline +int is_power_of_two(unsigned int value) +{ + return ((value & (value - 1)) == 0) && value > 0; +} + int bt_ctf_field_type_set_alignment(struct bt_ctf_field_type *type, unsigned int alignment) { int ret = 0; enum ctf_type_id type_id; - /* Alignment must be bit-aligned (1) or byte aligned */ - if (!type || type->frozen || (alignment != 1 && (alignment & 0x7))) { + /* Alignment must be a power of two */ + if (!type || type->frozen || !is_power_of_two(alignment)) { ret = -1; goto end; } -- 2.34.1