Skip to content

Commit 65febcc

Browse files
committed
Fix Bug#24707869 GCC 5 AND 6 MISCOMPILE MACH_PARSE_COMPRESSED
Prevent GCC from moving a mach_read_from_4() before we have checked that we have 4 bytes to read. The pointer may only point to a 1, 2 or 3 bytes in which case the code should not read 4 bytes. This is a workaround to a GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77673 Patch submitted by: Laurynas Biveinis <[email protected]> RB: 14135 Reviewed by: Pawel Olchawa <[email protected]>
1 parent da97aa6 commit 65febcc

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

storage/innobase/mach/mach0data.c

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*****************************************************************************
22
3-
Copyright (c) 1995, 2009, Innobase Oy. All Rights Reserved.
3+
Copyright (c) 1995, 2016, Oracle and/or its affiliates. All Rights Reserved.
44
55
This program is free software; you can redistribute it and/or modify it under
66
the terms of the GNU General Public License as published by the Free Software
@@ -55,40 +55,71 @@ mach_parse_compressed(
5555
if (flag < 0x80UL) {
5656
*val = flag;
5757
return(ptr + 1);
58+
}
59+
60+
/* Workaround GCC bug
61+
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77673:
62+
the compiler moves mach_read_from_4 right to the beginning of the
63+
function, causing and out-of-bounds read if we are reading a short
64+
integer close to the end of buffer. */
65+
#if defined(__GNUC__) && (__GNUC__ >= 5) && !defined(__clang__)
66+
#define DEPLOY_FENCE
67+
#endif
68+
69+
#ifdef DEPLOY_FENCE
70+
__atomic_thread_fence(__ATOMIC_ACQUIRE);
71+
#endif
5872

59-
} else if (flag < 0xC0UL) {
73+
if (flag < 0xC0UL) {
6074
if (end_ptr < ptr + 2) {
6175
return(NULL);
6276
}
6377

6478
*val = mach_read_from_2(ptr) & 0x7FFFUL;
6579

6680
return(ptr + 2);
81+
}
82+
83+
#ifdef DEPLOY_FENCE
84+
__atomic_thread_fence(__ATOMIC_ACQUIRE);
85+
#endif
6786

68-
} else if (flag < 0xE0UL) {
87+
if (flag < 0xE0UL) {
6988
if (end_ptr < ptr + 3) {
7089
return(NULL);
7190
}
7291

7392
*val = mach_read_from_3(ptr) & 0x3FFFFFUL;
7493

7594
return(ptr + 3);
76-
} else if (flag < 0xF0UL) {
95+
}
96+
97+
#ifdef DEPLOY_FENCE
98+
__atomic_thread_fence(__ATOMIC_ACQUIRE);
99+
#endif
100+
101+
if (flag < 0xF0UL) {
77102
if (end_ptr < ptr + 4) {
78103
return(NULL);
79104
}
80105

81106
*val = mach_read_from_4(ptr) & 0x1FFFFFFFUL;
82107

83108
return(ptr + 4);
84-
} else {
85-
ut_ad(flag == 0xF0UL);
109+
}
86110

87-
if (end_ptr < ptr + 5) {
88-
return(NULL);
89-
}
111+
#ifdef DEPLOY_FENCE
112+
__atomic_thread_fence(__ATOMIC_ACQUIRE);
113+
#endif
114+
115+
#undef DEPLOY_FENCE
116+
117+
ut_ad(flag == 0xF0UL);
90118

91-
*val = mach_read_from_4(ptr + 1);
92-
return(ptr + 5);
119+
if (end_ptr < ptr + 5) {
120+
return(NULL);
93121
}
122+
123+
*val = mach_read_from_4(ptr + 1);
124+
return(ptr + 5);
94125
}

0 commit comments

Comments
 (0)