From 444587d8a3c4b91421cd4cfd7bda2e3f4d1aebe4 Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Tue, 16 Jan 2018 23:00:21 +0100 Subject: BIT field woes * get_rec_bits() was always reading two bytes, even if the bit field contained only of one byte * In various places the code used field->pack_length() bytes starting from field->ptr, while it should be field->pack_length_in_rec() * Field_bit::key_cmp and Field_bit::cmp_max passed field_length as an argument to memcmp(), but field_length is the number of bits! --- include/my_compare.h | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'include/my_compare.h') diff --git a/include/my_compare.h b/include/my_compare.h index 0db22b593f4..87f0aa8f3ca 100644 --- a/include/my_compare.h +++ b/include/my_compare.h @@ -91,17 +91,19 @@ typedef struct st_HA_KEYSEG /* Key-portion */ #define size_to_store_key_length(length) ((length) < 255 ? 1 : 3) -#define get_rec_bits(bit_ptr, bit_ofs, bit_len) \ - (((((uint16) (bit_ptr)[1] << 8) | (uint16) (bit_ptr)[0]) >> (bit_ofs)) & \ - ((1 << (bit_len)) - 1)) - -#define set_rec_bits(bits, bit_ptr, bit_ofs, bit_len) \ -{ \ - (bit_ptr)[0]= ((bit_ptr)[0] & ~(((1 << (bit_len)) - 1) << (bit_ofs))) | \ - ((bits) << (bit_ofs)); \ - if ((bit_ofs) + (bit_len) > 8) \ - (bit_ptr)[1]= ((bit_ptr)[1] & ~((1 << ((bit_len) - 8 + (bit_ofs))) - 1)) | \ - ((bits) >> (8 - (bit_ofs))); \ +static inline uint16 get_rec_bits(const uchar *ptr, uchar ofs, uint len) +{ + uint16 val= ptr[0]; + if (ofs + len > 8) + val|= (uint16)(ptr[1]) << 8; + return (val >> ofs) & ((1 << len) - 1); +} + +static inline void set_rec_bits(uint16 bits, uchar *ptr, uchar ofs, uint len) +{ + ptr[0]= (ptr[0] & ~(((1 << len) - 1) << ofs)) | (bits << ofs); + if (ofs + len > 8) + ptr[1]= (ptr[1] & ~((1 << (len - 8 + ofs)) - 1)) | (bits >> (8 - ofs)); } #define clr_rec_bits(bit_ptr, bit_ofs, bit_len) \ -- cgit v1.2.1