llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.81k stars 11.91k forks source link

variable set in two consecutive try-catch blocks marked as never read [clang-analyzer-deadcode.DeadStores] #32514

Open llvmbot opened 7 years ago

llvmbot commented 7 years ago
Bugzilla Link 33167
Version 3.9
OS Linux
Attachments minimal working example for the bug
Reporter LLVM Bugzilla Contributor
CC @akrzemi1,@AnnaZaks,@VReichelt,@Xazax-hun

Extended Description

Problem found on: clang-tidy --version LLVM (http://llvm.org/): LLVM version 4.0.0 Optimized build. Default target: x86_64-unknown-linux-gnu Host CPU: haswell

A variable set in two consecutive try-catch blocks is marked as never read (I assume it means it's never read before it is stored to again). However, if the second function call (bar2 in the example) will throw, the second store will never happen.

The bug shows for the most basic clang-tidy invocation: $ clang-tidy clang-tidy-error-ex.cpp

0f73b9cf-134f-41af-a8b1-14d9f305ee95 commented 7 years ago

I am CC-ed on all of Ted's bugs. I'll ask Tanya to set the screener to either me or someone else on our team.

Xazax-hun commented 7 years ago

Is Ted still a good default assignee for the SA bugs?

I doubt that, he is no longer the official owner/maintainer of the SA codebase. I think Anna would be a better default.

llvmbot commented 7 years ago

Is Ted still a good default assignee for the SA bugs?

0acd4e0f-fb71-46f2-a106-6376379b1b59 commented 7 years ago

Same with the following example (in version 3.9):

#include <iostream>

void configure() { throw 0;}

int main()
{
  bool configured = true;

  for (int i = 0; i < 10 ; ++i)
  {
    try {
    configured = false;
    configure();
    configured = true;
    }
    catch (...) {}
  }
  std::cout << configured << std::endl;
}

analyzer complains that store configured = false is never read, but as the output statement shows, we are able to observe the store.

VReichelt commented 7 years ago

Here's a similar code snippet with just one try-catch block that produces a false positive with the command

clang-tidy -checks='-*,clang-analyzer-deadcode.DeadStores' bug.cc --
void foo() { throw 0; }

int main()
{
  int i = 0;

  try
  {
    i = 1;
    foo();
    return 0;
  }
  catch(...) {}

  return i;
}
bug.cc:9:5: warning: Value stored to 'i' is never read [clang-analyzer-deadcode.DeadStores]
    i = 1;
    ^
bug.cc:9:5: note: Value stored to 'i' is never read

Since the program actually returns 1, the store in line 9 cannot be dead. This happens since clang 3.5.0.

llvmbot commented 7 years ago

assigned to @AnnaZaks