Skip to content

Fix FP due to namespace scope#3475

Merged
danmar merged 1 commit into
cppcheck-opensource:mainfrom
KenPatrickLehrmann:namespacescope
Oct 4, 2021
Merged

Fix FP due to namespace scope#3475
danmar merged 1 commit into
cppcheck-opensource:mainfrom
KenPatrickLehrmann:namespacescope

Conversation

@KenPatrickLehrmann
Copy link
Copy Markdown
Contributor

namespace test {
class Foo {};
struct Bar {
  Foo *_foo;
};

int f(Bar *bar);

void g(Bar *bar) {
  {
    Foo foo;
    bar->_foo = &foo;
    bar->_foo = nullptr;
  }
  f(bar);
}
}

would give

a.cpp:15:5: error: Using pointer to local variable 'foo' that is out of scope. [invalidLifetime]
  f(bar);
    ^
a.cpp:12:17: note: Address of variable taken here.
    bar->_foo = &foo;
                ^
a.cpp:11:9: note: Variable created here.
    Foo foo;
        ^

@pfultz2
Copy link
Copy Markdown
Contributor

pfultz2 commented Sep 30, 2021

I am not sure why the windows CI is failing, it seems unrelated to this PR.

```
namespace test {
class Foo {};
struct Bar {
  Foo *_foo;
};

int f(Bar *bar);

void g(Bar *bar) {
  {
    Foo foo;
    bar->_foo = &foo;
    bar->_foo = nullptr;
  }
  f(bar);
}
}
```
would give
```
a.cpp:15:5: error: Using pointer to local variable 'foo' that is out of scope. [invalidLifetime]
  f(bar);
    ^
a.cpp:12:17: note: Address of variable taken here.
    bar->_foo = &foo;
                ^
a.cpp:11:9: note: Variable created here.
    Foo foo;
        ^
```
@danmar danmar merged commit 61cddab into cppcheck-opensource:main Oct 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants