Commit 9971844
[analyzer] Canonicalize the Decls of FieldRegions (#156668)
When calculating the offset of a FieldRegion, we need to find out which
field index the given field refers to.
Previously, if for some reason the field was not found, then the `Idx`
passed to `Layout.getFieldOffset` was out of bounds and caused undefined
behavior when dereferenced an out of bounds element in
`ASTVector::FieldOffsets::operator[]`, which asserts this in debug
builds, but exposes the undefined behavior in release builds.
In this patch, I refactored how we enumerate the fields, and gracefully
handle the scenario where the field is not found.
That case is still bad, but at least it should not expose the undefined
behavior in release builds, and should assert earlier in debug builds
than before.
The motivational case was transformed into a regression test, that would
fail if no canonicalization would happen when creating a FieldRegion.
This was reduced from a production crash.
In the test case, due to how modules work, there would be multiple
copies of the same template specialization in the AST. This could lead
into inconsistent state when the FieldRegion's Decl was different to the
RecordDecl's field - because one referred to the first and the other to
the second. This made `calculateOffset` fail to compute the field index,
triggering the undefined behavior in production.
While this inconsistency gets fixed now, I think the assertion is still
warranted to avoid potential undefined behavior in release builds.
CPP-6691,CPP-6849
Co-authored-by: Marco Borgeaud <[email protected]>1 parent a23a5b0 commit 9971844
File tree
3 files changed
+55
-13
lines changed- clang
- include/clang/StaticAnalyzer/Core/PathSensitive
- lib/StaticAnalyzer/Core
- test/Analysis/modules
3 files changed
+55
-13
lines changedLines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1548 | 1548 | | |
1549 | 1549 | | |
1550 | 1550 | | |
1551 | | - | |
1552 | | - | |
| 1551 | + | |
| 1552 | + | |
1553 | 1553 | | |
1554 | 1554 | | |
1555 | 1555 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1268 | 1268 | | |
1269 | 1269 | | |
1270 | 1270 | | |
1271 | | - | |
1272 | | - | |
1273 | | - | |
1274 | | - | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
| 1274 | + | |
1275 | 1275 | | |
1276 | 1276 | | |
1277 | 1277 | | |
| |||
1704 | 1704 | | |
1705 | 1705 | | |
1706 | 1706 | | |
1707 | | - | |
1708 | | - | |
1709 | | - | |
1710 | | - | |
1711 | | - | |
1712 | | - | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
| 1716 | + | |
| 1717 | + | |
| 1718 | + | |
1713 | 1719 | | |
| 1720 | + | |
1714 | 1721 | | |
1715 | 1722 | | |
1716 | | - | |
| 1723 | + | |
1717 | 1724 | | |
1718 | 1725 | | |
1719 | 1726 | | |
| |||
Lines changed: 35 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
0 commit comments