github / github/codeql

Weird(?) behavior of `Expr.getType`

未关闭
#4,180 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
C++ enhancement
主要语言
CodeQL
星标
10.1k
派生
2.1k
平均合并
2 天 15 小时
30 天内合并 PR
141

描述

Hello,
I'm not sure if this place is relevant to post this topic(I'm sorry if not), but
I can't see what is going on with the below snippet:

```codeql
import cpp

class Set1 extends FunctionCall {
Set1() { this.getNumberOfArguments() >= 1 and exists(Expr e | 1 = 1 | this.getArgument(0) = e) }
}
class Set2 extends FunctionCall {
Set2() { this.getNumberOfArguments() >= 1 and exists(Type t | 1 = 1 | this.getArgument(0).getType() = t) }
}
from FunctionCall fc
where fc instanceof Set1 and (not fc instanceof Set2)
select fc
```

I was writing some simple query, which checks the type of the arguments of a certain sort of function calls.
But I realized that for some reason calling `getType()` narrows the result, regardless of any further operations.
My understanding is that `FunctionCall.getArgument(0)` always returns `Expr` as long as the number of arguments is greater than 0, and that `Expr.getType()` always returns some meaningful instance of `Type`.
Is this assumption wrong? Or is this a bug or something?

I tested the snippet in some projects including [flatbuffers](https://lgtm.com/projects/g/google/flatbuffers/) and [glibc](https://lgtm.com/projects/g/bminor/glibc/). In both, we can see that there are some function calls satisfying the condition(they are mostly calls of struct operator and __builtin_function, but I saw other types of function calls in a confidential project).

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。