<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/83228>83228</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Is it an UB here? if not, do I need a compiler barrier to save it to be well-defined?
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
zegao96
</td>
</tr>
</table>
<pre>
Suppose we have the following code sequence in C:
```
struct A { bool a; /* B if a==1 otherwise A */};
struct B { bool a; /* B if a==1 otherwise A */ int b;}
void foo(struct B *s) {
if (!s)
return;
if (s->a != 1)
return;
//do we need a compiler barrier here
//to make sure the compiler does not
//reorder access of s->b across s->a?
if (s->b != 2);
return;
...
}
void bar()
{
struct A *a = malloc(*a);
struct B *b = (struct B*)a;
foo(b);
}
```
In this case, one thing that is for sure is **s->b is only safe to access given that the condition s->a is true**. So from the compiler's POV:
1) does the type punning case in bar() makes foo() an UB even with -fno-strict-aliasing?
2) if not UB, would it happen to reorder two if branches in foo()?
3) if not UB, is a compiler barrier necessary as commented to restore this foo() to be a well-defined function?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyclEuP4jgQgH-NuZRAiU035JADDIvUp11pNHuvOBXiXeNiXU6j3l-_csKre9jLSIjIdj2_eqCIOwSiWr1s1ctuhkPqOdb_0gG5ep013H7U34fTiYXgTNDjO0HqCTr2ns8uHMBySyD0z0DBErgA35TZqGKniuv_a3H5jUdJcbAJNqBWW2iYPaAyW1B6r_QGtuC6fLFTZlcCp57i2Qllcb3JMqudMttPlra_ZglcSNBkY6vdY7jv7FromJVe3x3ojShdZUeTEABk80qvlS7z0_0aACKlIYZbnHdhmSvzG4LSpTI7KO96TzTGNPYtZ-yBqAUEy8eT8xShwRgdRegp0leNxHDEvwlkiFOpblotk0Dg9FUjEseWIqC1JALcwRhnA2gji0wnVGb_NJ3mmo7O6Txm8BTEYrG40H6CvcE4Iq2uIlfFe9PoDUL2dkTv2Y7SG_zJ82PlmlH-oZxj_Su8a0zVbh6t3KP73L1vAVLvBCwKKf0NOGTIeQ5SjwmcQMdxYu9k6rTNBZMT4OA_QLAjSHzFfXDvFCbtqVqhdclxuGDPaikONJlawHeGLvLxU2WVXgn88fufXwYv99dU9CycPk4EpyGEcWhRxlm9AR97Ri4k8hkD_NgC5djOLvUw7wLPJUVn0xy9Q3HhcGuJXPrcE4ET_NhmLmcefAsuQY-nU86P4dpm6cxZtokYbE-Sw7i5VWYPk0nzk0knz0YgUKaI8QNQ8uuRQqJ28ieJxxlwj4klhoYA4Uzez1vqXKAWuiHYDF2Z_aytTVuZCmdUl6uiKgttinLW168vqxerNVJnqs5arJbLZvVKVndVsSzty8zVutDLQut18WrWulygtdaUpisJl4jVSi0LOqLzC-_fjwuOh5kTGaheG63XM48NeRkXsdaBzjA-Kq3zXo511pk3w0HUsvBOktytJJc81W-SeU91GxeD2V8AZnotw9v_7pHEIHmzu3TB8wgnMxmir_uUTpJbbFwaB5f6oVlYPiq9z5FcPvNT5L_IJqX3Y_yi9H7M778AAAD__5Cx5ck">