Commit 564b4fc
committed
[silgenpattern] Fix some stack-use-after-free errors caused by iterating over an Optional<ArrayRef<T>>.
Specifically the bad pattern was:
```
for (auto *vd : *caseStmt->getCaseBodyVariables()) { ... }
```
The problem is that the optional is not lifetime extended over the for loop. To
work around this, I changed the API of CaseStmt's getCaseBodyVariable methods to
never return the inner Optional<MutableArrayRef<T>>. Now we have the following 3
methods (ignoring const differences):
1. CaseStmt::hasCaseBodyVariables().
2. CaseStmt::getCaseBodyVariables(). Asserts if the case body variable array was
never specified.
3. CaseStmt::getCaseBodyVariablesOrEmptyArray(). Returns either the case body
variables array or an empty array if we were never given any case body
variable array.
This should prevent anyone else in the future from hitting this type of bug.
radar://496097171 parent dc7879d commit 564b4fc
File tree
6 files changed
+35
-39
lines changed- include/swift/AST
- lib
- AST
- SILGen
- Sema
6 files changed
+35
-39
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1055 | 1055 | | |
1056 | 1056 | | |
1057 | 1057 | | |
1058 | | - | |
1059 | | - | |
1060 | | - | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
| 1062 | + | |
| 1063 | + | |
1061 | 1064 | | |
1062 | 1065 | | |
1063 | 1066 | | |
1064 | 1067 | | |
1065 | | - | |
1066 | | - | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
1067 | 1078 | | |
1068 | 1079 | | |
1069 | 1080 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1622 | 1622 | | |
1623 | 1623 | | |
1624 | 1624 | | |
1625 | | - | |
| 1625 | + | |
1626 | 1626 | | |
1627 | 1627 | | |
1628 | 1628 | | |
1629 | 1629 | | |
1630 | 1630 | | |
1631 | | - | |
| 1631 | + | |
1632 | 1632 | | |
1633 | 1633 | | |
1634 | 1634 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5094 | 5094 | | |
5095 | 5095 | | |
5096 | 5096 | | |
5097 | | - | |
| 5097 | + | |
5098 | 5098 | | |
5099 | 5099 | | |
5100 | 5100 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2349 | 2349 | | |
2350 | 2350 | | |
2351 | 2351 | | |
2352 | | - | |
2353 | | - | |
2354 | | - | |
2355 | | - | |
| 2352 | + | |
| 2353 | + | |
2356 | 2354 | | |
2357 | 2355 | | |
2358 | 2356 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2332 | 2332 | | |
2333 | 2333 | | |
2334 | 2334 | | |
2335 | | - | |
2336 | | - | |
2337 | | - | |
2338 | | - | |
2339 | | - | |
2340 | | - | |
| 2335 | + | |
| 2336 | + | |
2341 | 2337 | | |
2342 | 2338 | | |
2343 | 2339 | | |
| |||
2365 | 2361 | | |
2366 | 2362 | | |
2367 | 2363 | | |
2368 | | - | |
2369 | | - | |
2370 | | - | |
2371 | | - | |
2372 | 2364 | | |
2373 | 2365 | | |
2374 | 2366 | | |
2375 | | - | |
| 2367 | + | |
2376 | 2368 | | |
2377 | 2369 | | |
2378 | 2370 | | |
| |||
2436 | 2428 | | |
2437 | 2429 | | |
2438 | 2430 | | |
2439 | | - | |
2440 | | - | |
| 2431 | + | |
2441 | 2432 | | |
2442 | 2433 | | |
2443 | 2434 | | |
| |||
2448 | 2439 | | |
2449 | 2440 | | |
2450 | 2441 | | |
2451 | | - | |
| 2442 | + | |
2452 | 2443 | | |
2453 | 2444 | | |
2454 | 2445 | | |
| |||
2606 | 2597 | | |
2607 | 2598 | | |
2608 | 2599 | | |
2609 | | - | |
| 2600 | + | |
2610 | 2601 | | |
2611 | 2602 | | |
2612 | 2603 | | |
2613 | 2604 | | |
2614 | 2605 | | |
2615 | 2606 | | |
2616 | | - | |
| 2607 | + | |
2617 | 2608 | | |
2618 | 2609 | | |
2619 | 2610 | | |
| |||
2640 | 2631 | | |
2641 | 2632 | | |
2642 | 2633 | | |
2643 | | - | |
2644 | | - | |
| 2634 | + | |
2645 | 2635 | | |
2646 | 2636 | | |
2647 | 2637 | | |
| |||
2658 | 2648 | | |
2659 | 2649 | | |
2660 | 2650 | | |
2661 | | - | |
| 2651 | + | |
2662 | 2652 | | |
2663 | 2653 | | |
2664 | 2654 | | |
| |||
2845 | 2835 | | |
2846 | 2836 | | |
2847 | 2837 | | |
2848 | | - | |
2849 | | - | |
| 2838 | + | |
2850 | 2839 | | |
2851 | 2840 | | |
2852 | 2841 | | |
| |||
2856 | 2845 | | |
2857 | 2846 | | |
2858 | 2847 | | |
2859 | | - | |
| 2848 | + | |
2860 | 2849 | | |
2861 | 2850 | | |
2862 | 2851 | | |
2863 | 2852 | | |
2864 | 2853 | | |
2865 | | - | |
| 2854 | + | |
2866 | 2855 | | |
2867 | 2856 | | |
2868 | 2857 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2414 | 2414 | | |
2415 | 2415 | | |
2416 | 2416 | | |
2417 | | - | |
2418 | | - | |
2419 | | - | |
2420 | | - | |
| 2417 | + | |
| 2418 | + | |
2421 | 2419 | | |
2422 | 2420 | | |
2423 | 2421 | | |
| |||
0 commit comments