<div dir="ltr">I'm not sure that is undefined behavior, but it is caught by -fsanitize=integer<div><br clear="all"><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature">~Craig</div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jan 20, 2022 at 11:06 PM phy coder via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">And if I'm wrong or misunderstand something then please let me know.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jan 21, 2022, 12:00 phy coder <<a href="mailto:coderphy3@gmail.com" target="_blank">coderphy3@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I'm not sure if this is undefined behavior or not and it may be discussed before or not. I was testing some random code snippets to try "-fsanitize=undefined" but in this example:<br></div><div><br></div><div><div style="color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:"Droid Sans Mono","monospace",monospace,"Droid Sans Fallback";font-weight:normal;font-size:20px;line-height:27px;white-space:pre-wrap"><div><span style="color:rgb(197,134,192)">#include</span><span style="color:rgb(206,145,120)"><iostream></span></div><br><div><span style="color:rgb(86,156,214)">char</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">func</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">a</span><span style="color:rgb(212,212,212)">, </span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">b</span><span style="color:rgb(212,212,212)">) {</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">sum</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(156,220,254)">a</span><span style="color:rgb(212,212,212)">+</span><span style="color:rgb(156,220,254)">b</span><span style="color:rgb(212,212,212)">;</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(197,134,192)">return</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">sum</span><span style="color:rgb(212,212,212)"> ;</span></div><div><span style="color:rgb(212,212,212)">}</span></div><br><div><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)">main</span><span style="color:rgb(212,212,212)">() {</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">n</span><span style="color:rgb(212,212,212)">;</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">char</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">s</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(220,220,170)">func</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(181,206,168)">100</span><span style="color:rgb(212,212,212)">,</span><span style="color:rgb(181,206,168)">100</span><span style="color:rgb(212,212,212)">);</span></div><div><span style="color:rgb(212,212,212)">    </span><span style="color:rgb(86,156,214)">int</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)"> = </span><span style="color:rgb(220,220,170)">func</span><span style="color:rgb(212,212,212)">(</span><span style="color:rgb(181,206,168)">100</span><span style="color:rgb(212,212,212)">,</span><span style="color:rgb(181,206,168)">100</span><span style="color:rgb(212,212,212)">);</span></div><div><span style="color:rgb(212,212,212)">   </span><span style="color:rgb(78,201,176)">std</span><span style="color:rgb(212,212,212)">::</span><span style="color:rgb(156,220,254)">cout</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)"><<</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(206,145,120)">"hello"</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)"><<</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">s</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(220,220,170)"><<</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(156,220,254)">t</span><span style="color:rgb(212,212,212)">;</span></div><div><span style="color:rgb(212,212,212)">  </span><span style="color:rgb(197,134,192)">return</span><span style="color:rgb(212,212,212)"> </span><span style="color:rgb(181,206,168)">0</span><span style="color:rgb(212,212,212)">;</span></div><br><div><span style="color:rgb(212,212,212)">}</span></div><div><span style="color:rgb(212,212,212)"><br></span></div></div></div><div>I was expecting some kind of undefined behavior here because of some overflowing the char return type with integer return type. But "-fsanitize=undefined" is not able to catch this . And I got the output this : hello�-56 <br></div><div>Kinda weird for me . <br></div></div>
</blockquote></div>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>