undefined-behavior

Where in the C99 standard does it say that signed integer overflow is undefined behavior?

帅比萌擦擦* 提交于 2020-01-30 07:42:08
问题 Where in the C99 standard does it say that signed integer overflow is undefined behavior? I see the comment about unsigned integer overflow being well-defined (see Why is unsigned integer overflow defined behavior but signed integer overflow isn't?) in section 6.2.5: A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be

Where in the C99 standard does it say that signed integer overflow is undefined behavior?

二次信任 提交于 2020-01-30 07:42:07
问题 Where in the C99 standard does it say that signed integer overflow is undefined behavior? I see the comment about unsigned integer overflow being well-defined (see Why is unsigned integer overflow defined behavior but signed integer overflow isn't?) in section 6.2.5: A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be

Can unverified scanf call cause an undefined behavior?

拜拜、爱过 提交于 2020-01-30 06:08:46
问题 Does below snippet invoke undefined behavior in case of an error? #include <stdio.h> int main() { int i; /* Indeterminate */ if (scanf("%d", &i) == 1) /* Initialize */ printf("%d\n", i); /* Success! Print read value */ else printf("%d\n", i); /* Input failed! Is printing `i` UB or not? */ return 0; } What if scanf fails, is an uninitialized variable accessed? EDIT Moreover what if I replace scanf("%d", &i) with my_initializer(&i) : int my_initializer(int *pi) { double room_temp_degc = get

Can unverified scanf call cause an undefined behavior?

江枫思渺然 提交于 2020-01-30 06:07:20
问题 Does below snippet invoke undefined behavior in case of an error? #include <stdio.h> int main() { int i; /* Indeterminate */ if (scanf("%d", &i) == 1) /* Initialize */ printf("%d\n", i); /* Success! Print read value */ else printf("%d\n", i); /* Input failed! Is printing `i` UB or not? */ return 0; } What if scanf fails, is an uninitialized variable accessed? EDIT Moreover what if I replace scanf("%d", &i) with my_initializer(&i) : int my_initializer(int *pi) { double room_temp_degc = get

Is it legal/safe to cast away `const` for a heap-allocated object?

旧街凉风 提交于 2020-01-30 02:31:38
问题 My use case is as follows. I develop a library in which some loaded plugins can create objects (allocated using malloc() by the library), and some other plugins can read properties of those objects but not modify them. For me this is a case of having a non- const API for the creating/writer side and a const API for the reader side, for example: // writer API struct obj *obj_create(void); void obj_set_some_property(struct obj *obj, int property); // reader API int obj_get_some_property(const

Is it legal/safe to cast away `const` for a heap-allocated object?

淺唱寂寞╮ 提交于 2020-01-30 02:30:11
问题 My use case is as follows. I develop a library in which some loaded plugins can create objects (allocated using malloc() by the library), and some other plugins can read properties of those objects but not modify them. For me this is a case of having a non- const API for the creating/writer side and a const API for the reader side, for example: // writer API struct obj *obj_create(void); void obj_set_some_property(struct obj *obj, int property); // reader API int obj_get_some_property(const

C++ UBSAN produces false positives with derived objects

只愿长相守 提交于 2020-01-24 15:14:47
问题 I wanted to use UBSAN (undefined behavior sanitizer) but found it completely worthless as it reports to many false positives. E.g. a simple std::make_shared<int>(42); is enough to trigger warnings like member access within address 0x00000236de70 which does not point to an object of type '_Sp_counted_base' Reducing this example to a MWE shows that the problem is more general with base classes and inheritance: Example: struct Foo{ int f(){ return g(); } virtual int g() = 0; }; struct Bar: Foo{

C++ UBSAN produces false positives with derived objects

妖精的绣舞 提交于 2020-01-24 15:14:06
问题 I wanted to use UBSAN (undefined behavior sanitizer) but found it completely worthless as it reports to many false positives. E.g. a simple std::make_shared<int>(42); is enough to trigger warnings like member access within address 0x00000236de70 which does not point to an object of type '_Sp_counted_base' Reducing this example to a MWE shows that the problem is more general with base classes and inheritance: Example: struct Foo{ int f(){ return g(); } virtual int g() = 0; }; struct Bar: Foo{

Turning vector of shared_ptr into vector of shared_ptr to const

家住魔仙堡 提交于 2020-01-24 03:05:35
问题 Let class A { std::vector<std::shared_ptr<int>> v_; }; Now I'd like to add access to v_ using two public member functions std::vector<std::shared_ptr<int>> const & v() { return v_; } and std::vector<std::shared_ptr<int const> const & v() const { TODO } I cannot replace TODO with return v_; though. One option would be to not return a reference but a copy. Apart from the obvious performance penalty, this would also make the interface somewhat less desirable. Another option is to make TODO equal

Turning vector of shared_ptr into vector of shared_ptr to const

被刻印的时光 ゝ 提交于 2020-01-24 03:04:37
问题 Let class A { std::vector<std::shared_ptr<int>> v_; }; Now I'd like to add access to v_ using two public member functions std::vector<std::shared_ptr<int>> const & v() { return v_; } and std::vector<std::shared_ptr<int const> const & v() const { TODO } I cannot replace TODO with return v_; though. One option would be to not return a reference but a copy. Apart from the obvious performance penalty, this would also make the interface somewhat less desirable. Another option is to make TODO equal