Blender And A Null Pointer

Andrey Karpov
2 min readMar 9, 2021

This is a continuation of a small note series about regular checks of the Blender project with the PVS-Studio static analyzer. The aim of these publications is to demonstrate how it is beneficial to use static code analysis tools regularly. In doing so, many errors can be found at the earliest stage, and fixing them will be cheap and fast.

This time, the error crept into a new file — geometry_component_mesh.cc. According to the description, this file was made from a part of previously existing code.

Cleanup: Move geometry component implementations to separate files.

It turns out that the error actually was already there. It was noticed now because the code containing it was moved to a different file. Just a reminder — we only look at new errors and ignore the once found earlier. So, on the one hand, this example is not entirely honest, but, on the other hand, the file is new, the error is there, and it is revealed. Profit :).

So, the error itself:

ReadAttributePtr try_get_for_read(
const GeometryComponent &component,
const StringRef attribute_name) const final
{
....
const Mesh *mesh = mesh_component.get_for_read();
....
if (mesh == nullptr || mesh->dvert == nullptr) {
static const float default_value = 0.0f;
return std::make_unique<ConstantReadAttribute>(
ATTR_DOMAIN_POINT, mesh->totvert,
CPPType::get<float>(), &default_value);
}
....
}

PVS-Studio warning: V522: There might be dereferencing of a potential null pointer ‘mesh’. geometry_component_mesh.cc 537

We’ve got a faulty error handler here. Is the mesh pointer is null, it will be dereferenced. This seems benign — after all, the condition won’t be satisfied often. That’s why the error has been under the radar. On the other hand, it will probably pop up on the user side at some point. It will spoil the experience with the program, and reproducing and fixing it will most likely be difficult.

In its turn, the PVS-Studio analyzer doesn’t break a sweat finding an error like this. And it virtually costs nothing to fix it right away, without getting any negative user feedback.

Use static code analysis! It will help you find and fix many errors easily, while you’re still writing the code.

Previous articles on the Blender project:

--

--

Andrey Karpov

Co-founder of the PVS-Studio project. Microsoft MVP in the ‘Developer Technologies’ nomination and PhD in Physics and Mathematics.