From 5259feeb7cda089523a2196248baa5395bce4b50 Mon Sep 17 00:00:00 2001 From: Arturo Borrero Gonzalez Date: Sat, 12 Apr 2014 10:40:08 +0200 Subject: expression: fix constant expression allocation on big endian When allocating a constant expression, a pointer to the data is passed to the allocation function. When the variable used to store the data is larger than the size of the data type, this fails on big endian since the most significant bytes (being zero) come first. Add a helper function to calculate the proper address for the cases where this is needed. This currently affects symbolic tables for values < u64 and payload dependency generation for protocol values < u32. Signed-off-by: Patrick McHardy --- include/utils.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'include/utils.h') diff --git a/include/utils.h b/include/utils.h index 88ee0c9c..cc5948c1 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,6 +1,7 @@ #ifndef NFTABLES_UTILS_H #define NFTABLES_UTILS_H +#include #include #include #include @@ -46,6 +47,19 @@ typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (void *)__mptr - offsetof(type,member) );}) +/** + * Return a pointer to a constant variable of a size smaller than the variable. + */ +#ifdef __LITTLE_ENDIAN_BITFIELD +#define constant_data_ptr(val, len) \ + ((void *)&(val)) +#elif defined(__BIG_ENDIAN_BITFIELD) +#define constant_data_ptr(val, len) \ + ((void *)&(val) + sizeof(val) - (len) / BITS_PER_BYTE) +#else +#error "byteorder undefined" +#endif + #define field_sizeof(t, f) (sizeof(((t *)NULL)->f)) #define array_size(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) #define div_round_up(n, d) (((n) + (d) - 1) / (d)) -- cgit v1.2.3