<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.apple-style-span
        {mso-style-name:apple-style-span;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Right, exactly.  To Phy: The math is being done as ‘int’, where there IS no overflow.  The narrowing conversion is implementation defined, so that isn’t UB.<o:p></o:p></p>
<p class="MsoNormal"><br>
Note that EVEN IF you change all of the ‘int’ to ‘char’ in ‘func’, the math ‘a+b’ is still done as ‘integer’ by rule, then narrowed, so you won’t get the overflow sanitizer here unless your inputs overflow the ‘int’ type.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> cfe-dev <cfe-dev-bounces@lists.llvm.org> <b>On Behalf Of
</b>Jessica Clarke via cfe-dev<br>
<b>Sent:</b> Friday, January 21, 2022 7:07 AM<br>
<b>To:</b> Craig Topper <craig.topper@gmail.com><br>
<b>Cc:</b> clang developer list <cfe-dev@lists.llvm.org><br>
<b>Subject:</b> Re: [cfe-dev] Function return not catching the undefined behavior<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">This is implementation-defined, not undefined. C99 6.3.1.3p3:<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">  Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.<o:p></o:p></p>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="color:black"><br>
<span class="apple-style-span">Jess</span><o:p></o:p></span></p>
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">On 21 Jan 2022, at 07:16, Craig Topper via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">I'm not sure that is undefined behavior, but it is caught by -fsanitize=integer<o:p></o:p></p>
<div>
<p class="MsoNormal"><br clear="all">
<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal">~Craig<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">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:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<p class="MsoNormal">And if I'm wrong or misunderstand something then please let me know.<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Fri, Jan 21, 2022, 12:00 phy coder <<a href="mailto:coderphy3@gmail.com" target="_blank">coderphy3@gmail.com</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<div>
<div>
<p class="MsoNormal">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:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#C586C0">#include</span><span style="font-size:15.0pt;font-family:"Courier New";color:#CE9178"><iostream></span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4"><o:p></o:p></span></p>
</div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#569CD6">char</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#DCDCAA">func</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">(</span><span style="font-size:15.0pt;font-family:"Courier New";color:#569CD6">int</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">a</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">,
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#569CD6">int</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">b</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">) {<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#569CD6">int</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">sum</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4"> =
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">a</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">+</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">b</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#C586C0">return</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">sum</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4"> ;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">}<o:p></o:p></span></p>
</div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#569CD6">int</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#DCDCAA">main</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">() {<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#569CD6">int</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">n</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#569CD6">char</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">s</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4"> =
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#DCDCAA">func</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">(</span><span style="font-size:15.0pt;font-family:"Courier New";color:#B5CEA8">100</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">,</span><span style="font-size:15.0pt;font-family:"Courier New";color:#B5CEA8">100</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">);<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#569CD6">int</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">t</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4"> =
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#DCDCAA">func</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">(</span><span style="font-size:15.0pt;font-family:"Courier New";color:#B5CEA8">100</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">,</span><span style="font-size:15.0pt;font-family:"Courier New";color:#B5CEA8">100</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">);<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#4EC9B0">std</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">::</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">cout</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#DCDCAA"><<</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#CE9178">"hello"</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#DCDCAA"><<</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">s</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#DCDCAA"><<</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#9CDCFE">t</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">;<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#C586C0">return</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">
</span><span style="font-size:15.0pt;font-family:"Courier New";color:#B5CEA8">0</span><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">;<o:p></o:p></span></p>
</div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4">}<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="line-height:20.25pt;background:#1E1E1E"><span style="font-size:15.0pt;font-family:"Courier New";color:#D4D4D4"><o:p> </o:p></span></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal">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<span style="font-family:"Tahoma",sans-serif">�</span>-56
<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Kinda weird for me . <o:p></o:p></p>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal">_______________________________________________<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" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal">_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev">https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><o:p></o:p></p>
</div>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</body>
</html>