llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

scan-build reports false positive nullptr dereference because it apparently incorrectly tracks properties of std::initializer_list as argument of a constructor

Open llvmbot opened this issue 8 years ago • 6 comments

Bugzilla Link 39042
Version 7.0
OS Linux
Attachments The full source and output of scan-build including the html-output
Reporter LLVM Bugzilla Contributor

Extended Description

I encountered false positive reports of nullptr dereferences when constructing objects from std::initializer_list and storing information from the initializer lists in member arrays. The actual problem occurred with nested initializer lists, but I could reproduce the problem also with a simple 1D case:

#include <memory>
#include <initializer_list>
#include <iostream>

class TestClass
{
    public:
        size_t* ptr_;

        TestClass() : ptr_(nullptr) {}

        TestClass(const std::initializer_list<size_t> &ini)
            : TestClass()
        {
            // check whether the list contains elements
            // The scan-build HTML-report wrongly suggests
            // that this condition is true and that the constructor
            // returns from here resulting in no change to ptr_.
            if (ini.size() == 0)
            {
                return;
            }

            // save the number of elements in *ptr_
            // The program actually arrives here as it should.
            ptr_ = new size_t;
            *ptr_ = ini.size();
       }

       ~TestClass() { delete ptr_; }
};

int main ()
{
    // Scan-build doesn't recognize that testObj.ptr_ is not nullptr
    // after constructing the object from the initializer list.
    TestClass testObj =
    {
        1,
    };

    // This correctly outputs 1, and the program also gives no errors
    // when checked with valgrind. However, scan-build reports a
    // nullptr dereference.
    std::cout << "*testObj.ptr_ = " << *testObj.ptr_ << std::endl;

    return 0;
}

llvmbot avatar Sep 21 '18 22:09 llvmbot

If the constructor uses a nested initializer list, the value returned by size() of the initializer list is tracked correctly, but properties of the inner nesting levels are not. Could this point to an incomplete recursion?

llvmbot avatar Sep 22 '18 16:09 llvmbot

assigned to @devincoughlin

llvmbot avatar Sep 21 '18 22:09 llvmbot

Bisected: the reported behavior no longer reproduces starting with https://github.com/llvm/llvm-project/commit/c019142a89b477cd247434c1d8f571662d26e19d.

Godbolt: https://godbolt.org/z/6on9naYvY

  • Clang 13.0.1: Reports Dereference of null pointer (loaded from field 'ptr_') at the testObj.ptr_ access
  • Clang 14.0.0: No longer reports the null-dereference warning
  • Clang (trunk): No longer reports the null-dereference warning

This is only an observed change in behavior. I have not checked whether this commit addresses the cause of this report or only stops it from reproducing; whether that resolves the issue is up to the maintainers.

Prepared with assistance from Codex.

hongtaihu avatar Nov 01 '26 09:11 hongtaihu

This was likely accidentally "fixed" by https://github.com/llvm/llvm-project/commit/c019142a89b477cd247434c1d8f571662d26e19d which was an NFC commit.

The change moved some code that checks isInSystemHeader and then marked a bug report invalid. This is likely what suppresses the report in this issue. Here is a minimized test case for demonstrating that behavior change: https://godbolt.org/z/Ej9asG9zM

     # 1 "sys.h" 1 3
     static inline int sysfn(int a) { if (a) return 1; return 0; }
     # 5 "repro.c" 2
     int f(void) {
       int x;
       sysfn(1);
       return x; // expected uninitialized-return warning
     }

This means that this was accidentally fixed in clang-14.

steakhal avatar Nov 04 '26 19:11 steakhal

@benedekaibas could you have a look at this and judge if we should add a test for this?

steakhal avatar Nov 04 '26 19:11 steakhal

8. @benedekaibas could you have a look at this and judge if we should add a test for this?

Since you included a minimized test case I was looking for the semantic of that in the test suite. This is not yet covered, so we should add a test case for it.

benedekaibas avatar Nov 04 '26 23:11 benedekaibas