Skip to content

Commit e6b1f0f

Browse files
committed
Avoid misaligned access using sanitizer
To write network data some macros handling misaligned access are used. Code correctly detect architectures handling correctly not aligned access and use it. However instrumented code could detect this as errors. Use a different way able to tell compiler that these misaligned access are what we want. Signed-off-by: Frediano Ziglio <[email protected]>
1 parent 3137078 commit e6b1f0f

File tree

1 file changed

+73
-17
lines changed

1 file changed

+73
-17
lines changed

include/freetds/bytes.h

+73-17
Original file line numberDiff line numberDiff line change
@@ -150,36 +150,92 @@ typedef union {
150150
# define TDS_HOST4BE(val) TDS_BYTE_SWAP32(val)
151151
#endif
152152

153-
/* these platform support unaligned fetch/store */
154-
/* map unaligned macro to aligned ones */
155-
#if defined(__i386__) || defined(__amd64__) || defined(__CRIS__) ||\
156-
defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) ||\
157-
defined(__s390__) || defined(__s390x__) || defined(__m68k__) ||\
158-
(defined(_MSC_VER) && (defined(_M_AMD64) || defined(_M_IX86) || defined(_M_X64))) ||\
159-
defined(__ARM_FEATURE_UNALIGNED) ||\
160-
defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_8__) ||\
161-
(defined(_M_ARM) && (_M_ARM >= 7))
153+
#if defined(__GNUC__) || defined(_MSC_VER)
154+
# if defined(__MINGW32__)
155+
# pragma pack(push,1)
156+
# elif defined(_MSC_VER)
157+
# pragma pack(push)
158+
# pragma pack(1)
159+
# endif
160+
161+
# if defined(__GNUC__)
162+
# define TDS_PACKED __attribute__((__packed__))
163+
# else
164+
# define TDS_PACKED
165+
# endif
166+
167+
typedef union TDS_PACKED
168+
{
169+
uint16_t usi;
170+
uint8_t uc[2];
171+
} TDS_MAY_ALIAS TDS_UNALIGNED_BYTE_CONVERT2;
172+
173+
typedef union TDS_PACKED
174+
{
175+
uint32_t ui;
176+
uint8_t uc[4];
177+
} TDS_MAY_ALIAS TDS_UNALIGNED_BYTE_CONVERT4;
178+
162179
# ifdef WORDS_BIGENDIAN
163180
# undef TDS_GET_UA2BE
164181
# undef TDS_GET_UA4BE
165-
# define TDS_GET_UA2BE(ptr) TDS_GET_A2BE(ptr)
166-
# define TDS_GET_UA4BE(ptr) TDS_GET_A4BE(ptr)
182+
# define TDS_GET_UA2BE(ptr) (((TDS_UNALIGNED_BYTE_CONVERT2*)(ptr))->usi)
183+
# define TDS_GET_UA4BE(ptr) (((TDS_UNALIGNED_BYTE_CONVERT4*)(ptr))->ui)
167184

168185
# undef TDS_PUT_UA2BE
169186
# undef TDS_PUT_UA4BE
170-
# define TDS_PUT_UA2BE(ptr,val) TDS_PUT_A2BE(ptr,val)
171-
# define TDS_PUT_UA4BE(ptr,val) TDS_PUT_A4BE(ptr,val)
187+
# define TDS_PUT_UA2BE(ptr,val) (((TDS_UNALIGNED_BYTE_CONVERT2*)(ptr))->usi = (val))
188+
# define TDS_PUT_UA4BE(ptr,val) (((TDS_UNALIGNED_BYTE_CONVERT4*)(ptr))->ui = (val))
172189
# else
173190
# undef TDS_GET_UA2LE
174191
# undef TDS_GET_UA4LE
175-
# define TDS_GET_UA2LE(ptr) TDS_GET_A2LE(ptr)
176-
# define TDS_GET_UA4LE(ptr) TDS_GET_A4LE(ptr)
192+
# define TDS_GET_UA2LE(ptr) (((TDS_UNALIGNED_BYTE_CONVERT2*)(ptr))->usi)
193+
# define TDS_GET_UA4LE(ptr) (((TDS_UNALIGNED_BYTE_CONVERT4*)(ptr))->ui)
177194

178195
# undef TDS_PUT_UA2LE
179196
# undef TDS_PUT_UA4LE
180-
# define TDS_PUT_UA2LE(ptr,val) TDS_PUT_A2LE(ptr,val)
181-
# define TDS_PUT_UA4LE(ptr,val) TDS_PUT_A4LE(ptr,val)
197+
# define TDS_PUT_UA2LE(ptr,val) (((TDS_UNALIGNED_BYTE_CONVERT2*)(ptr))->usi = (val))
198+
# define TDS_PUT_UA4LE(ptr,val) (((TDS_UNALIGNED_BYTE_CONVERT4*)(ptr))->ui = (val))
182199
# endif
200+
201+
# if defined(__MINGW32__) || defined(_MSC_VER)
202+
# pragma pack(pop)
203+
# endif
204+
205+
#else
206+
207+
/* these platform support unaligned fetch/store */
208+
/* map unaligned macro to aligned ones */
209+
# if defined(__i386__) || defined(__amd64__) || defined(__CRIS__) ||\
210+
defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) ||\
211+
defined(__s390__) || defined(__s390x__) || defined(__m68k__) ||\
212+
(defined(_MSC_VER) && (defined(_M_AMD64) || defined(_M_IX86) || defined(_M_X64))) ||\
213+
defined(__ARM_FEATURE_UNALIGNED) ||\
214+
defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_8__) ||\
215+
(defined(_M_ARM) && (_M_ARM >= 7))
216+
# ifdef WORDS_BIGENDIAN
217+
# undef TDS_GET_UA2BE
218+
# undef TDS_GET_UA4BE
219+
# define TDS_GET_UA2BE(ptr) TDS_GET_A2BE(ptr)
220+
# define TDS_GET_UA4BE(ptr) TDS_GET_A4BE(ptr)
221+
222+
# undef TDS_PUT_UA2BE
223+
# undef TDS_PUT_UA4BE
224+
# define TDS_PUT_UA2BE(ptr,val) TDS_PUT_A2BE(ptr,val)
225+
# define TDS_PUT_UA4BE(ptr,val) TDS_PUT_A4BE(ptr,val)
226+
# else
227+
# undef TDS_GET_UA2LE
228+
# undef TDS_GET_UA4LE
229+
# define TDS_GET_UA2LE(ptr) TDS_GET_A2LE(ptr)
230+
# define TDS_GET_UA4LE(ptr) TDS_GET_A4LE(ptr)
231+
232+
# undef TDS_PUT_UA2LE
233+
# undef TDS_PUT_UA4LE
234+
# define TDS_PUT_UA2LE(ptr,val) TDS_PUT_A2LE(ptr,val)
235+
# define TDS_PUT_UA4LE(ptr,val) TDS_PUT_A4LE(ptr,val)
236+
# endif
237+
# endif
238+
183239
#endif
184240

185241
#undef TDS_BSWAP16

0 commit comments

Comments
 (0)