undefined-behavior

Reading an indeterminate value invokes UB? [duplicate]

蹲街弑〆低调 提交于 2019-12-23 13:13:26
问题 This question already has answers here : (Why) is using an uninitialized variable undefined behavior? (7 answers) Closed 3 years ago . Various esteemed, high rep users on SO keeps insisting that reading a variable with indeterminate value "is always UB". So where exactly is this mentioned in the C standard? It is very clear that an indeterminate value could either be an unspecified value or a trap representation: 3.19.2 indeterminate value either an unspecified value or a trap representation

In a declaration with initialization, can I use a reference to uninitialized data?

痞子三分冷 提交于 2019-12-23 12:49:11
问题 I have a function that creates and returns an object. It also has a side-effect, writing a success flag into a boolean variable: struct MyObject { ... int field1; char field2; bool field3; }; MyObject CreateMyObject(bool& success) { ... } By pure coincidence it happens that I have to store the success flag in my object. So I can write it in this obvious way: bool success; MyObject x = CreateMyObject(success); x.field3 = success; Or in this way: MyObject x = CreateMyObject(x.field3); Does the

Struct alignment and type reinterpretation

不羁的心 提交于 2019-12-23 11:59:43
问题 Lets say I have two types A and B. Then I make this type struct Pair{ A a; B b; }; Now I have a function such as this. void function(Pair& pair); And lets assume that function will only ever use the a part of the pair. Then is it undefined behavior to use and call the function in this way? A a; function(reinterpret_cast<Pair&>(a)); I know that a compiler may insert padding bytes after a member but can it also do it before the first member? 回答1: I think it's defined behavior, assuming Pair is

Call to function (unknown) through pointer to incorrect function type

你说的曾经没有我的故事 提交于 2019-12-23 11:48:11
问题 I have a program that dynamically links against a library. The program passes a function pointer to that library, to execute. But the ubsan (Undefined Behavior Sanitizer) specified that the pointer is on an incorrect function type. And that occurs only if the callback function has a class as parameter if the callback function has a class as parameter, but only forward declared if I specify the compilation flags: -fvisibility=hidden. I use clang to compile my project. Is it a bug in clang

Call to function (unknown) through pointer to incorrect function type

落爺英雄遲暮 提交于 2019-12-23 11:47:57
问题 I have a program that dynamically links against a library. The program passes a function pointer to that library, to execute. But the ubsan (Undefined Behavior Sanitizer) specified that the pointer is on an incorrect function type. And that occurs only if the callback function has a class as parameter if the callback function has a class as parameter, but only forward declared if I specify the compilation flags: -fvisibility=hidden. I use clang to compile my project. Is it a bug in clang

C++: Return type of std::tie with std::ignore

我们两清 提交于 2019-12-23 09:55:51
问题 I am wondering if the C++11 standard gives any requirement about the type of the std::tuple returned by std::tie when some arguments are std::ignore . More specifically, can I assume that: decltype(std::tie(42, std::ignore)) is not the same as decltype(std::tie(std::ignore, 42)) decltype(std::tie(42, std::ignore)) is not the same as decltype(std::tie(42)) decltype(std::tie(std::ignore, 42)) is not the same as decltype(std::tie(42)) decltype(std::tie(std::ignore, std::ignore)) is not the same

Does calling printf without a proper prototype invoke undefined behavior?

大兔子大兔子 提交于 2019-12-23 09:40:45
问题 Does this innocent looking program invoke undefined behavior: int main(void) { printf("%d\n", 1); return 0; } 回答1: Yes invoking printf() without a proper prototype (from the standard header <stdio.h> or from a properly written declaration) invokes undefined behavior. As documented in C11 Annex J (informative only) J2 Undefined Behavior For call to a function without a function prototype in scope where the function is defined with a function prototype, either the prototype ends with an

If a part of the program exhibits undefined behavior, would it affect the remainder of the program?

£可爱£侵袭症+ 提交于 2019-12-23 09:08:05
问题 Suppose that a programmer forgot to initialize one of his automatic variables, and that he used its value, thereby invoking undefined behavior. ... int i = 0, j; ... printf("value of 'j': %d\n", j); ... ... char buf[256]; fputs("Enter query:", stdout); fgets(buf, sizeof(buf), stdin); ... //process input ... perform other tasks The programmer noticed gibberish characters on the screen, and realized that his program is erroneous, but it did not crash and went on anyway. Supposing after this

If the value of an uninitialized variable shouldn't affect the value of an expression, is it still UB?

这一生的挚爱 提交于 2019-12-23 07:49:44
问题 This is a follow-on from a discussion, which I think deserves a question of its own. Basically, is the result of this undefined? int x; int y = 1 || x; There are two "common-sense" arguments here: Mathematically speaking, no matter what the value of x , the value of y should be 1 . Because of short-circuiting, x is never evaluated anyway. But the counterargument is that we have an expression that involves an uninitialized variable, so all bets are off (in theory). More generally, if the value

Is a goto in alloca's function scope valid?

浪子不回头ぞ 提交于 2019-12-23 07:29:26
问题 The C standard prohibits a goto into a function scope where a VLA exists. A VLA and the call to alloca function should have the same result on low level. (I could be wrong, as I'm just a C, not a low level programmer, but in my imagin that appears to be witty) So will the following snippet be also undefined behaivng? int main() { char *p; goto label1; { p = _alloca(1); label1: p = NULL; } } Ofcourse I cant refference p , but whats about the behaviour? 回答1: Actually, the rule 6.8.6.1 states: A