undefined behavior

The Code Analyzer is wrong. Long live the Analyzer!

Combining many actions in a single C++ expression is a bad practice, as such code is hard to understand, maintain, and it is easy to make mistakes in it. For example, one can instill a bug by reconciling different actions when evaluating function arguments. We agree with the classic recommendation that code should be simple and clear. Now let's look at an interesting case where the PVS-Studio analyzer is technically wrong, but from a practical point of view, the code should still be changed.

The Code Analyzer is wrong. Long live the Analyzer!

by Andrey Karpov

From the article:

As you can see, once upon a time std::make_pair was taking arguments by value. If std::unique_ptr had existed at that time, then the code above would have been indeed incorrect. Whether this code would work or not would be a matter of luck. In practice, of course, this situation would never have occurred, since std::unique_ptr appeared in C++11 as a replacement for std::auto_ptr. Let's go back to our time. Starting with C++11, the constructor started to use move semantics.

PVS-Studio 6.26 Released

This new release included many interesting improvements related to analysis of C and C++ code, about which we'd like to tell our users.

PVS-Studio 6.26 Released

by Andrey Karpov

From the article:

A developer believes that this is a bug in the compiler. Nevertheless, it is the author of the code who is wrong. The function does not work correctly due to the fact that undefined behavior occurs in it. The compiler follows that in the r variable a certain sum is calculated. Overflow of the r variable must not happen. Otherwise, it is undefined behavior, which doesn't have to be considered or taken into account by a compiler. So, the compiler thinks that since the value of r variable after ending the loop cannot be negative, then r & 0x7fffffff operation is not needed to reset the sigh bit and the compiler just returns the value of r variable from the function.

 

C++Now Announces Closing Keynote by John Regehr

C++Now 2018 will be held in Aspen, May 6–11, 2018.

Closing Keynote Announced: John Regehr on Undefined Behavior and Compiler Optimizations

From the announcement:

John is a professor of computer science at the University of Utah, where his research group creates tools for making software more efficient and correct. One of his projects is Csmith, a tool that generates random C programs. Why? To test compilers, of course. Csmith has been used to find more than 500 previously unknown bugs in production-quality compilers.

John will share some of the insights he’s gained from his research into compilers.

He will discuss what undefined behavior means to the compiler and how compiler writers use it in surprising ways generate better code.

 

Why it is important to check what the malloc function returned

Unfortunately, many programmers are careless about checking of pointers, and sometimes they deliberately do not check whether to memory was allocated or not.

Why it is important to check what the malloc function returned

by Andrey Karpov

From the article:

There are 4 reasons at once, each of them is enough to prove that it is so necessary to write a check after you call the malloc function. If someone from your team doesn't write the checks, make him read this article.

  1. Null pointer dereference is undefined behavior
  2. Null pointer dereference is a vulnerability
  3. Where are guarantees that dereferencing of exactly a null pointer will occur?
  4. Where are the guarantees that memset fills the memory in a direct order?

Null Pointer Dereferencing Causes Undefined Behavior

I have unintentionally raised a large debate recently concerning the question if it is legal in C/C++ to use the &P->m_foo expression with P being a null pointer. The programmers' community divided into two camps. The first claimed with confidence that it wasn't legal while the others were as sure saying that it was. Both parties gave various arguments and links, and it occurred to me at some point that I had to make things clear.

Null Pointer Dereferencing Causes Undefined Behavior

by Andrey Karpov

From the article:

This code is incorrect in both C and C++ when the podhd pointer equals 0. If the pointer equals 0, undefined behavior occurs.

 

C++ Papers for Issaquah - Library, Graphics, Networking, Numerics and Undefined Behavior

This is the last part in the series for Issaquah, and its the most diverse:

C++ Papers for Issaquah - Library, Graphics, Networking, Numerics & Undefined Behavior

by Jens Weller

From the Article:

The 4th and last part about the C++ Papers for Issaquah. I already covered the first batch of proposals from the Library subgroup in the previous part, now its all about papers from Library, Graphics, Networking, Numerics and Undefined Behavior. A very diverse part.