<div dir="ltr"><div>Is it actually undefined behavior? </div><div>What does the standard say about " int32_value >> int64_value" ?</div><div>If int64_value should be first cast to int32, then there is no UB in:</div><div><div style="font-size:12.8px">    res += 123 >> l;</div></div><div style="font-size:12.8px"><br></div><div>As for the first buggy line, there is a FE warning: </div><div><div>b.c:6:19: warning: shift count >= width of type [-Wshift-count-overflow]</div><div>    int res = 123 >> 0x100000000ULL;</div></div><div><br></div><div>--kcc </div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Dec 12, 2016 at 2:46 PM, Dmitry Babokin <span dir="ltr"><<a href="mailto:babokin@gmail.com" target="_blank">babokin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello,<div><br></div><div>I'm using ubsan together with creduce to ensure that reduced test doesn't have UB. And I've noticed that ubsan is not detecting UB in case of large shift amount, particularly when rhs of shift has larger type that lhs. To reproduce, rhs also has to have the value that is non-zero, but after truncation to lhs type the value becomes zero.</div><div><br></div><div>Consider the following example.</div><div><br></div><div><div>#include <stdio.h></div><div><br></div><div>// 1 is in 33rd bit.</div><div>unsigned long long l = 0x100000000ULL;</div><div><br></div><div>int main() {</div><div>    // Ubsan doesn't fire</div><div>    int res = 123 >> 0x100000000ULL;</div><div>    // Ubsan doesn't fire</div><div>    res += 123 >> l;</div><div>    // Ubsan does fire</div><div>    res += 123ULL >> l;</div><div>    printf("result = %d\n", res);</div><div>    return 0;</div><div>}</div></div><div><br></div><div>Changing the constant to the value, which fit 32 bits makes ubsan firing.</div><div><br></div><div>I understand where the problem comes from - LLVM IR definition requires both operands of the shift to be of the same integer type. And ubsan actually check already truncated value. But it doesn't match C/C++ standard definition of UB in shift operation.</div><div><br></div><div>Is it possible to fix in current infrastructure? Should I file a bug for this problem?</div><div><br></div><div>Note, it's not a theoretical problem, it's very practical one, which pops up during automatic test reduction relatively frequently for me.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Dmitry.</div></font></span></div>
</blockquote></div><br></div>