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

[-Wunsafe-buffer-usage] Incorrect fixit for unsafe pointer initialized with nullptr

Open jkorous-apple opened this issue 2 years ago • 0 comments

There is a typo in test file https://github.com/llvm/llvm-project/blob/main/clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-local-var-span.cpp#L117 {{^3}} should be {{[^3]}} (It is copy-pasted to 3 more places in the test file.)

Some of the test cases fail when fixed and unfortunately it masks a real bug in FixVarInitializerWithSpan.

  // If `Init` has a constant value that is (or equivalent to) a
  // NULL pointer, we use the default constructor to initialize the span
  // object, i.e., a `std:span` variable declaration with no initializer.
  // So the fix-it is just to remove the initializer.
  if (Init->isNullPointerConstant(Ctx,
          // FIXME: Why does this function not ask for `const ASTContext
          // &`? It should. Maybe worth an NFC patch later.
          Expr::NullPointerConstantValueDependence::
              NPC_ValueDependentIsNotNull)) {
    std::optional<SourceLocation> InitLocation =
        getEndCharLoc(Init, SM, LangOpts);
    if (!InitLocation)
      return {};

    SourceRange SR(Init->getBeginLoc(), *InitLocation);

The problem is that for this form of initialization int * ptr = nullptr; the fixit removes only the "nullptr" but leaves "=" which makes the code syntactically invalid.

I can't think of a simple-enough solution that would cover all cases so filing issue for later.

int * ptr = nullptr; int * ptr {nullptr}; The latter form is likely very uncommon but we still need to account for it.

jkorous-apple avatar Feb 14 '24 21:02 jkorous-apple