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

False positive on -Wmismatched-new-delete if variable is reused

Open Sir-Walrus opened this issue 7 months ago • 8 comments

// Type your code here, or load an example.
int square(int num) {
    int* g = new int;
    delete g;
    g = new int[4];
    delete[] g;
    return num * num;
}

https://godbolt.org/z/jcWePexce ->

<source>:6:5: warning: 'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'? [-Wmismatched-new-delete]
    6 |     delete[] g;
      |     ^     ~~
<source>:3:14: note: allocated with 'new' here
    3 |     int* g = new int;
      |              ^

No it's not, it's a different pointer that happens to reuse the variable.

The code is admittedly dumb, but feels worth asking about, at least.

Sir-Walrus avatar May 04 '26 13:05 Sir-Walrus

@llvm/issue-subscribers-clang-frontend

Author: Alcaro

```c++ // Type your code here, or load an example. int square(int num) { int* g = new int; delete g; g = new int[4]; delete[] g; return num * num; } ``` https://godbolt.org/z/jcWePexce -> ``` <source>:6:5: warning: 'delete[]' applied to a pointer that was allocated with 'new'; did you mean 'delete'? [-Wmismatched-new-delete] 6 | delete[] g; | ^ ~~ <source>:3:14: note: allocated with 'new' here 3 | int* g = new int; | ^ ``` No it's not, it's a different pointer that happens to reuse the variable.

The code is admittedly dumb, but feels worth asking about, at least.

For a delete expression on some variable, the current implementation of MismatchingNewDeleteDetector issues a warning when it does not match the initialization new expression of that variable (this is presumably based on the assumption that initialized pointers should be used consistently). In this example, it detects the deleter to g (i.e., delete[] g) does not match its initializer (i.e., int *g = new int).

https://github.com/llvm/llvm-project/blob/00562c50f3f5c84885deb48ff1d4043bb903a79e/clang/lib/Sema/SemaExprCXX.cpp#L3852-L3855

It might be more reasonable to issue a warning only when none of the initializers to some variable, say, g, matches a deleter to g.

jjppp avatar May 05 '26 05:05 jjppp

It might be more reasonable to issue a warning only when none of the initializers to some variable, say, g, matches a deleter to g.

@jjppp It's not a correct one.

For

int *g = new int;
delete g;
g = new int[4];

The warning will be missed.

TPPPP72 avatar May 09 '26 07:05 TPPPP72

It might be more reasonable to issue a warning only when none of the initializers to some variable, say, g, matches a deleter to g.

@jjppp It's not a correct one.

For

int *g = new int;
delete g;
g = new int[4];

The warning will be missed.

Yes, I understand that some level of path sensitivity is required here as long as we want both soundness and (relatively) better precision. If that is the case, I believe a new analysis is required.

jjppp avatar May 09 '26 07:05 jjppp

Actually I'm thinking whether it is necessary to fix it?I don't think reuse a pointer variable is a good practice.

The current diagnostic approach uses an O(1) algorithm to match the initialization, which is sufficient in most cases. It's not complicated if we just want a general fix: Just need to reverse the AST.

But it can't always works well in all case.

Like this, we can't know whether this assignment applied to g

int *g = new int{};
if(cond)
    g = new int[4];
delete g;

If we try to reverse AST, I'm concerned it might lengthen complication time.

@shafik

TPPPP72 avatar May 09 '26 10:05 TPPPP72

Yes, this specific input is silly (it's debug code, I was trying to check if my custom malloc managed to override only parts of the new/delete family).

But let's consider a slightly less silly example - a beginner could easily forget the [] on a delete.

// Type your code here, or load an example.
int square(int num) {
    int* g;
    g = new int[4];
    delete g;
    return num * num;
}

Clang ignores it, because g has no initializer.

GCC, however, rightfully complains. https://godbolt.org/z/bfcvK4ssb

Sir-Walrus avatar May 09 '26 14:05 Sir-Walrus

Yes, this specific input is silly (it's debug code, I was trying to check if my custom malloc managed to override only parts of the new/delete family).

But let's consider a slightly less silly example - a beginner could easily forget the [] on a delete.

// Type your code here, or load an example. int square(int num) { int* g; g = new int[4]; delete g; return num * num; } Clang ignores it, because g has no initializer.

GCC, however, rightfully complains. https://godbolt.org/z/bfcvK4ssb

The original case is not very compelling one but this is more compelling. We have to see if there is a way to do it without having significant compile time hit. It is hard to know w/o seeing some actual numbers.

shafik avatar May 09 '26 17:05 shafik

The original case is not very compelling one but this is more compelling. We have to see if there is a way to do it without having significant compile time hit. It is hard to know w/o seeing some actual numbers.

We can traverse the AST from VarDecl until find the first concrete new. However, I think in the worst case, this is not fundamentally different from directly traversing the entire scope of the AST.

TPPPP72 avatar May 09 '26 18:05 TPPPP72