]> git.ipfire.org Git - thirdparty/strongswan.git/commit
lgtm: Add query to detect problematic uses of chunk_from_chars()
authorTobias Brunner <tobias@strongswan.org>
Mon, 27 Jan 2020 14:16:51 +0000 (15:16 +0100)
committerTobias Brunner <tobias@strongswan.org>
Mon, 27 Jan 2020 17:31:09 +0000 (18:31 +0100)
commit8ea13bbc5ccdb7a67e5b2c0e0465d432dd24614b
tree0fce0bd73bccccc5eb78ab6c7e85083c552fb889
parent9c6ab717822bd52f21c519244be327d9065ecbff
lgtm: Add query to detect problematic uses of chunk_from_chars()

GCC 9+ and clang 4+ (partially) optimize out usages of
chunk_from_chars() if the value is read outside of the block where the
macro is used.  For instance:

```
chunk_t chunk = chunk_empty;
if (...)
{
chunk = chunk_from_chars(0x01, 0x06);
}
/* do something with chunk */
```

The chunk_from_chars() macro expands to a chunk_t declaration, which is
technically only defined inside that block.

Still, with older GCC versions the fourth line was compiled to something
like this:

```
mov     WORD PTR [rsp+14], 1537 # 0x0106 in little-endian
lea     rdx, [rsp+14]
mov     ecx, 2
```

However, with GCC 9.1 and -O2 the first instruction might be omitted
(strangely the others usually were not, so the chunk pointed to whatever
was stored on the stack).  It's not easily reproducible, so there are
situations where the seemingly identical code is not optimized in this
way.

This query should detect such problematic uses of the macro (definition
and usage in different blocks).

References #3249.
.lgtm/cpp-queries/chunk_from_chars.ql [new file with mode: 0644]