[cfe-dev] Function return not catching the undefined behavior

Keane, Erich via cfe-dev cfe-dev at lists.llvm.org
Fri Jan 21 07:17:08 PST 2022


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.

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.

From: cfe-dev <cfe-dev-bounces at lists.llvm.org> On Behalf Of Jessica Clarke via cfe-dev
Sent: Friday, January 21, 2022 7:07 AM
To: Craig Topper <craig.topper at gmail.com>
Cc: clang developer list <cfe-dev at lists.llvm.org>
Subject: Re: [cfe-dev] Function return not catching the undefined behavior

This is implementation-defined, not undefined. C99 6.3.1.3p3:

  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.

Jess


On 21 Jan 2022, at 07:16, Craig Topper via cfe-dev <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>> wrote:

I'm not sure that is undefined behavior, but it is caught by -fsanitize=integer

~Craig


On Thu, Jan 20, 2022 at 11:06 PM phy coder via cfe-dev <cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>> wrote:
And if I'm wrong or misunderstand something then please let me know.

On Fri, Jan 21, 2022, 12:00 phy coder <coderphy3 at gmail.com<mailto:coderphy3 at gmail.com>> wrote:
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:

#include<iostream>

char func(int a, int b) {
int sum = a+b;
return sum ;
}

int main() {
int n;
char s = func(100,100);
int t = func(100,100);
std::cout << "hello" << s << t;
return 0;

}

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
Kinda weird for me .
_______________________________________________
cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
_______________________________________________
cfe-dev mailing list
cfe-dev at lists.llvm.org<mailto:cfe-dev at lists.llvm.org>
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20220121/8fa87544/attachment-0001.html>


More information about the cfe-dev mailing list