2022

Top 10 bugs found in C++ projects in 2022

New Year is coming! It means, according to tradition, it's time to recall 10 of the most interesting warnings that PVS-Studio found during 2022.

Top 10 bugs found in C++ projects in 2022

by Vladislav Stolyarov

From the article:

Here the analyzer detected that a function, marked as noexcept, calls a function that throws an exception. If an exception arises from the nothrow function's body, the nothrow function calls std::terminate, and the program crashes. It could make sense to wrap the setName function in the function-try-block and process the exceptional situation there — or one could use something else instead of generating the exception.

libstdc++ gets C++20 chrono

Screenshot_2022-12-24_094211.pngImagine Jonathan Wakely in a red suit with his helper elves, delivering presents:

libstdc++ gets C++20 <chrono>

As seen on Reddit:

It looks like Jonathan Wakely has just today contributed a huge amount of effort towards <chrono>.

He's added the time zones, leap seconds, all that good stuff.

He's even added GDB pretty printers for inevitable date time debugging!

And these features are supported in <format>, which libstdc++13 has, if you weren't already aware. He's also made many other various improvements. Lets give a round of applause to Jonathan Wakely!

Using modules in the big three compilers

With detailed step by step instructions on how to make it work...

Using modules in the big three compilers: a small experiment

 From the article:

The goal of my experiment was to see how easy it is to write code that a) uses C++20 modules, b) can be compiled by GCC, Clang and MSVC without using conditional compilation, c) imports something from the standard library, d) exports at least one templated function, e) has a peculiarity that makes the module harder to find (in my case, the module is named b but the file that contains it is named a.cppm).

The experiment sort of succeeded...

The Strategy Pattern -- Rainer Grimm

A canonical example:

 

The Strategy Pattern

by Rainer Grimm

From the article:

Purpose: Defines a family of algorithms and encapsulates them in objects

Also known as: Policy

Use case:

  • Different variations of an algorithm are needed
  • The client does not need to know the algorithm
  • The algorithm should be exchangeable at the run time of a program...

The Template Method -- Rainer Grimm

With or without actual templates:

The Template Method

by Rainer Grimm

From the article:

The key idea of the Template Method is easy to get. You define the skeleton of an algorithm that consists of a few typical steps. Implementation classes can only override the steps but cannot change the skeleton. The steps are often called hook methods...

High-confidence Lifetime Checks in Visual Studio version 17.5 Preview 2 -- Gabor Horvath

More safety please:

High-confidence Lifetime Checks in Visual Studio version 17.5 Preview 2

by Gabor Horvath

From the article:

The C++ Core Guidelines’ Lifetime Profile, aims to detect lifetime problems, like dangling pointers and references, in C++ code. ... Lately, there has been an increased push in the C++ community to introduce lifetime-related safety features, which has led us to revisit the lifetime analysis in MSVC.

We spent the last couple of months looking into the results of using the lifetime analysis on real world code. This blog post summarizes our experience and the improvements we made along the way. The biggest change is the introduction of a new set of warnings. These warnings are the high-confidence versions of the existing warnings. Users who want less noise can enable only the high-confidence warnings, while users who want more rigorous checks at the cost of noise can enable both the old and the new warnings. As of 17.5, the high-confidence warnings are still experimental, but depending on the feedback we might include them in some of the recommended profiles in future versions...

C and C++ Profiling Tools -- David Faure

When profiling is a good thing:

C/C++ Profiling Tools

by David Faure

From the article:

This blog will give you a brief overview of profiling C and C++ applications. Additionally, it will lay before you all of the tools available, with the purpose of aiding you in choosing the right tools at the right times...