A round of applause to the Tor project

Andrey Karpov
Articles: 327



My congratulations to the authors of the Tor project. I didn't manage to find any errors after the analysis by PVS-Studio static code analyzer. We write such words very rarely, so the authors may really be proud. They do deserve a medal for the high-quality code.

Picture 1

This is not the first time when we check Tor using PVS-Studio analyzer. The previous check was in 2012, after that I wrote a small post: "Security, security! But do you test it?" Interesting enough, 5 years ago I could find errors, but now they are not there.

Over the past 5 years we have significantly developed the diagnostic capabilities of PVS-Studio, and it learned to identify new patterns of errors, such as memory leaks, dead code, incorrect processing of the strings of BSTR type and much more. We easily find error in the code of such projects as GCC, LLVM, C++ REST SDK, GDB, Qt, Chromium, Linux kernel (see the list of articles). But we couldn't write anything about errors in Tor!

What does it show? The authors of the project started taking the quality of the code very seriously.

There were also cases when we didn't find any errors first, but when the analyzer became more powerful, we managed to find them. This is the first case, when we used to detect errors, but now they are not. Tor developers did a really great job! They are really an example for other programmers.

A couple of words about the check itself. We checked the latest version of the Tor source code at the time of writing this article. We used PVS-Studio, version 6.15 for the analysis. The analyzer issued a certain number of warnings, but they aren't very useful, as these are mostly false positives. Having studies the report, I noted down 9 patterns of false positives, that will help us improve the analyzer. It turned out that it wasn't the check of Tor project, but rather the additional testing of PVS-Studio.

As I have said, there were some warnings, which weren't really false positives, but there is no real use in them. A simple example:

ssize_t read_all(...., size_t count, ....)
{
  ....
  if (count > SIZE_T_CEILING || count > SSIZE_MAX) {

The analyzer issues a warning: V590 Consider inspecting this expression. The expression is excessive or contains a misprint. util.c 2116

Let's take a look after expanding the macros:

if (count > ((size_t)(0x7fffffffffffffffL -16)) ||
    count > 0x7fffffffffffffffL) {

Yes, the code is redundant: the second comparison can be removed. However, it's clear that there is no need to fix this code, because this check may be useful with other values of the constants. Which means that it is a false positive, but we don't know how to teach the analyzer to ignore such fragments.

There are some meaningful warnings, but they aren't enough to write an article with the title "Bugs in Tor project". One of such cases:

const char *err = strchr(cp, ':')+2;
tor_assert(err);

The assert would never work. Even if the function strchr returns NULL, then the pointer err will store an invalid value ((char *)2), but it won't be NULL. It would be correct to write the following:

const char *err = strchr(cp, ':');
tor_assert(err);
err += 2;

However, as I understand, the function strchr will never return NULL, and the check is written just in case.

I could note just one warning out of all of them, which indicates an error. Let's consider the following code fragment:

static time_t
edge_of_accounting_period_containing(time_t now, int get_end)
{
  ....
  case UNIT_MONTH: {
    if (tm.tm_mday < cfg_start_day ||
        (tm.tm_mday < cfg_start_day && before)) {
      --tm.tm_mon;
    }
  ....
}

PVS-Studio warning: V686 A pattern was detected: (tm.tm_mday < cfg_start_day) || ((tm.tm_mday < cfg_start_day) && ...). The expression is excessive or contains a logical error. hibernate.c 333

The check can be simplified to:

if (tm.tm_mday < cfg_start_day) {

Perhaps, there is a typo or a logical error in this condition.

There is nothing more I could write about. There is a change that I could have inadvertently skipped some error, but this wouldn't change the overall picture. Checking other open source projects, we find dozens and even hundreds errors. At this point we have more than 10 000 bugs in our collection, found in various open source projects. I would be happy to find some in Tor, and then describe 20-30 of the most interesting of them, but I couldn't. Once more, the authors of the project did a great job.



Use PVS-Studio to search for bugs in C, C++, and C# code

We offer you to check your project code with PVS-Studio. Just one bug found in the project will show you the benefits of the static code analysis methodology better than a dozen of the articles.

goto PVS-Studio;

Andrey Karpov
Articles: 327


Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++, and C#

goto PVS-Studio;